创建与关闭记事本进程
介绍:创建一个记事本进程,等待用户输入随意字符回车后,关闭记事本进程。由于对两个方法进行了归函,故取消了返回值。
1 #include <stdio.h> 2 #include <Windows.h> 3 void createProcess() 4 { 5 PROCESS_INFORMATION pi={0}; 6 STARTUPINFO si={0}; 7 8 si.cb=sizeof(STARTUPINFO); 9 BOOL bRet=CreateProcess("c:\\windows\\system32\\notepad.exe", 10 NULL,NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi); 11 if(bRet==FALSE) 12 { 13 printf("CreateProcess Error~\r\n"); 14 // return -1; 15 } 16 CloseHandle(pi.hThread); 17 CloseHandle(pi.hProcess); 18 } 19 void closeNotepadProcess() 20 { 21 HWND hNoteWnd=FindWindow(NULL,"无标题 - 记事本"); 22 //if(hNoteWnd==NULL){return -1;} 23 DWORD dwNotePid=0; 24 GetWindowThreadProcessId(hNoteWnd,&dwNotePid); 25 //if(dwNotePid==0)return -1; 26 HANDLE hNoteHandle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwNotePid); 27 //if(hNoteHandle==NULL)return -1; 28 TerminateProcess(hNoteHandle,0); 29 CloseHandle(hNoteHandle); 30 31 } 32 int main() 33 { 34 35 createProcess(); 36 getchar(); 37 closeNotepadProcess(); 38 39 return 0; 40 }