运行 WSReset.exe 使用 CreateProcess 或系统

Running WSReset.exe with CreateProcess or system

我正在 运行 WSReset.exe Windows 10 上尝试,但一直收到找不到文件的错误。 WSReset.exe 肯定在 "C:\Windows\System32\WSReset.exe" 中,但是从我的程序启动的 CMD/Powershell window 似乎找不到它。但是在我的程序之外启动的 CMD/Powershell window 确实执行了该文件。

STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
ZeroMemory(&processInfo, sizeof(processInfo));
int t = (CreateProcess(L"C:/Windows/System32/wsreset.exe", L"", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo));
    WaitForSingleObject(processInfo.hProcess, INFINITE);
    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);

SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = L"C:/Windows/System32/wsreset.exe";
ShExecInfo.lpParameters = L"";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);

生成 "Windows cannot find..." 错误消息。

该应用程序是 32 位的。奇怪的是 System32 = 64 位,SysWOW64 = 32 位。所以当我调用 "C:/Windows/System32/wsreset.exe" 时,windows 会把它变成 "C:/Windows/SysWOW64/wsreset.exe" 而那个位置没有 wsreset.exe。

解决方案是在调用 CreateProcess/ShellExecuteEx/System 之前禁用重定向。

//Disable redirection
PVOID OldValue = NULL;
Wow64DisableWow64FsRedirection(&OldValue);

STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo = { 0 };
CreateProcess(L"C:/Windows/System32/WSReset.exe", L"", NULL, NULL, FALSE, 0, NULL, NULL, &info, &processInfo);
WaitForSingleObject(processInfo.hProcess, INFINITE);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);

//Enable redirection (important, otherwise program can crash)
Wow64RevertWow64FsRedirection(OldValue);