CreateProcessEx is a function that extends CreateProcessEx a little - it minimizes the main app window while the external process is run, and then maxamises it again when it finishes.
This works nicely in almost all circumstances. If I call notepad like this:
CreateProcessEx("NOTEPAD.E
XE",NULL /*arguments*/, TRUE, FALSE, TRUE, TRUE, NULL);
Or like this:
CreateProcessEx("C:\\windo
ws\\NOTEPA
D.EXE",NUL
L /*arguments*/, TRUE, FALSE, TRUE, TRUE, NULL);
everything works as it should; my main window minimises, and then maxamises again when I close notepad.exe .
It just won't work for the program I actually want to call, which is pg_dump.exe, a command line utility through which SQL dumps are performed.
CreateProcessEx("C:\\test\
\pg_dump.e
xe",NULL /*arguments*/, TRUE, FALSE, TRUE, TRUE, NULL);
If I open it in windows, I get a dos box flashing for a moment, because it needs command line arguments (I have some command line arguments for it, but thats not relevant to the problem at hand.)
My window won't minimise, and I see no flashing dos box. I tried intentionally corrupting the file with a hex editor, and now when I open it in windows explorer I get an error messagebox. However, when I attempt to open it using CreateProcessEx, I see no such message box. I've even tried renaming notepad.exe to pg_dump.exe, and it will open notepad (pg_dump.exe) then fine.
What have I failed to take into account? What error have I made?
This is the CreateProcessEx function in it's entirety:
DWORD CreateProcessEx ( LPCSTR lpAppPath, LPCSTR lpCmdLine,
BOOL bAppInCmdLine, BOOL bCompletePath,
BOOL bWaitForProcess, BOOL bMinimizeOnWait, HWND hMainWnd ) {
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
char szAppPath [ _MAX_PATH ];
char szCmdLine [ _MAX_PATH ];
char drive [ _MAX_DRIVE ];
char dir [ _MAX_DIR ];
char name [ _MAX_FNAME ];
char ext [ _MAX_EXT ];
szAppPath[ 0 ] = '\0';
szCmdLine[ 0 ] = '\0';
ZeroMemory( &startupInfo, sizeof( STARTUPINFO ));
startupInfo.cb = sizeof( STARTUPINFO );
ZeroMemory( &processInformation, sizeof( PROCESS_INFORMATION ));
if ( bCompletePath ) {
GetModuleFileName( GetModuleHandle( NULL ), szAppPath, _MAX_PATH );
_splitpath( szAppPath, drive, dir, NULL, NULL );
_splitpath( lpAppPath, NULL, NULL, name, ext );
_makepath ( szAppPath, drive, dir, name, strlen( ext ) ? ext : ".exe" );
}
else strcpy( szAppPath, lpAppPath );
if ( bAppInCmdLine ) {
strcpy( szCmdLine, "\"" );
strcat( szCmdLine, szAppPath );
strcat( szCmdLine, "\"" );
}
if ( lpCmdLine ) {
// We must use quotation marks around the command line (if any)...
if ( bAppInCmdLine ) strcat( szCmdLine, " \"" );
else strcpy( szCmdLine, "\"" );
strcat( szCmdLine, lpCmdLine );
strcat( szCmdLine, "\"" );
}
DWORD dwExitCode = -1;
if ( CreateProcess( bAppInCmdLine ? NULL: szAppPath, // lpszImageName
szCmdLine, // lpszCommandLine
0, // lpsaProcess
0, // lpsaThread
FALSE, // fInheritHandles
DETACHED_PROCESS, // fdwCreate
0, // lpvEnvironment
0, // lpszCurDir
&startupInfo, // lpsiStartupInfo
&processInformation // lppiProcInfo
)) {
if ( bWaitForProcess ) {
if ( bMinimizeOnWait )
if ( IsWindow( hMainWnd )) ShowWindow( hMainWnd, SW_MINIMIZE );
#ifdef __AFX_H__
else AfxGetMainWnd()->ShowWindo
w( SW_MINIMIZE );
#endif
WaitForSingleObject( processInformation.hProces
s, INFINITE );
if ( bMinimizeOnWait )
if ( IsWindow( hMainWnd )) ShowWindow( hMainWnd, SW_RESTORE );
#ifdef __AFX_H__
else AfxGetMainWnd()->ShowWindo
w( SW_RESTORE );
#endif
GetExitCodeProcess( processInformation.hProces
s, &dwExitCode );
}
else {
CloseHandle( processInformation.hThread
);
CloseHandle( processInformation.hProces
s );
dwExitCode = 0;
}
}
return dwExitCode;
}
Thanks in advance,
Sternocera