程序主要功能是读取本机的硬件信息然后给服务器发送一个json字符串。在本机上运行正常,但是在其他未安装vs的机器上只能显示"start:"
#include <stdio.h>
#include <string.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Iphlpapi.h>
#include <winsock2.h>
#pragma comment(lib,"Iphlpapi.lib")
#pragma comment(lib,"WS2_32.lib")
#define SERVERADDRESS "192.168.1.101";
#define PATH "/PCinfo/host.php";
#define PORT 8080
#define BUFSIZE 4096
#define SUCCESS "Successfully received"
int core_count()
{
int count = 1;
SYSTEM_INFO si;
GetSystemInfo(&si);
count = si.dwNumberOfProcessors;
return count;
}
unsigned mem_size()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
return (unsigned int)statex.ullTotalPhys/1024/1024;
}
int disk_size()
{
int disk_size = 0;
int diskCount = 0;
DWORD diskInfo = GetLogicalDrives();
while(diskInfo)
{
if(diskInfo & 1)
{
diskCount++;
}
diskInfo = diskInfo >> 1;
}
//磁盘数量
//printf("%d\n",diskCount);
int dsLength = GetLogicalDriveStrings(0,NULL);
char *DStr = new char[dsLength];
GetLogicalDriveStrings(dsLength,DStr);
int DType;
int si = 0;
int fResult;
__int64 i64FreeBytesToCaller;
__int64 i64TotalBytes;
__int64 i64FreeBytes;
__int64 total = 0;
for(int i = 0 ; i < dsLength/4 ; i++)
{
char dir[3] = {DStr[si],':'};
DType = GetDriveType(DStr+i*4);
fResult = GetDiskFreeSpaceEx(dir,(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);
if(fResult)
{
total += i64TotalBytes;
}
si += 4;
}
disk_size = total / 1024 / 1024 / 1024;
return disk_size;
}
void hardaddress(char mac[32])
{
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
int nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);
if (ERROR_BUFFER_OVERFLOW==nRel)
{
delete pIpAdapterInfo;
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
nRel=GetAdaptersInfo(pIpAdapterInfo,&stSize);
}
if (ERROR_SUCCESS==nRel)
{
sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X",pIpAdapterInfo->Address[0],pIpAdapterInfo->Address[1],pIpAdapterInfo->Address[2],pIpAdapterInfo->Address[3],pIpAdapterInfo->Address[4],pIpAdapterInfo->Address[5]);
}
if (pIpAdapterInfo)
{
delete pIpAdapterInfo;
}
}
void genJsonString(char json[512])
{
char mac[32];
hardaddress(mac);
sprintf(json,
"data={\"%s\":\"%d\",\"%s\":\"%.2fGB\",\"%s\":\"%dGB\",\"%s\":\"%s\"}",
"cpucore",core_count(),"memorysize",mem_size()/1024.0,"disksize",disk_size(),"mac",mac);
}
void sendPost(char json[512],int n,char returnString[22])
{
struct sockaddr_in server;
struct hostent *hostinfo;
WSADATA wsaData;
WORD wVersionRequested;
SOCKET LocalSock;
char IpAddr[300] = SERVERADDRESS;
char buff[BUFSIZE] = {'\0'};
FILE *pMyDownFile=NULL;
int nWriteBytes=0;
int FindEntityMsgPos=0;
int MsgInfoSize=0;
char *pHostName="127.0.0.1";
char text[2048];
memset(text, 0, sizeof(char) * 2048);
char* path = PATH;
strcat(text, "POST ");
strcat(text, path);
strcat(text, " HTTP/1.1\r\n");
strcat(text, "Host: 127.0.0.1\r\n");
strcat(text, "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)\r\n");
strcat(text, "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
strcat(text, "Accept-Language: zh-cn,zh;q=0.5\r\n");
strcat(text, "Accept-Encoding: gzip,deflate\r\n");
strcat(text, "Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7\r\n");
strcat(text, "Keep-Alive: 300\r\n");
strcat(text, "Connection: keep-alive\r\n");
strcat(text, "Content-Type: application/x-www-form-urlencoded\r\n");
char strlength[200];
sprintf(strlength,"Content-Length: %d\r\n",strlen(json));
strcat(text, strlength);
strcat(text, "\r\n");
strcat(text, json);
char *request = text;
wVersionRequested = MAKEWORD( 2, 2 );
if (WSAStartup(wVersionRequested , &wsaData))
{
printf("Winsock Initialization failed.\n");
return;
}
if ((LocalSock=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)
{
printf("Can not create socket.\n");
return;
}
if ((hostinfo=gethostbyname(pHostName))!=NULL)
{
memcpy(IpAddr,inet_ntoa(*(struct in_addr *)*hostinfo-> h_addr_list),sizeof(IpAddr));
}
else
{
WSACleanup();
return;
}
server.sin_family = AF_INET;
server.sin_port = htons(8080);
server.sin_addr.s_addr= inet_addr(IpAddr);
if (connect(LocalSock,(struct sockaddr*)&server,sizeof(server))==0)
{
int nSendedByte=send(LocalSock,request,strlen(request),0);
printf("the sended data's length:%d bytes\n\n",nSendedByte);
printf("RequestHeader %d :\n%s\n",n,request);
int nRecvBytes=recv(LocalSock,buff,sizeof(buff),0);
printf("the recieved data's length is :%d bytes\n\n",nRecvBytes);
printf("ResponseHeader %d :\n%s\n",n,buff);
for(int i = 21; i >=0 ; i--)
{
returnString[i] = buff[nRecvBytes-(21-i)];
}
}
closesocket(LocalSock);
WSACleanup();
}
int main()
{
printf("start:\n");
char json[512];
char returnString[22] = {0};
int waitTime = 5000;
genJsonString(json);
int n = 1;
while(true)
{
if(strcmp(returnString,SUCCESS) == 0)
{
waitTime = 60000;
}else
{
waitTime = 5000;
}
sendPost(json,n++,returnString);
memset(returnString, 0, sizeof(char) * 22);
Sleep(waitTime);
}
return 0;
}
vs2010设置:
此处改成也不行:
3 个解决方案
#1
其它机器也需要安装.net framework
#2
曾经我遇见过着样的问题 也是如楼上所说
#3
用depends.exe查看exe依赖的dll
#1
其它机器也需要安装.net framework
#2
曾经我遇见过着样的问题 也是如楼上所说
#3
用depends.exe查看exe依赖的dll