我以前看到过一个程序,如果你试图停止它,它会立即关闭windows。
现在我急需这个功能,有没有高手能告诉我怎么实现?
看到这里有些同志可能认为很简单,其实不是,
在退出的时候关闭windows并不难,无非就是调个关机函数,
这个程序最绝的是能在用vc里的Process Viewer工具杀掉该进程
时它也能立即知道并关闭windows。这一点我研究了很久都不知道
如何实现???一个程序怎么能知道自己的进程什么时候被杀掉呢???
等。
create a second process to moniter your first instance,
CApp firstInst:
check its command line, if no argument, it is the first process, create CApp secondInst; pass firstInst.ProcessID to secondInst.
the secondInst waitfor the event of unexpected termination of firstInst.
DWORD CALLBACK CApp::ListenThreadProc(void * para)
{
HANDLE hProcessToListen = (HANDLE)para;
for(;;)
{
WaitForSingleObject(hProcessToListen,INFINITE);
if(gs_CloseApp)
{
g_Log.Debug("%s:%d ListenThreadProc() stop",__FILE__,__LINE__);
SetEvent(m_hEvevtCanPopup);
return 0;
}
hProcessToListen = CreateBuddy(theApp.IsMonitorProcess());
SetEvent(m_hEvevtCanPopup);
}
return 0;
}
HANDLE CApp::CreateBuddy(bool bmonitor)
{
// if bmonitor =true, its budy is main app
// otherwise its budy is monitor only
STARTUPINFO StartInfo;
PROCESS_INFORMATION pinfo;
char filename[MAX_PATH];
char para[MAX_PATH]={0};
GetModuleFileName(NULL,filename,MAX_PATH);
if(bmonitor)
sprintf(para,"%X",GetCurrentProcessId());
else
sprintf(para,"%X %s",GetCurrentProcessId(),monitor_server_param);
// g_Log.Debug("%s:%d parameter is %s",__FILE__,__LINE__,para);
memset(&StartInfo,0,sizeof(STARTUPINFO));
StartInfo.cb = sizeof(STARTUPINFO);
if( ! CreateProcess(
filename,
para,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&StartInfo,
&pinfo) )//创建进程失败
return NULL;
return pinfo.hProcess;
}
the above code is not exactly meet your requirement, just a referrence.
up
我怀疑还有别的进程或线程在监视这个程序。
晕
那我先关 掉你的监视进程
再关主进程.............
难道互相监视???
up
mark
study
建几个进程,让他们循环的监视
up