/////////////////////////////////////////////////////////////////// // // // Win32utils.cpp - useful helper functions for Win32 API // // ver 1.0 // // // // Language: Visual C, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, Win NT 4.0 // // Application: CSE691 Project #1 // // Author: Jim Fawcett, Instructor, CSE691, Fall '99 // // CST 2-187, Syracuse Univ, (315) 443-3948 // // fawcett@ecs.syr.edu // // // /////////////////////////////////////////////////////////////////// #include #include #include "Win32utils.h" using namespace std; //----< display messagebox with system error string >-------------- // // errCode input is usually the result of a call to GetLastError() // pMsg is a pointer to any C string and becomes the title of the box // void showSysError(const char *pMsg, DWORD errCode) { LPVOID lpMsgBuf; DWORD dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&lpMsgBuf), 0,NULL ); MessageBox(NULL,reinterpret_cast(lpMsgBuf),pMsg,MB_OK); LocalFree(lpMsgBuf); } //----< show current thread ID >----------------------------------- void showCurrentThreadID(HWND hwnd, const char *Msg) { DWORD cid = GetCurrentThreadId(); MessageBox( hwnd, makeString("thread ID = ",cid).c_str(), Msg, MB_OK ); } // //----< test stub >------------------------------------------------ #ifdef TEST_WIN32UTILS void main() { cout << "\n Testing Win32 Utility Functions " << "\n =================================\n"; showCurrentThreadID(NULL, "testing showCurrentThreadID(...)"); int x, y = 0; try { x = 1/y; } catch(...) { cout << "\n caught exception thrown by system on divide by zero\n"; }; showSysError("testing showSysError(...)",GetLastError()); HANDLE h = 0; // invalid handle to file char lpBuff[1]; // read buffer DWORD got; // bytes read ReadFile(h,lpBuff,1,&got,NULL); showSysError("testing showSysError",GetLastError()); cout << "\n\n testing makeString(char*,_int64)\n " << makeString(" error code = ",GetLastError()); cout << "\n\n testing makeString(char*,double)\n " << makeString(" small double precision number = ",3.45); cout << "\n\n testing makeString(double,char*)\n " << makeString(" large double precision number = ",3.456789e+15); cout << "\n\n"; } #endif