程序内部调用ping一下就OK,给个代码给你看
char szFetCmd[] = "ping www.baidu.com";
//这里为命令
//初始化返回MAC地址缓冲区
SECURITY_ATTRIBUTES sa;
HANDLE hReadPipe,hWritePipe;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
//创建管道
BOOL bret = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
if(!bret)
{
//创建失败了,,,
}
//控制命令行窗口信息
STARTUPINFO si;
//返回进程信息
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWritePipe;
si.hStdOutput = hWritePipe;
si.wShowWindow = SW_HIDE; //隐藏命令行窗口
si.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
//创建获取命令行进程
bret = CreateProcess(NULL,szFetCmd,NULL,NULL,TRUE,0,NULL,
NULL,&si,&pi);
char szBuffer[MAX_COMMAND_SIZE+1]; //放置命令行输出缓冲区
if (bret)
{
WaitForSingleObject (pi.hProcess, INFINITE);
unsigned long count;
CloseHandle(hWritePipe);
memset(szBuffer, 0x00, sizeof(szBuffer));
//读取ping返回的信息到szBuffer中
bret
=
ReadFile(hReadPipe,szBuffer,MAX_COMMAND_SIZE,&count,0);
if(!bret)
{
//失败了
//关闭所有的句柄
CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return "";
}
else
{
//成功了,szbuffer中就是ping返回的数据,查看一下返回数据就知道
//有没有连接网络了,当然还有更多方法,只要更换命令行就OK
//我这里用的是ping百度
}
//关闭所有的句柄
CloseHandle(hWritePipe);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hReadPipe);