Article View: borland.public.cppbuilder.nativeapi
Article #11905Re: calling dos program
From: Ed Mulroy [TeamB]
Date: Sat, 10 Jan 2004 20:08
Date: Sat, 10 Jan 2004 20:08
99 lines
3899 bytes
3899 bytes
I repeatedly tried with a variant of your code with BCB6 under XP and could
find no problem of the sort you describe. As your example is unrelated to
the VCL I used only the Windows API and C++. I substituted S4.EXE, a
utility of mine, for your CSW.EXE which I do not have.
Check your disk, your disk space, the file protection (system, read-only)
and, if it applies, the permissions for the account with which you are
logged in. They are more likely a source of the problem than the code you
posted.
Note that by using infiniite in testing the mutex you guarantee that your
code will hang if the DOS app hangs. Better to use a timeout in a loop.
Also note that by failing to include whitespace around parameters and
operators in your source you loose readability while gaining nothing.
. Ed
-----------------------
#include <windows.h>
#include <string>
using std::string;
bool RunDosApp(string const parameter)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
std::memset(&pi, 0, sizeof(pi)); // not needed
std::memset(&si, 0, sizeof(si)); // not needed
si.cb = sizeof(si);
string command_line("s4.exe ");
command_line = command_line + parameter;
// Run the DOS executable with CreateProcess.
int res =
CreateProcess(
NULL,
(LPSTR) command_line.c_str(),
0, // pointer to process attribs, should be NULL
0, // pointer to thread attribs, should be NULL
false, // BOOL type, should be FALSE
CREATE_NEW_CONSOLE,
0, // pointer to new environment, should be NULL
0, // pointer to current dir, should be NULL
&si,
&pi);
if (!res)
{
// Error...
res = GetLastError();
}
else
{
// If we get here, CreateProcess succeeded in
// starting the DOS app.
res = WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
// Error?
if (res == WAIT_FAILED)
res = GetLastError();
else // All OK, return true.
return true;
}
if (res)
{
char buf[32];
// Something went awry so let the user know.
wsprintf(buf, "Error code is %d", res);
MessageBox(HWND_DESKTOP, buf, "", MB_OK);
}
// Won't get here unless something went wrong.
return false;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
RunDosApp("sdump");
return 0;
}
-----------------------
> Fraser Ross wrote in message
> news:400038d3@newsgroups.borland.com...
>
> Sometimes when I run the dos program it appears to
> terminate before it is finished. It should overwrite a
> few bytes at the start of the output file it writes before
> finishing. I did not make the dos program.
Technical Details
Message-ID:
<40005bcc$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<400038d3@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,cd433fcf47e8f03b X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-01-10 12:09:34 PST From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> Newsgroups: borland.public.cppbuilder.nativeapi References: <400038d3@newsgroups.borland.com> Subject: Re: calling dos program Date: Sat, 10 Jan 2004 15:08:39 -0500 Lines: 101 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 24.74.175.102 Message-ID: <40005bcc$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1073765324 24.74.175.102 (10 Jan 2004 12:08:44 -0700) X-Authenticated-User: edmulroy Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail