LPPROCESS_INFORMATION

本文详细介绍了Windows操作系统中PROCESS_INFORMATION结构的各个成员变量及其用途。此结构由CreateProcess函数填充,用于返回新创建进程及其主线程的相关句柄和标识符。
http://msdn.microsoft.com/en-us/library/ms886775.aspx

PROCESS_INFORMATION


This structure is filled in by the CreateProcess function with information about a newly created process and its primary thread.

typedef struct _PROCESS_INFORMATION { 
HANDLE hProcess;
HANDLE hThread;
DWORD dwProcessId;
DWORD dwThreadId;
} PROCESS_INFORMATION;
Members
hProcess
Returns a handle to the newly created process.

The handle is used to specify the process in all functions that perform operations on the process object.

hThread
Returns a handle to the primary thread of the newly created process.

The handle is used to specify the thread in all functions that perform operations on the thread object.

dwProcessId
Returns a global process identifier that can be used to identify a process.

The value is valid from the time the process is created until the time the process is terminated.

dwThreadId
Returns a global thread identifier that can be used to identify a thread.

The value is valid from the time the thread is created until the time the thread is terminated.

Requirements

OS Versions: Windows CE 1.0 and later.
Header: Winbase.h.

HANDLE handle_read; HANDLE handle_write; bool ret = CreatePipe(&handle_read,&handle_write,&saAttr,0); if (!ret) { cout << "创建进程失败:create pipe fail" << endl; } cout << "设置句柄不可以被子进程继承,不设置也不影响。"; if (!SetHandleInformation(handle_write, HANDLE_FLAG_INHERIT, 0)) { cout << "设置句柄失败:set handle fail!" << endl; } cout << "第一步:子进程、设置管道句柄的继承" << endl; { char cmdline[] = "childprocess.exe"; //PROCESS_INFORMATION 是一个用于接收由 CreateProcess 或 CreateProcessAsUser //等函数创建的新进程信息的重要结构体。它包含两个关键句柄(进程句柄和主线程句柄)以及它们对应的标识符 // typedef struct _PROCESS_INFORMATION { //HANDLE hProcess; //HANDLE hThread; //DWORD dwProcessId; //DWORD dwThreadId; //PROCESS_INFORMATION, * PPROCESS_INFORMATION, * LPPROCESS_INFORMATION; PROCESS_INFORMATION piProInfo; //由于 PROCESS_INFORMATION 结构体中的字段可能包含未定义或无效的数据,因此在调用 CreateProcess 之前必须将其清零, //以确保函数能够正确地写入新数据。ZeroMemory 是一种常用的内存初始化方式,它会将指定内存区域的所有字节设置为 0 ZeroMemory(&piProInfo, sizeof(piProInfo)); //STARTUPINFO 是一个用于指定新创建进程的窗口外观和标准句柄配置的结构体 STARTUPINFO si; ZeroMemory(&si, sizeof(si)); //必须手动设置 cb 成员为 sizeof(STARTUPINFO),以告知系统当前结构的大小 si.cb = sizeof(STARTUPINFO); si.hStdInput = handle_read;//把管道的读取句柄给子进程 //通过 wShowWindow 和 dwFlags 可以控制新进程主窗口的显示方式 其中 dwFlags 指定了哪些字段是有效的,而 wShowWindow 则指定了窗口的初始状态 si.dwFlags = STARTF_USESTDHANDLES; ret = CreateProcess( //调用失败,返回0;调用成功返回非0; NULL, //一般都是空;(另一种批处理情况:此参数指定"cmd.exe",下一个命令行参数 "/c otherBatFile") cmdline, //命令行参数 NULL, //_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, NULL, //_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, FALSE, //_In_ BOOL bInheritHandles, CREATE_NEW_CONSOLE, //新的进程使用新的窗口。 NULL, //_In_opt_ LPVOID lpEnvironment, NULL, //_In_opt_ LPCTSTR lpCurrentDirectory, &si, //_In_ LPSTARTUPINFO lpStartupInfo, &piProInfo); //_Out_ LPPROCESS_INFORMATION lpProcessInformation if (!ret) { cout << "creat fail"<<endl; } else { cout << "创建子进程成功" << endl; CloseHandle(piProInfo.hProcess); CloseHandle(piProInfo.hThread); CloseHandle(handle_read);为什么只要CloseHandle(handle_read)不要CloseHandle(handle_write)
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值