📄 vmprocessrunner.cpp
字号:
{
return( F_NODRIVE );
}
// make a local copy of the dir for tokenizing
//
strcpy( achDir, pchDirectory );
achTemp[ 0 ] = '\0';
// tokenize the directory building a copy and checking each subdir
//
pchToken = strtok( achDir, achSeps );
while ( pchToken )
{
strcat( achTemp, pchToken );
AddBackSlash( achTemp );
if ( ::SetCurrentDirectory( achTemp ) == false )
{
if ( ::CreateDirectory( achTemp, &xSecurityAttribs ) == false )
{
return( F_CREATEDIR );
}
if ( ::SetCurrentDirectory( achTemp ) == false )
{
return( F_SETCURRENTDIR );
}
}
pchToken = strtok( NULL, achSeps );
}
return( true );
}
/* end of function "CheckForOrMakeDir" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: AddBackSlash
DESCRIPTION: Check end of string and add a backshash if one does not
exist
INPUT: pchBuffer - a pointer to the string to add the '\' to
RETURNS: void
*/
void VMProcessRunner::AddBackSlash( char* pchBuffer )
{
short iLength;
// add a \ if needed
//
iLength = strlen( pchBuffer );
if ( iLength )
{
if ( pchBuffer[ iLength - 1 ] != '\\' )
{
strcat( pchBuffer, "\\" );
}
}
}
/* end of function "AddBackSlash" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: StartAndWaitForProcess
DESCRIPTION: Starts a process, and then waits for it to complet running
INPUT: pchToStart - the 'command line' to run
roOutput - data returned from stdout of the background task
will be written here
RETURNS: none
*/
bool VMProcessRunner::StartAndWaitForProcess( char* pchToStart, PROCESS_OUTPUT& roOutput, VMDebuggerHook* poDebugger )
{
SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL );
int iKount = 0;
HANDLE hWritePipe;
HANDLE hReadPipe;
bool bAbleToStartProcess;
SECURITY_ATTRIBUTES xSecurityAttribs;
SECURITY_DESCRIPTOR xSecurityDescriptor;
LPSECURITY_ATTRIBUTES pxSecurityAttribs = NULL;
STARTUPINFO xStartUp;
PROCESS_INFORMATION xProcInfo;
bool bCanContinue;
// Create a security descriptor allowing inheritence of handles
//
if ( 0 == InitializeSecurityDescriptor( &xSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION ) )
{
bCanContinue = false;
goto GETOUT;
}
if ( SetSecurityDescriptorDacl( &xSecurityDescriptor, true, NULL, false ) )
{
bCanContinue = true;
xSecurityAttribs.nLength = sizeof( SECURITY_ATTRIBUTES );
xSecurityAttribs.bInheritHandle = true;
xSecurityAttribs.lpSecurityDescriptor = &xSecurityDescriptor;
pxSecurityAttribs = &xSecurityAttribs;
}
else
{
bCanContinue = false;
goto GETOUT;
}
if ( !CreatePipe( &hReadPipe, &hWritePipe, pxSecurityAttribs, 1 ) )
{
bCanContinue = false;
goto GETOUT;
}
ZeroMemory( &xStartUp, sizeof( STARTUPINFO ) );
xStartUp.cb = sizeof( STARTUPINFO );
xStartUp.dwFlags = STARTF_USESTDHANDLES;
xStartUp.hStdOutput = hWritePipe;
xStartUp.hStdError = hWritePipe;
bAbleToStartProcess = ( TRUE == CreateProcess( NULL, // lpApplicationName
pchToStart, // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
true, // bInheritHandles
DETACHED_PROCESS, // dwCreationFlags
NULL, // lpEnvironment
NULL, // lpCurrentDirectory
&xStartUp,
&xProcInfo ) );
SetThreadPriority( xProcInfo.hThread, THREAD_PRIORITY_HIGHEST );
CloseHandle( xProcInfo.hThread );
if ( bAbleToStartProcess )
{
DWORD dwWait;
dwWait = WaitForSingleObject( xProcInfo.hProcess, 1 );
while ( WAIT_TIMEOUT == dwWait )
{
bool bStatus;
DWORD dwBytesRead;
DWORD dwBytesAvailable;
if ( PeekNamedPipe( hReadPipe,
NULL,
NULL,
NULL,
&dwBytesAvailable,
NULL ) )
{
if ( 0 != dwBytesAvailable )
{
char* pchBuffer = new char[ dwBytesAvailable + 1 ];
memset( pchBuffer, '\0', dwBytesAvailable + 1 );
bStatus = ( TRUE == ReadFile( hReadPipe,
pchBuffer,
dwBytesAvailable,
&dwBytesRead,
NULL ) );
if ( ( true == bStatus ) && ( 0 != dwBytesRead ) )
{
roOutput.push_back( std::string( pchBuffer ) );
if ( NULL != poDebugger )
{
poDebugger->DebuggerProgressTick();
}
// printf( pchBuffer );
}
delete[] pchBuffer;
}
}
dwWait = WaitForSingleObject( xProcInfo.hProcess, 1 );
}
}
else
{
bCanContinue = false;
roOutput.push_back( std::string( "Unable To Start Process" ) );
}
CloseHandle( xProcInfo.hProcess );
CloseHandle( hReadPipe );
CloseHandle( hWritePipe );
GETOUT:
if ( false == bCanContinue )
{
// errors were encountered during setup phase
// attempt to get more errors out of the system
//
LPVOID pvMsgBuffer;
DWORD dwErrCode = GetLastError();
char achMsg[ 255 ];
if ( 0 != dwErrCode )
{
FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwErrCode,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
(LPTSTR) &pvMsgBuffer,
0,
NULL );
sprintf( achMsg, "Error: '%d' was returned from the system\r\n", dwErrCode );
roOutput.push_back( std::string( achMsg ) );
if ( strlen( (const char*)pvMsgBuffer ) )
{
roOutput.push_back( std::string( (const char*)pvMsgBuffer ) );
}
LocalFree( pvMsgBuffer );
}
}
SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_NORMAL );
return( bAbleToStartProcess );
}
/* end of function "StartAndWaitForProcess" */
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -