15,472
社区成员
发帖
与我相关
我的任务
分享
TCHAR cmdline[] = _T("ping 127.0.0.1 -t");
BOOL b = CreateProcess(NULL,cmdline,NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi);
if (!b)
{
printf("CreateProcess failed!\n");
return -1;
}
SetConsoleCtrlHandler(CtrlHandler,TRUE);
TCHAR cmdline[] = _T("ping 127.0.0.1 -t");
BOOL b = CreateProcess(NULL,cmdline,NULL,NULL,FALSE,CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi);
if (!b)
{
printf("CreateProcess failed!\n");
return -1;
}
SetConsoleCtrlHandler(CtrlHandler,TRUE);
//中间略去一些无关紧要的代码
GenerateConsoleCtrlEvent(CTRL_C_EVENT,pi.dwProcessId);
#include "stdafx.h"
#include <Windows.h>
#include <process.h>
PROCESS_INFORMATION pi;
BOOL WINAPI CtrlHandler(DWORD dwCtrlType)
{
switch (dwCtrlType)
{
case CTRL_C_EVENT:
printf("Pid=%d",_getpid());
return TRUE;
case CTRL_BREAK_EVENT:
printf("server Break Event\n");
printf("%s",GenerateConsoleCtrlEvent(CTRL_C_EVENT,pi.dwProcessId)?"true\n":"false\n");
return TRUE;
default:
return FALSE;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
ZeroMemory(&pi,sizeof(pi));
si.cb = sizeof(si);
TCHAR cmdline[] = _T("ping 127.0.0.1 -t");
BOOL b = CreateProcess(NULL,cmdline,NULL,NULL,FALSE,CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi);
if (!b)
{
printf("CreateProcess failed!\n");
return -1;
}
SetConsoleCtrlHandler(CtrlHandler,TRUE);
int i = 0;
while(1)
{
scanf("%d",&i);
if (i==1)
{
ZeroMemory(&pi,sizeof(pi));
b = CreateProcess(NULL,cmdline,NULL,NULL,FALSE,NULL/*CREATE_NEW_PROCESS_GROUP*/,NULL,NULL,&si,&pi);
}
if (i>1)
{
//GenerateConsoleCtrlEvent(CTRL_C_EVENT,pi.dwProcessId);
printf("%s",GenerateConsoleCtrlEvent(CTRL_C_EVENT,i)?"true\n":"false\n");
}
if(WaitForSingleObject(pi.hProcess,200)==WAIT_OBJECT_0)
printf("Ping exited!");
// break;
}
return 0;
}