Re: CreateProcess not working as doc specifies.
- From: "Hector Santos" <nospamhere@xxxxxxxxxxxxxx>
- Date: Wed, 15 Jun 2005 22:29:28 -0400
"David Bennion" <DavidBennion@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B3B987CA-368C-441F-8BCD-77ED08E388BE@xxxxxxxxxxxxxxxx
>
>
> Thanks for your reply Hector. The issue is that the client has many batch
> files and I don't want to tell him he must modify them all. I understand
how
> to modify the batch files to make it work.
>
> Additionally, simply setting exit at the end causes the terminal window to
> exit -- so we would need to protect that exit with an if that could detect
if
> my product was running.
Not with the /B option.
> I am beginning to wonder if this is even possible.
If you are trying to do this as a cross-the-board product for all the
various OSes, you might have some issues to test. Even though I been
working with this stuff for a long time, I don't recall off hand if Win95
for example supports the EXIT command.
Anyway, If you don't have control of the client batch file, then you may
want to consider wrapping it with a dynamically created skeleton script
batch file.
Your dynamically created batch file skeleton.bat:
@echo off
REM - Auto Created by DaveApplet.exe
call client.bat
exit %errorlevel%
Also you can do some tricks to test of the errorlevel has changed, for
example:
@echo off
REM - Auto Created by DaveApplet.exe
call :SetErrorLevel
call client.bat
exit %errorlevel%
:--------------------------
:SetErrorLevel
:--------------------------
exit /b %1
goto :eof
Here is a complete working example:
// File: TestParent2.cpp
// Author: Hector Santos, Santronics Software, Inc.
// Compile: cl testparent.cpp /W3 /GX /MD /D "_AFXDLL"
#include <stdio.h>
#include <afx.h>
BOOL runbatch(const char *fn, int &exitcode)
{
exitcode = -1;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
DWORD dwFlags = 0;
char szCmd[MAX_PATH];
strcpy(szCmd,"/c ");
strcat(szCmd,fn);
if (!CreateProcess(getenv("comspec"),(char *)szCmd,
NULL, NULL, FALSE,
dwFlags,
NULL, NULL, &si, &pi)) {
printf("! CreateProcess() error %d\n",GetLastError());
return FALSE;
}
CloseHandle(pi.hThread);
if (WAIT_OBJECT_0== WaitForSingleObject(pi.hProcess, INFINITE)) {
GetExitCodeProcess(pi.hProcess, (DWORD *)&exitcode);
} else {
printf("error with wait\n");
}
CloseHandle(pi.hProcess);
return TRUE;
}
BOOL MakeSkeleton(const char *szTemp, const char *szClient)
{
FILE *fv = fopen(szTemp,"wt");
if (fv) {
fprintf(fv,"@echo off\n");
fprintf(fv,":--------------------------\n");
fprintf(fv,":Auto Created Wrapper for %s\n",szClient);
fprintf(fv,":--------------------------\n");
fprintf(fv," call :SetErrorLevel -1\n");
fprintf(fv," call %s\n",szClient);
fprintf(fv," exit %errorlevel%\n");
fprintf(fv," goto :eof\n");
fprintf(fv,":--------------------------\n");
fprintf(fv,":SetErrorLevel\n");
fprintf(fv,":--------------------------\n");
fprintf(fv," exit /b %1\n");
fprintf(fv," goto :eof\n");
fclose(fv);
return TRUE;
}
return FALSE;
}
void main(char argc, char *argv[])
{
int exitcode;
MakeSkeleton("skeleton.cmd","client.bat");
BOOL ok = runbatch("skeleton.cmd", exitcode);
printf("ok : %d exitcode: %d error: %d\n",ok, exitcode, GetLastError());
}
--
Hector Santos, Santronics Software, Inc.
http://www.santronics.com
.
- Follow-Ups:
- Re: CreateProcess not working as doc specifies.
- From: Gary Chanson
- Re: CreateProcess not working as doc specifies.
- From: Hector Santos
- Re: CreateProcess not working as doc specifies.
- References:
- CreateProcess not working as doc specifies.
- From: David Bennion
- Re: CreateProcess not working as doc specifies.
- From: Walter Briscoe
- Re: CreateProcess not working as doc specifies.
- From: David Bennion
- Re: CreateProcess not working as doc specifies.
- From: Gary Chanson
- Re: CreateProcess not working as doc specifies.
- From: Walter Briscoe
- Re: CreateProcess not working as doc specifies.
- From: David Bennion
- Re: CreateProcess not working as doc specifies.
- From: Hector Santos
- Re: CreateProcess not working as doc specifies.
- From: David Bennion
- CreateProcess not working as doc specifies.
- Prev by Date: Re: how to implement Thread Local Storage (TLS) in kernel
- Next by Date: Re: Service crash using MSDN example code
- Previous by thread: Re: CreateProcess not working as doc specifies.
- Next by thread: Re: CreateProcess not working as doc specifies.
- Index(es):
Relevant Pages
|