这是一个使用ping命令的例子 输入参数为目的IP地址
bool checknetwork(char* szCmdLine)
{
bool isconn = false;
PROCESS_INFORMATION pi;
//保存原来路径
DWORD dwLen = MAX_PATH;
TCHAR szStdOut[MAX_PATH] = _T("\0");
BOOL fRet = GetStdioPathW(1, szStdOut, &dwLen);
char cmd[256] = "\0";
sprintf(cmd,"%s %s","/c ping",szCmdLine);
TCHAR execmd[256] = _T("\0");
mbstowcs(execmd,cmd,strlen(cmd));
//执行命令
SetStdioPathW(1, TEXT("\\PocketMory1\\tempout.txt"));
CreateProcess (TEXT("\\Windows\\cmd.exe"), execmd, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi);
WaitForSingleObject (pi.hProcess, 100);
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
//再设置回原来路径
SetStdioPathW(1, szStdOut);
HANDLE hand = CreateFile(TEXT("\\PocketMory1\\tempout.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, 0);
char buffer[512] = "\0";
DWORD actlen;
if (ReadFile(hand,buffer,512,&actlen,NULL) && (actlen > 0))
{
if (strstr(buffer,"Reply from"))
{
isconn = true;
}
else
{
isconn = false;
}
}
CloseHandle(hand);
DeleteFile(TEXT("\\PocketMory1\\tempout.txt"));
return isconn;
}