I am wrapping the Windows API CreateProcess.
Here is my code.
int __stdcall _Execute(LPTSTR program, LPTSTR workingdir, WORD show){
// Check if program has content
if (!program || !*program){
MessageBox(NULL, L"You must provide path to executable", L"Error", MB_OK);
return 0;
}
// Create buffers and copy program param to szCmd buffer
TCHAR szDir[MAX_PATH + 1], szCmd[MAX_PATH + 1];
wcscpy_s(szCmd, program);
// Check if workingdir has content
if (!workingdir || !*workingdir){
// If not, use current dir
GetCurrentDirectoryW(sizeof(szDir), szDir);
}else{
// If so, use it
wcscpy_s(szDir, workingdir);
}
// Create structs
PROCESS_INFORMATION pi;
STARTUPINFO si;
// zero and fill structs
//TODO - check std flags.
SecureZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = show;
SecureZeroMemory(&pi, sizeof(pi));
// Create the process or return fail
if (!CreateProcess(NULL, szCmd, NULL, NULL, TRUE, 0, NULL, szDir, &si, &pi))
{
return 0;
}
// Close thread and return win
CloseHandle(pi.hThread);
return 1;
}
If I call it like this for example...
_Execute("C:\windows\notepad.exe C:\file.txt", "C:\windows\system32", SHOW);
Notepad will startup and load file.txt.
But if I try...
_Execute("C:\Windows\System32\cmd.exe ping google.co.uk", "C:\windows\system32", SHOW);
I only get my command prompt open and set at system dir.
I expected it to run the ping command.
I'm hoping for hints and tips as to my mistakes.