///////////////////////////////////////////////////////////////////// // CreateUtils.h - Create a process, retrieve error messages // // // // Language: Visual C++, ver 7.0 // // Platform: Dell Dimension 8100, Windows 2000, SP2 // // Application: Tool to support converting web pages from // // XML/XSL to HTML // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// #include "stdafx.h" #include #include "CreateUtils.h" bool Process::Create(char *pCL) { // arguments for create process: STARTUPINFO startInfo; // establishes properties of child process PROCESS_INFORMATION procInfo; // holds info returned from CreateProcess LPCTSTR appName = NULL; // program to run LPTSTR commandLine = pCL; // main's args LPSECURITY_ATTRIBUTES procSec = NULL; // default security LPSECURITY_ATTRIBUTES thrdSec = NULL; // default security BOOL inheritHandles = FALSE; // don't inherit hndls // DWORD createFlags = CREATE_NEW_CONSOLE; // make a console DWORD createFlags = NULL; // don't make a console LPVOID environment = NULL; // caller's evironmt LPCTSTR currentPath = NULL; // default dir LPSTARTUPINFO pStartInfo = &startInfo; // set process style LPPROCESS_INFORMATION pPrInfo = &procInfo; // retrieve info // std::string buildTitle = std::string("child process: "); //+= pName; GetStartupInfo(&startInfo); startInfo.lpTitle = (char*)buildTitle.c_str(); startInfo.dwFlags = STARTF_USEPOSITION | STARTF_USESIZE; startInfo.dwX = 200; startInfo.dwY = 250; startInfo.dwXSize = 600; startInfo.dwYSize = 300; std::cout << "\n Attempting to start: " // << appName << "\n with arguments: " << commandLine << std::endl; BOOL OK = CreateProcess( appName,commandLine,procSec,thrdSec,inheritHandles, createFlags,environment,currentPath,pStartInfo,pPrInfo ); // wait for child process to terminate before terminating WaitForSingleObject(procInfo.hProcess,INFINITE); CloseHandle(procInfo.hThread); // parent needs no access to child thread CloseHandle(procInfo.hProcess); // parent needs no access to child process std::cout << "\n\n"; return OK; }; std::string SysErrors::GetLastErrorMsg() { DWORD errorCode = GetLastError(); // map errorCode into a system defined error string DWORD dwFlags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER; LPCVOID lpSource = NULL; DWORD dwMessageID = errorCode; DWORD dwLanguageId = MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US); LPSTR lpBuffer; DWORD nSize = 0; va_list *Arguments = NULL; FormatMessage( dwFlags,lpSource,dwMessageID,dwLanguageId, (LPTSTR)&lpBuffer,nSize,Arguments ); std::cout << "\n error: "; if(lpBuffer) std::cout << lpBuffer << "\n --- did you forget extension .exe?" << "\n\n"; return std::string(lpBuffer); }