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?
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.