Creating Processes-创建进程

来源:互联网 发布:php应用开发是干嘛的 编辑:程序博客网 时间:2024/06/06 01:34

The CreateProcess function creates a new process, which runs independently of the creating process. However, for simplicity, the relationship is referred to as a parent-child relationship.

此函数创建一个新的线程,新的线程是独立运行的. 简单的讲,他们的关系是主进程与子进程的关系.

可执行代码

#include <windows.h>#include <stdio.h>#include <tchar.h>void _tmain(int argc, TCHAR *argv[]){STARTUPINFO si;PROCESS_INFORMATION pi;ZeroMemory(&si, sizeof(si));si.cb = sizeof(si);ZeroMemory(&pi, sizeof(pi));if (argc != 2){printf("Usage: %s [cmdline]\n", argv[0]);// 如 CreatProcesses  aaa.exe // 程序将会启动并切换到 aaa.exe ,等aaa.exe执行. return;}// Start the child process. if (!CreateProcess(NULL,   // No module name (use command line)argv[1],        // Command lineNULL,           // Process handle not inheritableNULL,           // Thread handle not inheritableFALSE,          // Set handle inheritance to FALSE0,              // No creation flagsNULL,           // Use parent's environment blockNULL,           // Use parent's starting directory &si,            // Pointer to STARTUPINFO structure&pi)           // Pointer to PROCESS_INFORMATION structure){printf("CreateProcess failed (%d).\n", GetLastError());return;}// Wait until child process exits.WaitForSingleObject(pi.hProcess, INFINITE);// Close process and thread handles. CloseHandle(pi.hProcess);CloseHandle(pi.hThread);}

If CreateProcess succeeds, it returns a PROCESS_INFORMATION structure containing handles and identifiers for the new process and its primary thread. The thread and process handles are created with full access rights, although access can be restricted if you specify security descriptors. When you no longer need these handles, close them by using the CloseHandle function.

如果进程创建成果,则会返回一个PROCESS_INFORMATION 结构的数据, 些数据包含了新进程和句柄及标识信息,当然也包含其主线程. 当前进程可以使用创建的新的进程及其线程,如果声明了安全规则另有限制. 当句柄使用完后可以使用CloseHandle函数关闭它.

You can also create a process using the CreateProcessAsUser or CreateProcessWithLogonW function. This allows you to specify the security context of the user account in which the process will execute.

其他的两个函数也可以用来创建线程(CreateProcessAsUser 及 CreateProcessWithLogonW). 它们可以让你指定它运行在的用户类别的安全上下文信息.


-------------------

翻译暂存,后续修改.

原创粉丝点击