0

I have a .bat file which sends a local message. When I run it myself (double-click) it works (a message pops out). It doesn't work when I launch the .bat with ShellExecute(); though. What could be the case? Here's the code:

message.bat

msg * hello

main.cpp

#include <windows.h>

int main()
{
    ShellExecute(NULL, "open", "message.bat", NULL, NULL, 0);
}

Other things in .bat such as start <something>, shutdown, etc. work with ShellExecute();.

EDIT I can't even run msg with system();. It only works manually from cmd or a .bat file.

gregjj2
  • 1
  • 3

2 Answers2

0

You should run batch files like this:

const TCHAR batchFilePath[MAX_PATH] = _T("C:\\Test\\message.bat");

TCHAR systemDirPath[MAX_PATH] = _T("");
::GetSystemDirectory( systemDirPath, sizeof(systemDirPath)/sizeof(_TCHAR) );

TCHAR commandLine[2 * MAX_PATH + 16] = _T("");

_sntprintf( commandLine, sizeof(commandLine)/sizeof(_TCHAR),
    _T("\"%s\\cmd.exe\" /C \"%s\""), systemDirPath, batchFilePath );

STARTUPINFO si = {0}; 
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {0};

if( !::CreateProcess( NULL,
    commandLine,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &si,
    &pi )
    )
{
    _tprintf( _T("CreateProcess failed (%d)\n"), GetLastError() );
    return FALSE;
}

::WaitForSingleObject( pi.hProcess, INFINITE );
::CloseHandle( pi.hProcess );
::CloseHandle( pi.hThread );
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
0

Could it be that you're compiling it as 32bit on 64bit OS? in that case it will not be allowed to run msg.exe

Roman Pustylnikov
  • 1,937
  • 1
  • 10
  • 18