码迷,mamicode.com
首页 > Windows程序 > 详细

windows系统调用 进程终止

时间:2014-05-17 18:50:15      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

bubuko.com,布布扣
 1 #include "windows.h"
 2 #include "iostream"
 3 #include "stdio.h"
 4 using namespace std;
 5 
 6 static LPCTSTR q_szMutexName="w2kdg.ProcTerm.mutex.Suicide";
 7 
 8 HANDLE StartClone(){
 9     TCHAR szFilename[MAX_PATH];
10     GetModuleFileName(NULL,szFilename,MAX_PATH);
11 
12     TCHAR szCmdLine[MAX_PATH];
13     sprintf_s(szCmdLine,"\"%s\" \"child\"",szFilename);14 
15     STARTUPINFO si;
16 
17     ZeroMemory(reinterpret_cast<void*>(&si),sizeof(si));
18     si.cb=sizeof(si);
19     PROCESS_INFORMATION pi;
20 
21     BOOL bCreateOK=CreateProcess(
22         szFilename,
23         szCmdLine,
24         NULL,
25         NULL,
26         FALSE,
27         CREATE_NEW_CONSOLE,
28         NULL,
29         NULL,
30         &si,
31         &pi
32         );
33 
34     if(bCreateOK){
35         CloseHandle(pi.hProcess);
36         CloseHandle(pi.hThread);
37         return pi.hProcess;
38     }
39 
40     else return INVALID_HANDLE_VALUE;
41 }
42 
43 void Parent(){
44     cout<<"Creating the child process and waited child process to quit."<<endl;
45     HANDLE hchild=StartClone();
46     if(hchild!=INVALID_HANDLE_VALUE)
47     {
48         WaitForSingleObject(hchild,INFINITE);
49         cout<<"The child process had quited."<<endl;
50 
51     }
52 
53     else 
54         cout<<"Create child process failed."<<endl;
55 }
56 
57 
58 void Child(){
59     cout<<"Child id quiting"<<endl;
60     Sleep(5000);
61 
62 }
63 
64 int main(int argc,char * argv[]){
65     if(argc>1&&strcmp(argv[1],"child")==0){
66         Child();
67     }
68     else{
69         Parent();
70     }
71     return 0;
72 
73 }
bubuko.com,布布扣

 

windows系统调用 进程终止,布布扣,bubuko.com

windows系统调用 进程终止

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/593213556wuyubao/p/3733456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!