1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#define WIN32_LEAN_AND_MEAN // prevent windows.h from including "kitchen sink"
#include <windows.h> // include using <>
#include <iostream>
int main()
{
LPCSTR Application = "C:\\Program Files\\Windows Media Player\\wmplayer.exe";
// Command is passed as a non-const char* so should be passed in a modifiable buffer
// Media file extension must be provided
// Paths are quoted
char Command[1024] = "\"C:\\Program Files\\Windows Media Player\\wmplayer.exe\""
" \"C:\\Documents and Settings\\Jason\\My Documents\\Jason\\My Music\\LOOKING LIKE THIS.wav\"";
// use -A form of struct as working with char, LPCSTR, etc rather
// than TCHAR, LPCTSTR, etc
STARTUPINFOA si = {0}; // zero struct
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {0}; // zero struct
// use -A form of function as working with char, LPCSTR, etc rather
// than TCHAR, LPCTSTR, etc
if( CreateProcessA(
Application,
Command,
NULL,
NULL,
FALSE,
NULL,
0,
NULL,
&si,
&pi) )
{
std::cout << "CreateProcess succeeded\n";
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = 0;
GetExitCodeProcess(pi.hProcess, &exitCode);
std::cout << "Exit code: " << exitCode << "\n";
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
DWORD errCode = GetLastError();
std::cerr << "CreateProcess failed\nError code: " << errCode << "\n";
}
return 0;
}
|