Windows 创建进程 — 详解API函数CreateProcess()
-
【关于操作系统进程和线程的编程实例】在windows NT 环境 下创建进程的和子进程的方法可以调用windows 的 API 函数 CreateProcess . 实际上即是 在一个 CreateProcessTest.exe 的程序中使用了 CreateProcess() 函数调用了另外一个可执行模块,而这个可执行模块即是CreateProcessTest.exe 的子进程 。 用很简单明了的话来说就是 在一个 可执行文件中调用了 另外一个可执行文件作为子进程(初步理解,有待深入,望高人能指点指点)。通过在下下面的细细到来,您如果未曾涉猎这个领域的话,应该能很快的学会如何在windows中创建进程,以及线程句柄的控制。以及获取进程和线程的相关信息。Thx.
下面首先来具体说一下这个API 函数:- //<strong> Windows CreateProcess 函数原型 </strong>
- ***********************************
- BOOL WINAPI CreateProcess(
- __in LPCTSTR lpApplicationName,
- [指定可执行模块的字符串]
- __in_out LPTSTR lpCommandLine,
- [从命令行读入执行模块]
- __in LPSECURITY_ATTRIBUTES lpProcessAttributes,
- [决定所返回的句柄是否可以被子进程继承]
- __in LPSECURITY_ATTRIBUTES lpThreadAttributes,
- [决定所返回的句柄是否可以被子线程继承]
- __in BOOL bInheritHandles,
- [如果这个参数为TRUE,这所有句柄会被子进程继承]
- __in DWORD dwCreationFlags,
- [控制优先级和创建过程]
- __in LPVOID lpEnvironment,
- [指向一个新进程的环境块。如果此参数为空,新进程使用调用进程的环境。]
- __in LPCTSTR lpCurrentDirectory,
- [子进程的工作路径]
- __in LPSTARTUPINFO lpStartupInfo,
- [决定新进程的主窗体如何显示]
- __out LPPROCESS_INFORMATION lpProcessInformation
- [接收新进程的识别信息]
- );
- ************************************
//适用平台:
Client :Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server :Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server.
//需求 :
Header : Declared in Winbase.h; include Windows.h.Library : Use Kernel32.lib.
DLL : Requires Kernel32.dll.
Unicode : Implemented as CreateProcessW (Unicode) and CreateProcessA (ANSI).
在调用该函数的时候可以传入两种可执行模块,一个是直接在函数参数中传递,通过LPCTSTR lpApplicationName,参数 。另一种是通过MS-DOS命令行,通过LPTSTR lpCommandLine, 实现 。
在接下来的实例中是这样的:
if( !CreateProcess( NULL,
argv[1], //在这里读入控制台参数,创建进程
…..) ; //为了简洁,只写了第一二个参数 。
接下来是源码,注释还算详细,细细看看,应该就明白了:- // created by daniel sun @ 2008.11.09 wellcome to my homepage www.Allove.org
- #include "stdafx.h"
- #include <windows .h>
- #include <stdio .h>
- #include <tchar .h>
- #include <iostream>
- void _tmain( int argc, TCHAR *argv[] )
- {
- STARTUPINFO si; //Specifies the window station,
- //desktop, standard handles, and appearance of
- //the main window for a process at creation time.
- PROCESS_INFORMATION pi; //进程信息,原始结构体如下:
- /*
- typedef struct _PROCESS_INFORMATION
- { HANDLE hProcess;
- HANDLE hThread;
- DWORD dwProcessId;
- DWORD dwThreadId;
- } PROCESS_INFORMATION, *LPPROCESS_INFORMATION;
- */
- //Fills a block of memory with zeros. 初始化内存块,全部填充值
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
- if( argc != 2 ) //控制台读入两个字符串,一个是本程序的名字,另一个是要建立子的进程
- {
- printf("用法: %s [cmdline]\n", argv[0]); //提示用户如何传入参数
- printf("运行方式不正确!系统退出..\n"); //用于测试
- system("pause"); //用于测试
- return;
- }
- // 创建子进程
- if( !CreateProcess( NULL, // No module name (use command line)
- argv[1], // Command line //TEXT("D:\\MyDocuments\\Visual Studio 2005\\project\\Stack_TCC\\debug\\Stack_TCC.exe"),
- NULL, // Process handle not inheritable
- NULL, // Thread handle not inheritable
- FALSE, // Set handle inheritance to FALSE
- 0, // No creation flags
- NULL, // Use parent's environment block
- NULL, // Use parent's starting directory
- &si, // Pointer to STARTUPINFO structure
- &pi ) // Pointer to PROCESS_INFORMATION structure
- )
- {
- printf("创建进程失败(%d)\n", GetLastError() );
- //printf("创建进程失败..");
- system("pause"); //用于测试
- return;
- }
- //打印进程信息
- printf("一下是子进程的信息:\n");
- printf("进程ID pi.dwProcessID: %d \n",pi.dwProcessId);
- printf("线程ID pi.dwThreadId: %d \n",pi.dwThreadId);
- //printf("句柄进程pi.hProcess: %s \n",pi.hProcess);
- //printf("句柄线程pi.hThread: %s \n",pi.hThread);
- // 等待知道子进程退出...
- WaitForSingleObject( pi.hProcess, INFINITE );
- //WaitForSingleObject()函数检查对象的状态,如果是未确定的则等待至超时
- /*
- //WaitForSingleObject()函数原型:
- DWORD WINAPI WaitForSingleObject(
- __in HANDLE hHandle,
- __in DWORD dwMilliseconds //超时时限 INFINITE 为无穷
- );
- */
- //子进程退出
- printf("子进程已经退出... \n");
- //关闭进程和句柄
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- system("pause");
- exit(0);
- }
- </iostream></tchar></stdio></windows>
编译上面这段代码应该能得到一个二进制文件,假设你将它命名为 :CreateProcessTest.exe
使用方法是这样的:
在命令行中执行 CreateProcessTest.exe yourProgramName.exe
【程序运行之后可以在任务管理器里面查看进程】
【为了不引起不必要的麻烦建议yourProgramName.exe使用一个win32控制台程序(你可以简单的写一个HelloWord.exe来测试),这里所谓的不必要的麻烦是程序中传的参数决定的,今天暂时不讲】就到这里吧!小结一下子 : 详细介绍了windows API 函数 CreateProcess() 各个参数及其意义与用法,通过一个实例来阐释了如何在windows系统中创建进程,和获取进程信息,以及相关的句柄。下面呢?没了。
-
我们的说明!
欢迎转载,但请您以链接形式注明本文出处和本站原文链接,下面是链接形式,谢谢合作!
出处链接:Allove of Paradise
原文链接:http://blog.allove.org/archives/windows-api-createprocess.html
随机日志




十一月 9th, 2008 at 12:51 下午
related这块挺酷的~~
PS:貌似没有上下篇的链接~
[回复]
Daniel Reply:
十一月 9th, 2008 at 3:33 下午
呵呵,我还没注意 现在有了,还有什么细节的地方一定告诉我哟。Thx
[回复]
十一月 9th, 2008 at 4:39 下午
学习了~
[回复]
十一月 9th, 2008 at 6:01 下午
这个太高深了,我不懂……
[回复]
十一月 9th, 2008 at 8:35 下午
嘿嘿,貌似以前用过一点,好久不用了,现在都忘记了,,,,,
[回复]
十一月 9th, 2008 at 8:36 下午
子曰:很深奥啊
[回复]
十一月 10th, 2008 at 8:46 上午
主题太赞了 丫丫
[回复]
十一月 10th, 2008 at 9:53 下午
厄,好像都挺好了吧,除了一些细节的配色
PS:顶部的分类会不会占用太大了,一行三四个?
[回复]
十一月 11th, 2008 at 11:19 上午
这个我就不关注了
皮是很好看 但总觉得细节哪里还抠的不够…继续精进吧…
[回复]