143 lines
4.0 KiB
C++
143 lines
4.0 KiB
C++
/*********************************************************************************************
|
|
* Copyright 2002 - Volian Enterprises, Inc. All rights reserved.
|
|
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
|
|
* ------------------------------------------------------------------------------
|
|
* $Workfile: WinExeClass.cpp $ $Revision: 3 $
|
|
* $Author: Jsj $ $Date: 12/16/03 11:59a $
|
|
*
|
|
* $History: WinExeClass.cpp $
|
|
*
|
|
* ***************** Version 3 *****************
|
|
* User: Jsj Date: 12/16/03 Time: 11:59a
|
|
* Updated in $/LibSource/WinExeClass
|
|
* modified to all use in .net 1.0 and .net 1.1
|
|
*
|
|
* ***************** Version 2 *****************
|
|
* User: Jsj Date: 1/06/03 Time: 9:52a
|
|
* Updated in $/LibSource/WinExeClass
|
|
* fixed problem of running a C# program - was returning to main process
|
|
* before completing spawned process
|
|
*
|
|
* ***************** Version 1 *****************
|
|
* User: Jsj Date: 8/23/02 Time: 2:57p
|
|
* Created in $/LibSource/WinExeClass
|
|
*********************************************************************************************/
|
|
|
|
// This is the main DLL file.
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "WinExeClass.h"
|
|
|
|
using namespace WinExeClass;
|
|
|
|
// Spawn off another process and run the passed in program.
|
|
// Note that the documentation for WinExec() says that it's provided
|
|
// for 16-bit windows compatibility and that applications should use
|
|
// the CreateProcess function. So this class gives you access to
|
|
// either the WinExe() or the CreateProcess() functions, just in case
|
|
// one works better than the other for your current programming needs.
|
|
|
|
bool CLWinExec::RunWinExe(char *prgName)
|
|
{
|
|
bool RtnVal = false;
|
|
RtnVal = DoWinExe(prgName);
|
|
return RtnVal;
|
|
}
|
|
|
|
bool CLWinExec::RunCreateProcess(char *prgName)
|
|
{
|
|
bool RtnVal = false;
|
|
RtnVal = DoCreateProcess(prgName);
|
|
return RtnVal;
|
|
}
|
|
|
|
bool CLWinExec::RunWinExe(String *prgName)
|
|
{
|
|
USES_CONVERSION;
|
|
bool RtnVal = false;
|
|
// UINT hInst;
|
|
|
|
// Grab the program name from the String class and place
|
|
// in a NUll terminated char array.
|
|
// Note that String uses unicode chars so not every character
|
|
// can be convert to the char array. But this works for
|
|
// what we currently need.
|
|
int len;
|
|
char *tmpstr;
|
|
len = (prgName->get_Length)();
|
|
tmpstr = new char(len + 1);
|
|
ZeroMemory(tmpstr,len+1);
|
|
for (int i=0; i < len; i++)
|
|
tmpstr[i] = (prgName->get_Chars)(i);
|
|
|
|
RtnVal = DoWinExe(tmpstr);
|
|
|
|
return RtnVal;
|
|
}
|
|
|
|
|
|
bool CLWinExec::RunCreateProcess(String *prgName)
|
|
{
|
|
USES_CONVERSION;
|
|
bool RtnVal = false;
|
|
// UINT hInst;
|
|
|
|
// Grab the program name from the String class and place
|
|
// in a NUll terminated char array.
|
|
// Note that String uses unicode chars so not every character
|
|
// can be convert to the char array. But this works for
|
|
// what we currently need.
|
|
int len;
|
|
char tmpstr[256];
|
|
len = (prgName->get_Length)();
|
|
// tmpstr = new char[len + 1];
|
|
ZeroMemory(tmpstr,len+1);
|
|
for (int i=0; i < len; i++)
|
|
tmpstr[i] = (prgName->get_Chars)(i);
|
|
|
|
RtnVal = DoCreateProcess(tmpstr);
|
|
|
|
return RtnVal;
|
|
}
|
|
|
|
|
|
bool CLWinExec::DoWinExe(char *prgName)
|
|
{
|
|
bool RtnVal = false;
|
|
UINT hInst;
|
|
|
|
hInst = WinExec(prgName,SW_NORMAL);
|
|
if (hInst > 31){
|
|
RtnVal = true;
|
|
// Wait until child process exits.
|
|
WaitForSingleObject((HANDLE)hInst,INFINITE);
|
|
}
|
|
return RtnVal;
|
|
}
|
|
|
|
bool CLWinExec::DoCreateProcess(char *prgName)
|
|
{
|
|
bool RtnVal = false;
|
|
STARTUPINFO procIn;
|
|
PROCESS_INFORMATION procOut;
|
|
|
|
ZeroMemory(&procIn,sizeof(procIn));
|
|
ZeroMemory(&procOut,sizeof(procOut));
|
|
|
|
procIn.dwFlags = STARTF_FORCEOFFFEEDBACK;
|
|
procIn.cb = sizeof(STARTUPINFO);
|
|
|
|
// if (CreateProcess(prgName,NULL,NULL,NULL,FALSE,CREATE_SEPARATE_WOW_VDM,NULL,NULL,&procIn,&procOut))
|
|
if (CreateProcess(NULL,prgName,NULL,NULL,FALSE,0,NULL,NULL,&procIn,&procOut))
|
|
{
|
|
RtnVal = true; // process completed normally
|
|
// Wait until child process exits.
|
|
WaitForSingleObject(procOut.hProcess,INFINITE);
|
|
// Close process and thread handles.
|
|
CloseHandle(procOut.hProcess);
|
|
CloseHandle(procOut.hThread);
|
|
}
|
|
return RtnVal;
|
|
}
|