IDontWorkAtNASA
IDontWorkAtNASA

Reputation: 303

What is the difference between opening the command prompt with ShellExecute() vs. by hand?

I'm trying to run a java program via a windows program I created as a sort of "Launcher" application. Normally to run this java program I would either run a batch file or go directly to the cmd prompt window (Windows Key + R, type "cmd") and enter java -Xmx4096M -Dsun.java2d.noddraw=true [...]. The point is that java seems to be recognized in this window.

On the other hand, when I launch the command prompt from my application, java is no longer "recognized as an internal or external command, operable program or batch file."

TCHAR szPath[MAX_PATH];
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_SYSTEM, NULL, 0, szPath))) {
    PathAppend(szPath, TEXT("cmd.exe"));
    ShellExecuteW( GetSafeHwnd(),L"open", szPath, NULL, NULL, 1 );
}

Why is this so?

Upvotes: 2

Views: 765

Answers (1)

IDontWorkAtNASA
IDontWorkAtNASA

Reputation: 303

For future visitors who have this problem, here's my working solution:

PVOID OldValue = NULL;
if( Wow64DisableWow64FsRedirection(&OldValue) )  {
        TCHAR szPath[MAX_PATH];
    if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_SYSTEM, NULL, 0, szPath))) {
        PathAppend(szPath, TEXT("cmd.exe"));
        ShellExecuteW( GetSafeHwnd(), L"open", szPath, NULL, NULL, 1 );
    }
    if ( FALSE == Wow64RevertWow64FsRedirection(OldValue) ) {
        AfxMessageBox(L"A script that should never fail has failed.");
        return;
    }
}

Thanks to HansPassant for leading me in the right direction.

Upvotes: 3

Related Questions