本文介绍了Windows下C ++中的系列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <memory.h>
#include <windows.h>
#include <winsock2.h>
#include <iostream.h>
#include <lm.h>
#include <vector>
#include <string>
#include <memory.h>
using namespace std;
struct ClientInformation
{
	string sCliName;
	int iPlatId;
	int iMajor;
	int iType;
	int iMinor;
};

class cClient
{
private:
	DWORD dwLevel;
	DWORD dwPrefMaxLen;
	LPSERVER_INFO_101 pBuf;
	DWORD dwEntriesRead;
	DWORD dwTotalEntries;
	DWORD dwServerType;
	LPTSTR pszDomainName;
	NET_API_STATUS nStatus;
	WSADATA wsaData;
	DWORD dwResumeHandle;
	char cszBuf[200];
	SOCKET ClientSocket;
	int iTemp;
	string str;
public:
	cClient()
	{
		dwLevel = 101;
		dwPrefMaxLen = MAX_PREFERRED_LENGTH;
		pBuf = NULL;
		dwEntriesRead = 0;
		dwTotalEntries = 0;
		dwServerType = SV_TYPE_SERVER;
		pszDomainName = NULL;
		dwResumeHandle = 0;	
		
	}
	
	void FindDomainSystem()
	{	
		ClientInformation *StructObj;
		vector <ClientInformation> *myvec;
		WSAStartup(MAKEWORD(2,2), &wsaData);
		nStatus = NetServerEnum(NULL,dwLevel,(LPBYTE *) & pBuf,dwPrefMaxLen,
								&dwEntriesRead,&dwTotalEntries,dwServerType, 
								NULL, &dwResumeHandle);
		myvec = new vector <ClientInformation>();

		if(nStatus == NERR_Success)
		{
			cout<<"The NetServerEnumeration sucessfuly "<<"\n";
			cout<<"The total entires is "<<dwEntriesRead<<"\n\n";
			for(int i=1; i<= dwEntriesRead;i++)
			{
				
				memset(cszBuf,0,sizeof(cszBuf));
                                myvec = new vector <ClientInformation>();
				sprintf( cszBuf, "%S", pBuf->sv101_name );
				cout<<"\nThe server name "<<cszBuf<<"\n";
				StructObj->sCliName.erase();
				StructObj->sCliName.append(cszBuf);
				cout<<"The Platform id of the system is "<<pBuf->sv101_platform_id<<"\n";
				StructObj->iPlatId = pBuf->sv101_platform_id; 
				cout<<"The type of system running is "<<pBuf->sv101_type<<"\n";
				StructObj->iType = pBuf->sv101_type;
				cout<<"The major version of the system is "<<pBuf->sv101_version_major<<"\n";
				StructObj->iMajor = pBuf->sv101_version_major;
				cout<<"The minor version of the system is "<<pBuf->sv101_version_minor<<"\n";
				StructObj->iMinor = pBuf->sv101_version_minor;		
				send(ClientSocket,(char*)&StructObj,sizeof(StructObj),0);
				myvec->push_back(*StructObj);
				pBuf++;
			}
		}
		else
		{
			cout<<"\nThe Net Enumeration producing Error\n";
		}
		cout<<"\n\n\n\nThe vector data\n";
		display(myvec);
		
	}
	void display(vector <ClientInformation> *my)
	{
		BYTE *pData = NULL;
		if(my)
		{
			int i = my->size();
			if(i)
			{
				pData = new BYTE(i*sizeof(ClientInformation));
				ClientInformation *psData = (ClientInformation*)pData;
				for(int i1=0;i1<i;i++)
				{
					memcpy_s(psData[i1],sizeof(ClientInformation),&my->at(i1),sizeof(ClientInformation));  //prodcing error in that place.. what i want to do now...
				}
			}

			//int icount = my
		}
		
	}


};
int main(int argc, char* argv[])
{
	
	cClient o;
	o.FindDomainSystem();
	return 0;
}



先生,..这是我的程序,用于获取该域下可用的系统信息..

问题是我无法通过套接字发送该数据
..
为此,我进行了BYTE操作.

但是memcpy_s();
它给出了错误..

请帮助我



sir.. this is my program that get the system information that are available under the domain..

problem is i can''t able to send that data over the socket
..
for that purpose i done BYTE operation.

but the memcpy_s();
it gives error..

please help me

推荐答案

memcpy_s(psData+i1,sizeof(ClientInformation),&my->at(i1),sizeof(ClientInformation));




or

memcpy_s(&psData[i1],sizeof(ClientInformation),&my->at(i1),sizeof(ClientInformation));




or

memcpy(psData+i1,&my->at(i1),sizeof(ClientInformation));


问候.



new BYTE[...];

,而不是

new BYTE(...);

,即分配BYTES数组而不是单个初始化的BYTE吗?

i.e allocation of an array of BYTES instead of a single initialized BYTE ?


这篇关于Windows下C ++中的系列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:11