Re: CreateProcess not working as doc specifies.




"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




.



Relevant Pages

  • Re: exit /b does not work properly in subroutines
    ... Also why exit without /b switch exits entire batch, ... echo This line should never be reached ... within a batch to exit current batch and set ERRORLEVEL to 1. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: exit /b does not work properly in subroutines
    ... I see - /b switch is required to exit batch. ... echo This line should never be reached ... within a batch to exit current batch and set ERRORLEVEL to 1. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: exit /b does not work properly in subroutines
    ... Please have a look at two batch files: they only differ in one line (exit ... echo This line should never be reached ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: cmd batch file question
    ... Exit quits the CMD.EXE program (command interpreter) or the current batch ... This question was posted individually to multiple groups. ... I don't use cmd.exe very much and I don't write batch files very often. ...
    (microsoft.public.windowsxp.help_and_support)