0

I'm trying to get the output of the powercfg -requests command from a vc++ program. However, it gives a output which is totally different from the output which is given when I manually execute the command in cmd.exe.

I tried to search about it and realized that I get the correct output when the cmd.exe and powercfg.exe present in "\Windows\System32" and get the different/undesired output when the cmd.exe and powercfg.exe present in "\Windows\SysWOW64" are used.

I tried to use CreateProcess and give the absolute path to both, as shown below:

STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;
printf("infoBuf : %S\n",infoBuf);
wchar_t cmdline[] = L"C:\\Windows\\System32\\cmd.exe /K C:\\Windows\\System32\\powercfg.exe -requests
wchar_t path[] = L"C:\\Windows\\System32\\cmd.exe";

if (!CreateProcess(path, cmdline, NULL, NULL, false, CREATE_UNICODE_ENVIRONMENT,
   NULL, NULL, &si, &pi))
{
    std::cout << GetLastError();
    abort();
}

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

But the compiled executable still gives out the output similar to the 64 bit cmd.exe and powercfg.exe.

Am I missing anything? How else do I explicitly set and make sure that my program should use only the 32bit versions of cmd.exe and powercfg.exe.

San852
  • 19
  • 8
  • why you run *cmd.exe* but not direct *powercfg.exe* ?? and not visible how you try gey output. std handles you not redirect – RbMm Mar 30 '18 at 07:10
  • anyway if you run from wow64 app - you redirect to syswow64 folder, unless you call `Wow64DisableWow64FsRedirection` – RbMm Mar 30 '18 at 07:15
  • @RbMm I tried to run direct powercfg.exe, even then the same result and mine is a 32bit app, any suggestions? – San852 Mar 30 '18 at 07:20
  • In 64-bit Windows, the real "System32" directory has 64-bit executables, and "SysWOW64" has 32-bit executables. However, in a 32-bit process on such a system (WOW64 emulation), by default "System32" gets redirected to "SysWOW64". This is done to work around the countless programs that hard code using "System32" rather than calling `GetSystemDirectory`. Under WOW64 the real 64-bit system directory is available as "%SystemRoot%\SysNative". – Eryk Sun Mar 30 '18 at 07:23
  • [`Wow64EnableWow64FsRedirection`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365744(v=vs.85).aspx)`(FALSE)` before call `CreateProcess` – RbMm Mar 30 '18 at 07:31
  • Wouldn't it just be easier to use the API? And why do you involve cmd? You start cmd and ask it to run powercfg rather than going direct to powercfg? Why? – David Heffernan Mar 30 '18 at 07:42
  • @DavidHeffernan I searched for the API and wasn't able to find the equivalent API for powercfg -requests. I was trying to explicitly run the powercfg present under System 32, hence starting cmd under System 32 and then asking it to run powercfg – San852 Mar 30 '18 at 07:58
  • It makes no sense to invoke cmd here. Think about it. – David Heffernan Mar 30 '18 at 08:05
  • *"Am I missing anything?"* - Indeed you are: A [mcve], and a clear problem statement, including the expected behavior and the observed behavior. – IInspectable Mar 30 '18 at 11:02

0 Answers0