Search code examples
c++windowscmdshadow-copy

Strange CMD errors only when CMD is opened from my program


This is a weird one for sure.

If I open a command prompt window directly (searching cmd in start, right click > open command window here, cmd within bat file, etc....) all commands entered run perfectly fine.

If I open a command prompt window from within my C++ application (system("cmd"); or QProcess::startDetached("cmd"); etc....) the commands I enter throw errors.

Here are a few commands that don't work in the cmd opened from my app:

vssadmin delete shadows /all
vssadmin list shadows
wmic
shadowcopy

and so on... I get Class not registered and Initialization failure errors all around. Anything to do with shadow copies isn't working at all. But again, the weird thing is, those same commands work perfectly fine when cmd was opened traditionally (not from a program). Both instances of cmd have admin privileges.

So my question is, how come the way I open cmd affects whether or not some commands work? Everything I can see says there should be no difference.


Solution

  • 32-bit applications running on WOW64 will be put under file system redirection. Therefore if your app is a 32-bit one, the call system("c:\\windows\\system32\\cmd.exe"); will be redirected to C:\Windows\SysWOW64\cmd.exe and 32-bit cmd will always be invoked. You have some solutions:

    • Use system("c:\\windows\\sysnative\\cmd.exe"); to access the real system32 folder and get the 64-bit cmd
    • Turn off file system redirection explicitly (should be avoided in general)
    • Or better compiling it as a 64-bit app.