#ifndef WIN32UTILS_H #define WIN32UTILS_H /////////////////////////////////////////////////////////////////// // // // Win32utils.h - 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 // // // /////////////////////////////////////////////////////////////////// /* Module Operations: ================== This module provides a few helper functions for Win32 programming. Public Interface: ================= initializeUtils(); // call before using safeOut(...) cleanUpUtils(); // call after finishing with safeOut(...) // Next two functions show MessageBoxs showCurrentThreadID("WinMain thread ID is "); showSysError("error is: ",GetLastError()); trace("sent ",numBytes," bytes"); // show annotated value (template) safeOut("my message"); // exclusive access to standard output errorOut("can't connect"); // same as safeOut, but then quit int x = Min(x,10); // find minimum of two values (template) string s = makeString("the value is ",v); // string from annotated value (template) */ /////////////////////////////////////////////////////////////////// // build process // /////////////////////////////////////////////////////////////////// // required files: // // Win32utils.h, Win32utils.cpp // // // // building in IDE: // // - build Win32 Application project // // - add these files // // - compile, line, and run // /////////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 1.0 : 06 Nov 99 - first release */ /////////////////////////////////////////////////////////////////// // // // Note: // // template function bodies must be in header file // // non-template function bodies must be in implementation file // // // /////////////////////////////////////////////////////////////////// // #include #include #include #include /////////////////////////////////////////////////////////////////// // declarations // /////////////////////////////////////////////////////////////////// class trace { public: enum level { debugging, critical, fatal, noChange }; trace(level lv); virtual ~trace(); level set(level lv); std::ostream* set(std::ostream *outStream); void out(const std::string &msg, level lv=noChange); protected: std::ostream *_pOutStream; level _lv; CRITICAL_SECTION csOut; }; //----< change level of trace output >------------------------- inline trace::level trace::set(level lv) { level temp = _lv; _lv = lv; return temp; } //----< change pointer to output stream >---------------------- inline std::ostream* trace::set(std::ostream *pOutStream) { std::ostream *temp = _pOutStream; _pOutStream = pOutStream; return temp; } void showCurrentThreadID(HWND hwnd=NULL, const char *Msg=NULL); void showSysError(const char *pMsg, DWORD errCode); void safeOut(const char *s); void errorOut(const char *s, bool quit=true); template T Min(T t1, T t2) { return (t1 < t2 ? t1 : t2); } //----< make string from annotated value >--------------------- template std::string makeString(const char *msg, const T &t) { std::ostrstream format; if(!format.good()) { showSysError("construct ostrstream failed in makeString",GetLastError()); exit(1); } if(msg != NULL) format << msg; format << t; format << ends; return format.str(); } //----< trace function >--------------------------------------- template void tracer(const char *s1,T t, const char *s2) { ostrstream format; format << "\n " << s1 << t << s2 << "\n" << ends; safeOut(format.str()); } //----< tracer function >-------------------------------------- typedef const char* str; template void tracer(str s1, T t, str s2, U u) { string temp = makeString(s1,t); temp += s2; trace(temp.c_str(),u,""); } #endif