gpt4 book ai didi

Can't create a process with CreateProcessW(无法使用"进程“创建进程)

转载 作者:bug小助手 更新时间:2023-10-24 19:07:44 24 4
gpt4 key购买 nike



I need to create a process and get a handle and PID of it.

我需要创建一个进程,并获得它的句柄和ID。


Here is my code:

以下是我的代码:


STARTUPINFOW SI;
PROCESS_INFORMATION PI;

SI.cb = sizeof(SI);

std::wstring Name = L"Dummy";

if (!::CreateProcessW(Name.c_str(), NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI))
{
std::wcout << L"Failed to create a process";
}

HANDLE hDummy = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, PI.dwProcessId);

It outputs "Failed to create a process" in output Window. I can't see a process in Process Explorer and all values of PI are still 0.

它在输出窗口中输出“无法创建进程”。我在Process Explorer中看不到进程,PI的所有值仍然为0。


It also gives me this message: Exception thrown at 0x00007FFA5202C286 (ntdll.dll) in PEX.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

它还给我一条消息:在PEX.exe中的0x00007FFA5202C286(ntdll.dll)处抛出异常:0xC0000005:访问冲突读取位置0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF


更多回答

After a successful call to CreateProcessW, the PROCESS_INFORMATION structure holds a handle to the process object. If you need a PID instead, pass it into GetProcessId. If this doesn't work for you, you need to provide a minimal reproducible example.

在成功调用CreateProcessW之后,PROCESS_INFORMATION结构持有Process对象的句柄。如果您需要一个PID,则将其传递给GetProcessID。如果这对您不起作用,您需要提供一个最小的可重复性示例。

PROCESS_INFORMATION receives both a handle and PID.

PROCESS_INFORMATION同时接收句柄和ID。

"After CreateProcessW(), value of all PI's members is 0" That's because you're passing "Dummy" as the process command and thus the API is failing.

“在CreateProcessW()之后,所有PI的成员的值都是0”,这是因为您将“Dummy”作为进程命令传递,因此API失败。

CreateProcessW does not return a PID. It returns a BOOL indicating whether or not the call succeeded. You must not ignore that.

CreateProcessW不返回ID。它返回一个BOOL,指示调用是否成功。你不能忽视这一点。

Have your tried using GetProcessId? As Community said that it can retrieves the process identifier from PROCESS_INFORMATION structure. You should try it.

您尝试过使用GetProcessID吗?正如社区所说,它可以从Process_Information结构中检索进程标识符。你应该试试。

优秀答案推荐

The problem was solved by making an array of WCHAR, and passing it to the second parameter of CreateProcessW, instead of the first one

通过创建WCHAR数组并将其传递给CreateProcessW的第二个参数而不是第一个参数,解决了该问题



(Edit: Note: This is an answer to the original question, not to the edited one...)

(Edit:注:这是对原始问题的回答,而不是对编辑后的问题的回答.)


PI.hProcess

This is a handle of the process - this is the value that would be returned by OpenProcess() (but the access flags differ from PROCESS_ALL_ACCESS).

这是进程的句柄--这是OpenProcess()返回的值(但访问标志不同于PROCESS_ALL_ACCESS)。


PI.dwProcessId

This is the ID of the process - this is the value that you can pass to OpenProcess().

这是进程的ID--这是您可以传递给OpenProcess()的值。


(... and the value you seem to be looking for.)

(.以及您似乎在寻找的价值。)


PID = CreateProcess(...)

This returns a boolean value: Unequal to FALSE if the process has been created, FALSE if creating the process failed.

这将返回一个布尔值:如果进程已创建,则不等于False;如果创建进程失败,则返回False。


If this value is FALSE, PI.dwProcessId and PI.hProcess contain invalid values.

如果此值为FALSE,则PI.dwProcessID和PI.hProcess包含无效值。


Note that the Windows API uses the (typedef-) type BOOL to store the boolean value (instead of the type bool) and it uses the upper-case constants TRUE and FALSE. Also note that the type BOOL can hold more than just two values and that any value unequal to FALSE means "true". So comparing using == TRUE is not a good idea.

请注意,Windows API使用(tyecif-)类型BOOL来存储布尔值(而不是类型bool),并使用大写常量True和False。还要注意,类型BOOL可以容纳两个以上的值,任何不等于FALSE的值都表示“真”。因此,使用==TRUE进行比较不是一个好主意。


更多回答

The ABI type for a BOOL is an INT, a 32-bit signed integer value. Not a DWORD (an unsigned integer value). Sounds like nitpicking until VARIANT_BOOL enters the scene.

BOOL的ABI类型为int,即32位有符号整数值。不是DWORD(无符号整数值)。在VARIANT_BOOL出现之前,这听起来像是吹毛求疵。

It is still incorrect. CreateProcessW returns a "nonzero" value on success. That value may or may not be equal to the TRUE constant. As written, this is an invitation to compare against TRUE for equality. Which is the wrong thing to do for practically any boolean type. If you need to compare for equality, compare against FALSE, false, VARIANT_FALSE, ...

这仍然是不正确的。如果成功,CreateProcessW将返回“非零”值。该值可能等于也可能不等于真常量。正如所写的那样,这是一种将平等与真实进行比较的邀请。这对于几乎任何布尔类型都是错误的。如果需要比较是否相等,请比较FALSE、FALSE、VARIANT_FALSE、...

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com