CWnd* pEdit=GetDlgItem(IDC_IP);
CString str;
pEdit->GetWindowText(str);
char cmdLine[MAX_PATH];
wsprintf(cmdLine,"ping.exe %s",str);
SECURITY_ATTRIBUTES sa={sizeof(sa),NULL,TRUE};
SECURITY_ATTRIBUTES *psa=NULL;
DWORD dwShareMode=FILE_SHARE_READ|FILE_SHARE_WRITE;
OSVERSIONINFO osVersion={0};
osVersion.dwOSVersionInfoSize=sizeof(osVersion);
if(GetVersionEx(&osVersion)){
if(osVersion.dwPlatformId==VER_PLATFORM_WIN32_NT){
psa=&sa;
dwShareMode|=FILE_SHARE_DELETE;
}
}
HANDLE hConsoleRedirect=CreateFile(
"D:\\NetStatus.txt",
GENERIC_WRITE,
dwShareMode,
psa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
ASSERT(hConsoleRedirect!=INVALID_HANDLE_VALUE);
STARTUPINFO s={sizeof(s)};
s.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
s.hStdOutput=hConsoleRedirect;//输出重定向
s.wShowWindow=SW_HIDE;//隐藏窗口
s.dwFlags=STARTF_USESTDHANDLES;
s.hStdOutput=hConsoleRedirect;
PROCESS_INFORMATION pi={0};
if(CreateProcess(NULL,cmdLine,NULL,NULL,TRUE,NULL,NULL,NULL,&s,&pi))
{
WaitForSingleObject(pi.hProcess,INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
CloseHandle(hConsoleRedirect);
CFile myFile("D:\\NetStatus.txt",CFile::modeRead);
ASSERT(myFile.m_hFile!=NULL);
char *pszNetStatus=new char[myFile.GetLength()+1];
ZeroMemory(pszNetStatus,myFile.GetLength()+1);
myFile.Read(pszNetStatus,myFile.GetLength()+1);
myFile.Close();
DeleteFile("D:\\NetStatus.txt");
GetDlgItem(IDC_STATUS)->SetWindowText(pszNetStatus);
delete pszNetStatus;