⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmprocessrunner.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive: $

      $Revision: $
          $Date: $
        $Author: $

    Description: Implementation of the VMProcessRunner Class

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/


#include "../../../stdafx.h"
#include <string.h>
#include "../VMCoreInterpreter.h"
#include "VMProcessRunner.h"



/*****************************************************************************/
/*
     FUNCTION NAME:  VMProcessRunner::StartProcessNoWait

       DESCRIPTION:  starts the process provided in pchToStart

             INPUT:  pchToStart - pointer to character string containing full
                     path of process to start

            OUTPUT:  none

           RETURNS:  true if worked, false otherwise
*/
bool VMProcessRunner::StartProcessNoWait( char* pchToStart )
{
  char                achPath[ _MAX_PATH ];
  char                achCurPath[ _MAX_PATH ];
  char                achDrive[ _MAX_DRIVE ];
  char                achDir[ _MAX_DIR ];
  char                achFname[ _MAX_FNAME ];
  char                achExt[ _MAX_EXT ];
  bool                bSetDir = false;
  STARTUPINFO         xStartupInfo;
  PROCESS_INFORMATION xProcessInfo;
  bool                bReturn;

  SECURITY_ATTRIBUTES xSecurityAttribs = { sizeof( SECURITY_ATTRIBUTES ), 
                                           NULL,    // NULL security descriptor
                                           true };  // Inherit handles (necessary!)

  // set up the full path to file to execute
  //
  if ( NULL != strchr( pchToStart, ':' ) )
  {
     bSetDir = ( TRUE == GetCurrentDirectory( MAX_PATH, achCurPath ) );
     _splitpath( pchToStart, achDrive, achDir, achFname, achExt );
     strcpy( achPath, achDrive );
     strcat( achPath, achDir );
     achPath[strlen(achPath)-1] = '\0';
     bSetDir &= ( TRUE == SetCurrentDirectory( achPath ) );
  }

  // init process startup struct
  //
  FillMemory( &xStartupInfo, sizeof(xStartupInfo), 0 );
  xStartupInfo.cb          = sizeof( xStartupInfo );
  xStartupInfo.dwFlags     = STARTF_USESHOWWINDOW;
  xStartupInfo.wShowWindow = SW_SHOWDEFAULT;

  bReturn = ( TRUE == CreateProcess( NULL,                    // lpApplicationName
                                     pchToStart,              // lpCommandLine
                                     NULL,                    // lpProcessAttributes
                                     NULL,                    // lpThreadAttributes
                                     true,                    // bInheritHandles
                                     NORMAL_PRIORITY_CLASS,   // dwCreationFlags
                                     NULL,                    // lpEnvironment
                                     NULL,                    // lpCurrentDirectory
                                     &xStartupInfo,
                                     &xProcessInfo ) ); 

  CloseHandle( xProcessInfo.hThread );
  CloseHandle( xProcessInfo.hProcess );

  if ( bSetDir )
  {
    SetCurrentDirectory( achCurPath );
  }
  return( bReturn );
}
/* End of function "VMProcessRunner::StartProcessNoWait"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMProcessRunner::StartProcessGetHandle

       DESCRIPTION:  starts the process provided in pchToStart

             INPUT:  pchToStart - pointer to character string containing full
                     path of process to start

            OUTPUT:  none

           RETURNS:  the process handle
*/
HANDLE VMProcessRunner::StartProcessGetHandle( char* pchToStart, char* pchArgs )
{
  char                achPath[_MAX_PATH];
  char                achCurPath[_MAX_PATH];
  char                achDrive[_MAX_DRIVE];
  char                achDir[_MAX_DIR];
  char                achFname[_MAX_FNAME];
  char                achExt[_MAX_EXT];
  bool                bSetDir = false;
  STARTUPINFO         xStartupInfo;
  PROCESS_INFORMATION xProcessInfo;
  bool                bReturn;

  SECURITY_ATTRIBUTES xSecurityAttribs = { sizeof(SECURITY_ATTRIBUTES), 
                                           NULL,    // NULL security descriptor
                                           true };  // Inherit handles (necessary!)

  // set up the full path to the process to execute
  //
  if ( NULL != strchr( pchToStart, ':' ) )
  {
     bSetDir = ( TRUE == GetCurrentDirectory( MAX_PATH, achCurPath ) );
     _splitpath( pchToStart, achDrive, achDir, achFname, achExt );
     strcpy( achPath, achDrive );
     strcat( achPath, achDir );
     achPath[strlen(achPath)-1] = '\0';
     bSetDir &= ( TRUE == SetCurrentDirectory( achPath ) );
  }

  // init process startup struct
  //
  FillMemory( &xStartupInfo, sizeof( xStartupInfo ), 0 );
  xStartupInfo.cb          = sizeof( xStartupInfo );
  xStartupInfo.dwFlags     = STARTF_USESHOWWINDOW;
  xStartupInfo.wShowWindow = SW_SHOWDEFAULT;

  char achCmdLine [ _MAX_PATH + 1 ];
  sprintf( achCmdLine, "%s %s", pchToStart, pchArgs );

  bReturn = ( TRUE == CreateProcess( pchToStart,              // lpApplicationName
                                     achCmdLine,              // lpCommandLine
                                     NULL,                    // lpProcessAttributes
                                     NULL,                    // lpThreadAttributes
                                     true,                    // bInheritHandles
                                     NORMAL_PRIORITY_CLASS,   // dwCreationFlags
                                     NULL,                    // lpEnvironment
                                     NULL,                    // lpCurrentDirectory
                                     &xStartupInfo,
                                     &xProcessInfo ) );

  CloseHandle( xProcessInfo.hThread );

  if ( bSetDir )
  {
    SetCurrentDirectory( achCurPath );
  }

  if ( bReturn )
  {
    return( xProcessInfo.hProcess );
  }
  else
  {
    return( NULL );
  }
}
/* End of function "VMProcessRunner::StartProcessGetHandle"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: CheckForOrMakeDir

       DESCRIPTION: Confirms the existance of (or creates) a directory on
                    on the destination path	

             INPUT: pchDirectory - the target directory

           RETURNS: a variety of coded values based on success of various
                    failure modes	
*/
short VMProcessRunner::CheckForOrMakeDir( const char* pchDirectory )
{
  char*               pchToken;
  char                achDir[ MAXDIRLEN ];
  char                achTemp[ MAXDIRLEN + 50 ];
  char                achSeps[] = "\\";
  SECURITY_ATTRIBUTES xSecurityAttribs;
	
  xSecurityAttribs.nLength              = sizeof( xSecurityAttribs );
  xSecurityAttribs.lpSecurityDescriptor = NULL;
  xSecurityAttribs.bInheritHandle       = false;

  // check and make sure they have a drive indicater
  //
  if ( pchDirectory[1] != ':' )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -