简体   繁体   中英

C++ ShellExecute msg issue

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.

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 );

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM