-1

I have the following C code that should run the default Windows program changepk.exe with an UAC prompt

ShellExecute(NULL, "runas", "C:\\Windows\\System32\\changepk.exe", 0, 0, SW_SHOWNORMAL);

(Side note that the output of the ShellExecute is 2). However, when I try to execute the 'changepk.exe' with these lines nothing happens at all, but for 'notepad.exe' instead of 'changepk.exe' it works and gives me an UAC prompt. What could be the issue here and what are the potential ways around it?

  • What is the return value of `ShellExecute()`? Have you tried `ShellExecuteEx()` (which does better error reporting)? Do you have the same problem if you pass input parameters to `changepk` to actually perform an action? Do you have the same problem if you execute `changepk` via `cmd.exe /C`? – Remy Lebeau Aug 09 '20 at 19:10
  • @RemyLebeau Thank you for your suggestions. The first option didn't work, tried the /ProductKey argument. The cmd.exe /C switch didn't work either. – Vilius Povilaika Aug 09 '20 at 19:12

1 Answers1

4

Error 2 is ERROR_FILE_NOT_FOUND. Make sure that changepk.exe actually exists on your machine at that path.

More importantly, if your app is a 32bit EXE running on a 64bit Windows, then you are likely encountering the File System Redirector at work, which will redirect "C:\\Windows\\System32\\..." to "C:\\Windows\\SysWOW64\\..." for 32bit processes.

If that is the case, try using WOW64's sysnative alias to reach the 64bit System32 folder from a 32bit app, eg:

ShellExecute(NULL, "runas", "C:\\Windows\\Sysnative\\changepk.exe", 0, 0, SW_SHOWNORMAL);

Or, you can disable the redirector temporarily by using Wow64DisableWow64FsRedirection(), eg:

PVOID oldValue;
Wow64DisableWow64FsRedirection(&oldValue);

ShellExecute(NULL, "runas", "C:\\Windows\\System32\\changepk.exe", 0, 0, SW_SHOWNORMAL);

Wow64RevertWow64FsRedirection(oldValue);

The reason why NotePad works is because a 64bit Windows provides both 32bit and 64bit versions of NotePad, so you end up running the correct one regardless of where "C:\\Windows\\System32\\notepad.exe" actually maps to.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770