标签:系统进程 mic 相关 format close cgi char 通过 nat
操作系统是计算机相关专业的一门极其重要的专业课,但实验课的时候很多人不知道代码应该怎么写,这里贴出博主使用过的一部分,亲测可行。
更新时间:2017-10-26
内容及要求:
① 掌握Windows进程的创建和销毁API的调用方法;编程代码,在程序中创建和销毁一个Word进程;
② 能够挂起和激活被创建进程的主线程;
③ 通过Windows进程管理器查看系统进程列表的变化。
实验指导:
①创建进程的API
BOOL CreateProcess(
LPCTSTR lpApplicationName,
LPTSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);
例程:
1 void main( VOID ){ 2 STARTUPINFO si; 3 PROCESS_INFORMATION pi; 4 ZeroMemory( &si, sizeof(si) ); 5 si.cb = sizeof(si); 6 ZeroMemory( &pi, sizeof(pi) ); 7 // Start the child process. 8 if( !CreateProcess( NULL, // No module name (use command line). 9 "MyChildProcess", // Command line. 10 NULL, // Process handle not inheritable. 11 NULL, // Thread handle not inheritable. 12 FALSE, // Set handle inheritance to FALSE. 13 0, // No creation flags. 14 NULL, // Use parent‘s environment block. 15 NULL, // Use parent‘s starting directory. 16 &si, // Pointer to STARTUPINFO structure. 17 &pi ) // Pointer to PROCESS_INFORMATION structure. 18 ) { 19 ErrorExit( "CreateProcess failed." ); 20 } 21 // Wait until child process exits. 22 WaitForSingleObject( pi.hProcess, INFINITE ); 23 // Close process and thread handles. 24 CloseHandle( pi.hProcess ); 25 CloseHandle( pi.hThread ); 26 }
② 销毁进程API
BOOL TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);
③ 挂起进程的主线程API
DWORD SuspendThread(
HANDLE hThread
);
④激活进程的主线程API
DWORD ResumeThread(
HANDLE hThread
);
⑤进程查看器
源码:
1 #include<iostream> 2 #include<windows.h> 3 using namespace std; 4 void main( VOID ) 5 { 6 STARTUPINFO si; 7 PROCESS_INFORMATION pi; 8 /** 9 * 这里填个人的word可执行文件的路径 10 * ps:"\"需要进行转义 11 **/ 12 TCHAR szCommandLine[]=TEXT("D:\\Microsoft Office\\Office14\\WINWORD.EXE"); 13 ZeroMemory(&si, sizeof(si) ); 14 si.cb= sizeof(si); 15 ZeroMemory(&pi, sizeof(pi) ); 16 if(!CreateProcess( NULL, szCommandLine, NULL,NULL, FALSE,0,NULL, NULL,&si,&pi ) ) 17 { 18 fprintf(stderr,"进程创建失败!"); 19 } 20 int x; 21 while(1) 22 { 23 cout << "请输入要选择的操作:\n1:创建进程\n2:销毁进程\n3:挂起进程\n4:激活进程\n0:退出\n"; 24 cin >> x; 25 switch(x) 26 { 27 case 1: 28 if(CreateProcess( NULL, szCommandLine, NULL,NULL, FALSE,0,NULL, NULL,&si,&pi ) ) 29 fprintf(stderr,"进程创建成功!"); 30 else 31 cout << "进程创建失败!"<<endl; 32 break; 33 case 2: 34 if(TerminateProcess(pi.hProcess,0)) 35 cout<<"销毁进程成功"<<endl; 36 else 37 cout<<"销毁失败!"<<endl; 38 break; 39 case 3: 40 if(SuspendThread(pi.hThread)) 41 cout<<"挂起进程成功"<<endl; 42 else 43 cout<<"挂起失败"<<endl; 44 break; 45 case 4: 46 if(ResumeThread(pi.hThread)) 47 cout<<"激活进程成功"<<endl; 48 else 49 cout<<"激活失败"<<endl; 50 break; 51 case 0: 52 exit(0); 53 default: 54 cout<<"输入有误不正确"<<endl; 55 } 56 } 57 }
持续更新中!!!
作者:耑新新,发布于 博客园
转载请注明出处,欢迎邮件交流:zhuanxinxin@foxmail.com
标签:系统进程 mic 相关 format close cgi char 通过 nat
原文地址:http://www.cnblogs.com/Amedeo/p/7738077.html