///////////////////////////////////////////////////////////////// // sysUtils.H - utility classes to support Win32 API // // ver 1.0 // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win 2000 Pro, SP 2 // // Application: CSE691/791 - SWM&A Demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////// #include #include "sysUtils.h" using namespace std; //----< get system error message string >---------------------- string sysError::getLastMsg() { // ask system what type of error occurred 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 ); _msg = lpBuffer; LocalFree(lpBuffer); return _msg; } // //----< get path where this process was spawned >-------------- string procInfo::getSpawnPath() { const int bufferSize = 256; char nameBuffer[bufferSize]; GetModuleFileName(NULL,nameBuffer,bufferSize); string temp = nameBuffer; int pos = temp.find_last_of('\\'); temp.resize(pos+1); return temp; } //---< get name of file from which this process was spawned >-- string procInfo::getFileName() { const int bufferSize = 256; char nameBuffer[bufferSize]; GetModuleFileName(NULL,nameBuffer,bufferSize); string temp = nameBuffer; int pos = temp.find_last_of('\\'); temp.erase(temp.begin(), temp.begin()+pos+1); return temp; } // //----< test stub >-------------------------------------------- #ifdef TEST_SYSUTILS #include #include void main() { cout << "\n Demonstrating sysUtils " << "\n ========================\n"; cout << "\n testing sysError message retrieval " << "\n ------------------------------------\n"; sysError myErr; cout << "\n last error message = " << myErr.getLastMsg(); ifstream in("doesNotExist"); if(!in.good()) cout << "\n last error message = " << myErr.getLastMsg() << "\n\n"; cout << "\n testing procInfo - my path and name evaluator " << "\n -----------------------------------------------\n"; procInfo pi; cout << "\n file path = " << pi.getSpawnPath() << endl; cout << "\n file name = " << pi.getFileName() << "\n\n\n"; cout << "\n testing TtoStr and StrToT converts " << "\n ------------------------------------\n"; TtoStr dblStr(3.1415927); cout << "\n string representation of PI is " << dblStr.convert(); StrToT strDbl(dblStr.convert()); cout << "\n double retrieved from string is " << strDbl.convert(); // Using temporary objects we can convert from a primitive type to // a string and vice versa in a single expression: cout << "\n string representation of int(-17) is " << TtoStr(-17).convert(); cout << "\n integer retrieved from string is " << StrToT("-17").convert(); // This looks a little weird, but, if you think about it for awhile // the syntax actually makes sense. At least the author thinks so. cout << "\n\n"; } #endif