Board index » cppbuilder » CreateProcess help

CreateProcess help


2006-03-16 11:29:05 PM
cppbuilder31
Hi All
I have the following code that only works consistently if I have the sleep()
function included., I discovered this by accident after using a break point
at the CreateProcess function (trying to diagnose what was happening).
Without a time delay the application creates an empty zip file, a sleep
value of 500msec does not work consistantly .
If I run the app from a command prompt then the zip is always created
correctly.
Can anyone think of a reason for such strange behaviour or am I doing
something wrong?
Thanks for any help
Jack
AnsiString MyApp = "c:\\Magus\\7_Zip\\7z.exe ";
AnsiString MyParams = "a -tzip c:\\magus\\fbdb\\zipbackups\\jack.zip
c:\\magus\\fbdb\\freddy.fbk";
CreateChildProcessAndWaitUntilDone(MyApp + MyParams);
}
//--------------------------------------------------------------------------
--------------
void __fastcall TManagerMainForm::CreateChildProcessAndWaitUntilDone
(const AnsiString&
strCmdLine)
{
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
// Set up members of STARTUPINFO structure.
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.lpReserved = NULL;
siStartInfo.lpReserved2 = NULL;
siStartInfo.cbReserved2 = 0;
siStartInfo.lpDesktop = NULL;
siStartInfo.wShowWindow = SW_HIDE;
siStartInfo.dwFlags = STARTF_USESHOWWINDOW;
Sleep(1000);
CreateProcess(
NULL,
strCmdLine.c_str(),
NULL, // process security attributes
NULL, // primary thread security attributes
0, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
// Wait for the processs to finish
DWORD rc = WaitForSingleObject(piProcInfo.hProcess, INFINITE);
}
Using Win2000, C++Builder 6
 
 

Re:CreateProcess help

"Jack Mills" < XXXX@XXXXX.COM >wrote in message
Quote
I have the following code that only works consistently if I have
the sleep() function included
Try this code instead:
{
CreateChildProcessAndWaitUntilDone("c:\\Magus\\7_Zip\\7z.exe a -tzip
c:\\magus\\fbdb\\zipbackups\\jack.zip c:\\magus\\fbdb\\freddy.fbk");
}
void __fastcall
TManagerMainForm::CreateChildProcessAndWaitUntilDone(const AnsiString&
strCmdLine)
{
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW;
if( CreateProcess(NULL, strCmdLine.c_str(), NULL, NULL, FALSE, 0,
NULL, NULL, &si, &pi) )
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
Gambit
 

Re:CreateProcess help

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

Try this code instead:
Thanks for the reply Gambit.
Unfortunately the new code suffers the same problem as the old code, an
empty zip is created.
If I put the sleep staement at the start of your code then the zip file is
created correctly. i.e.
Sleep(1000);
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
Jack
 

{smallsort}

Re:CreateProcess help

Hi Again
The problem is something different.
The sleep statement does not cure the problem consistantly, just most of the
time, maybe the zip application is the problem ?
Jack
"Jack Mills" < XXXX@XXXXX.COM >wrote in message>
Quote
Thanks for the reply Gambit.

Unfortunately the new code suffers the same problem as the old code, an
empty zip is created.

If I put the sleep staement at the start of your code then the zip file is
created correctly. i.e.
Sleep(1000);
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};

Jack


 

Re:CreateProcess help

Some more information that may give someone a clue to the problem (that is
beyond my skills to fix)
I have tried a different compression utility & the same problem, an empty
zip is created.(occasionally works)
Both of the these utilities work fine when run from a dos box (command
prompt)
I then tried using WinZip's command line tool, this opens it's own dos box &
the user is asked to press a key to continue, the zip is then created
correctly, every time.
Does this trigger any ideas?
Jack
 

Re:CreateProcess help

"Jack Mills" < XXXX@XXXXX.COM >wrote in message
Quote
Unfortunately the new code suffers the same problem as the
old code, an empty zip is created.
If I put the sleep staement at the start of your code then the
zip file is created correctly
No amount of sleeping is going to solve this problem. You are sleeping
before even launching the zip application at all, which means that the zip
application is always running at full speed. All you are doing is slowing
down your own application.
Gambit
 

Re:CreateProcess help

"Jack Mills" < XXXX@XXXXX.COM >wrote in message
Quote
Both of the these utilities work fine when run from a dos box
Then they should be working in your own code as well, assuming that you are
using the exact same command.
Gambit
 

Re:CreateProcess help

Unfortunately they dont run correctly, I use the same command string.
I am using the code you supplied, when I set the window to SH_NORMAL I see
the dos box open, but most of the time it closes very quickly creating just
the empty zip.
The interesting thing is that when using winzip command line, the fact of
having to press a key to start the zip process seems to give a delay that
allows the operation to complete properly, is there a way of acheiving the
same thing. i.e. open the dos box, make the user press akey before executing
the command string?
Thanks for the help
Jack
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

Then they should be working in your own code as well, assuming that you
are
using the exact same command.


Gambit


 

Re:CreateProcess help

OK Found the problem, ME
The file that I was trying to zip was sometimes still being cretaed by a
database backup.
The command line zip app was telling the file was being used by another
process.
Thanks for the help Gambit & sorry to have wasted your time.
Jack
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

Then they should be working in your own code as well, assuming that you
are
using the exact same command.

Gambit


 

Re:CreateProcess help

"Jack Mills" < XXXX@XXXXX.COM >wrote in message
Quote
Unfortunately they dont run correctly, I use the same command string.
I am using the code you supplied, when I set the window to SH_NORMAL
I see the dos box open, but most of the time it closes very quickly
creating
just the empty zip.
I suggest that you write a .bat file that launches the command and calls
'pause' at the end, and then have CreateProcess() launch that .bat file
instead of the command directly. Or else assign a file handle to the
STARTUPINFO structure's hStdout and hStdErr members in order to send output
directly to a text file. This way, you can see the actual output of the
command. Chances are, the command is probably failing and you simply can't
see the result under normal circumstances since you are not displaying a DOS
box for it.
Gambit
 

Re:CreateProcess help

"Jack Mills" < XXXX@XXXXX.COM >wrote in message
Quote
The file that I was trying to zip was sometimes still being
cretaed by a database backup. The command line zip app
was telling the file was being used by another process.
A lot of DOS command-line applications return a non-zero return code when a
failure occurs. This allows batch files to detect errors when running
scripts that execute command-line applications. In your code, after you
call CreateProcess() and WaitForSingleObject(), you can then call
GetExitCodeProcess() to retreive that same code. If you are lucky, then
your zip applications are doing the same thing.
Gambit
 

Re:CreateProcess help

I added the extra GetExitCodeProcess function & it works fine (I sent a
faulty string & it reported the error code).
Thanks for all the help
Jack
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

A lot of DOS command-line applications return a non-zero return code when
a
failure occurs. This allows batch files to detect errors when running
scripts that execute command-line applications. In your code, after you
call CreateProcess() and WaitForSingleObject(), you can then call
GetExitCodeProcess() to retreive that same code. If you are lucky, then
your zip applications are doing the same thing.


Gambit