18,356
社区成员
发帖
与我相关
我的任务
分享
unsigned long SomeThread( LPVOID pVoid )
{
char Buffer [ 4096 ];
DWORD dwExit = 1;
HANDLE hReadPipe = NULL;
HANDLE hWritePipe = NULL;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
CString szCurPath = pDlg->GetFilePath();
STARTUPINFO StartupInfo = { sizeof(StartupInfo) };
PROCESS_INFORMATION ProcessInfo = { 0 };
TCHAR szCommandLine[ 256 ]; // = _T("c:\\windows\\system32\\cmd.exe /c dir");
_tcscpy_s( szCommandLine, 256, ( szCurPath + _T("VPNRel.EXE")));
ZeroMemory( Buffer, 4096 );
if( ! CreatePipe( &hReadPipe, &hWritePipe, &sa, 0 ))
{
goto EXIT_THREAD;
}
StartupInfo.cb = sizeof( STARTUPINFO );
GetStartupInfo( &StartupInfo );
StartupInfo.hStdError = hWritePipe;
StartupInfo.hStdOutput = hWritePipe;
StartupInfo.wShowWindow = SW_HIDE;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if( ! CreateProcess( NULL, szCommandLine, NULL, NULL, TRUE, 0, NULL, szCurPath, &StartupInfo, &ProcessInfo ))
{
goto EXIT_THREAD;
}
// - 创建目标进程成功,开始等待目标进程结束并监视其输出
// ----------------------------目标进程输出-------------------------------
CloseHandle( hWritePipe );
hWritePipe = NULL;
while( dwExit )
{
DWORD dwWait = WaitForSingleObject( ProcessInfo.hProcess, 200 );
DWORD dwReaded;
ZeroMemory( Buffer, sizeof( Buffer )); // Cleaer Buffer.
if( ReadFile( hReadPipe, Buffer, 4095, &dwReaded, NULL ) == false )
{
if( dwWait == WAIT_TIMEOUT )
continue;
else
break;
}
else
{
#ifdef _UNICODE
TCHAR szWBuffer[ 4096 ];
ZeroMemory( szWBuffer, sizeof( TCHAR ) * 4096 );
MultiByteToWideChar( CP_ACP, 0, Buffer, -1, szWBuffer, 4095 );
APPEND( szWBuffer ); // == m_strSomeEdit += szWBuffer; UpdateData( false );
#else
APPEND( Buffer ); // == m_strSomeEdit += Buffer; UpdateData( false );
#endif
}
if( dwWait == WAIT_OBJECT_0 )
break;
}
//--------------------------目标进程已经结束------------------------------
EXIT_THREAD:
if( ProcessInfo.hProcess != NULL )
CloseHandle( ProcessInfo.hProcess );
if( hWritePipe != NULL )
CloseHandle( hWritePipe );
if( hReadPipe != NULL )
CloseHandle( hReadPipe );
pDlg->m_hThread = NULL;
return 0;
}
unsigned long SomeThread( LPVOID pVoid )
{
char Buffer [ 4096 ];
DWORD dwExit = 1;
HANDLE hReadPipe = NULL;
HANDLE hWritePipe = NULL;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
CString szCurPath = pDlg->GetFilePath();
STARTUPINFO StartupInfo = { sizeof(StartupInfo) };
PROCESS_INFORMATION ProcessInfo = { 0 };
TCHAR szCommandLine[ 256 ]; // = _T("c:\\windows\\system32\\cmd.exe /c dir");
_tcscpy_s( szCommandLine, 256, ( szCurPath + _T("VPNRel.EXE")));
ZeroMemory( Buffer, 4096 );
if( ! CreatePipe( &hReadPipe, &hWritePipe, &sa, 0 ))
{
goto EXIT_THREAD;
}
StartupInfo.cb = sizeof( STARTUPINFO );
GetStartupInfo( &StartupInfo );
StartupInfo.hStdError = hWritePipe;
StartupInfo.hStdOutput = hWritePipe;
StartupInfo.wShowWindow = SW_HIDE;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if( ! CreateProcess( NULL, szCommandLine, NULL, NULL, TRUE, 0, NULL, szCurPath, &StartupInfo, &ProcessInfo ))
{
goto EXIT_THREAD;
}
// - 创建目标进程成功,开始等待目标进程结束并监视其输出
// ----------------------------目标进程输出-------------------------------
CloseHandle( hWritePipe );
hWritePipe = NULL;
while( dwExit )
{
DWORD dwWait = WaitForSingleObject( ProcessInfo.hProcess, 200 );
DWORD dwReaded;
ZeroMemory( Buffer, sizeof( Buffer )); // Cleaer Buffer.
if( ReadFile( hReadPipe, Buffer, 4095, &dwReaded, NULL ) == false )
{
if( dwWait == WAIT_TIMEOUT )
continue;
else
break;
}
else
{
#ifdef _UNICODE
TCHAR szWBuffer[ 4096 ];
ZeroMemory( szWBuffer, sizeof( TCHAR ) * 4096 );
MultiByteToWideChar( CP_ACP, 0, Buffer, -1, szWBuffer, 4095 );
APPEND( szWBuffer ); // == m_strSomeEdit += szWBuffer; UpdateData( false );
#else
APPEND( Buffer ); // == m_strSomeEdit += Buffer; UpdateData( false );
#endif
}
if( dwWait == WAIT_OBJECT_0 )
break;
}
//--------------------------目标进程已经结束------------------------------
EXIT_THREAD:
if( ProcessInfo.hProcess != NULL )
CloseHandle( ProcessInfo.hProcess );
if( hWritePipe != NULL )
CloseHandle( hWritePipe );
if( hReadPipe != NULL )
CloseHandle( hReadPipe );
pDlg->m_hThread = NULL;
return 0;
}
[/quote]
你的正解,虽然我找到方法了,还是很感谢各位回帖的大神#include <windows.h>
#include <stdio.h>
#include <string.h>
char YN(int k) {
FILE *f;
char fn[40];
char ln[80];
char yn;
int n;
yn='N';
sprintf(fn,"d:\\ping%d.txt",k);
f=fopen(fn,"r");
if (NULL!=f) {
n=0;
while (1) {
if (NULL==fgets(ln,80,f)) break;//
if (strstr(ln,"ms ")) {
yn='Y';
break;//
}
n++;
if (n>=4) break;//
}
fclose(f);
}
return yn;
}
void main(int argc,char **argv) {
char cmdstr[256];
int i;
int IP[3];
char c;
if (argc<2) {
USAGE:
printf("Usage example:\n %s 192.168.60.\nto test 192.168.60.1-254\n",argv[0]);
return;
}
if (4==sscanf(argv[1],"%d.%d.%d%c",&IP[0],&IP[1],&IP[2],&c)) {
if (0<=IP[0] && IP[0]<=255
&& 0<=IP[1] && IP[1]<=255
&& 0<=IP[2] && IP[2]<=255
&& '.'==c) {
for (i=1;i<255;i++) {
sprintf(cmdstr,"cmd /c ping %s%d -n 1 -w 1000 >d:\\ping%d.txt",argv[1],i,i);
WinExec(cmdstr,SW_HIDE);
}
Sleep(3000);
for (i=1;i<255;i++) {
printf("%c %s%d\n",YN(i),argv[1],i);
}
Sleep(3000);
WinExec("cmd /c del /q d:\\ping*.txt",SW_HIDE);
} else goto USAGE;
} else goto USAGE;
}