home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / odbc / admndemo / errcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-06  |  5.6 KB  |  160 lines

  1. //*---------------------------------------------------------------------------------
  2. //|  ODBC System Administrator
  3. //|
  4. //|  This code is furnished on an as-is basis as part of the ODBC SDK and is
  5. //|  intended for example purposes only.
  6. //|
  7. //*---------------------------------------------------------------------------------
  8. #include "errcheck.h"
  9. #include "standard.h"
  10. #include "strings.h"
  11.  
  12.  
  13. //*---------------------------------------------------------------------------------
  14. //|   Global variables
  15. //*---------------------------------------------------------------------------------
  16. char        szErrOut[100];
  17.  
  18. dCSEG(char) szErrTitle[]                  =  "Error!";
  19. dCSEG(char) szError[]                     =  "Error: %s,  File: %s, Line: %d";
  20. dCSEG(char) szOutOfMemory[]               =  "Memory levels are very low.  Please exit other applications and try your request again.";
  21. dCSEG(char) szInvalidParms[]              =  "Invalid parameters";
  22. dCSEG(char) szRegisterClassFailed[]       =  "Register class failed";
  23.  
  24.  
  25. //*------------------------------------------------------------------------
  26. //| GetSQLState:
  27. //|     Parameters:
  28. //|         handletype     - Type of handle (env, conn, stmt, descr)
  29. //|         handle         - Handle used in last ODBC call
  30. //|         psMsgNum       - Pointer to variable storing message number
  31. //|         szState        - Return sqlstate
  32. //|         szNative       - Native return code (driver specific)
  33. //|         szMessage      - Return message
  34. //*------------------------------------------------------------------------
  35. LPSTR GetSQLState(SWORD handletype, SQLHANDLE handle, SWORD *psMsgNum,
  36.                   LPSTR szState, SDWORD FAR * pfNative, LPSTR szMessage)
  37. {
  38.    RETCODE  rc;
  39.    SWORD    cb;
  40.  
  41.    rc = SQLGetDiagRec(handletype, handle, (*psMsgNum)++,
  42.                       szState, pfNative,
  43.                       szMessage, RTN_MSG_SIZE, &cb);
  44.    if(rc == SQL_NO_DATA || rc == SQL_ERROR)
  45.       return NULL;
  46.    else
  47.       return szState;
  48. }
  49.  
  50.  
  51.  
  52. //*------------------------------------------------------------------------
  53. //| DoPostError:
  54. //|   This function will post an error message to standard output, whereever
  55. //|      that should be.
  56. //| Parms:
  57. //|   in       szErr                Error message
  58. //|   in       szFile               File name
  59. //|   in       cbLine               Line number
  60. //| Returns:
  61. //|   Nothing.
  62. //*---------------------------------------------------------------------------------
  63. void DoPostError(LPSTR szErr, LPSTR szFile, int cbLine)
  64. {
  65.    wsprintf(szErrOut, szError, (LPSTR)szErr, szFile, cbLine);
  66.    MessageBox(NULL, szErrOut, szErrTitle, MB_OK);
  67. }
  68.  
  69.  
  70.  
  71. //*------------------------------------------------------------------------
  72. //| PrintErrors:
  73. //|     Print out all relevant errors.
  74. //|         ci          -  Pointer to client information
  75. //|         handletype  -  Which handle has relevant error information
  76. //*------------------------------------------------------------------------
  77. void PrintErrors(CHILDINFO FAR * ci, SWORD handletype)
  78. {
  79.    SQLHANDLE handle;
  80.  
  81.    switch (handletype) {
  82.      case SQL_HANDLE_ENV:
  83.       handle = (SQLHANDLE) ci->henv;
  84.       break;
  85.  
  86.      case SQL_HANDLE_DBC:
  87.       handle = (SQLHANDLE) ci->hdbc;
  88.       break;
  89.  
  90.      case SQL_HANDLE_STMT:
  91.       handle = (SQLHANDLE) ci->hstmt;
  92.       break;
  93.    }
  94.  
  95.    DisplayErrors(ci->hwndOut, (LPSTR)szErrTitle, SQL_HANDLE_ENV, ci->henv);
  96.    if(!ci->hwndOut)
  97.       PrintErrorsHwnd(ci->hwndOut, handletype, handle);
  98. }
  99.  
  100.  
  101. //*------------------------------------------------------------------------
  102. //| PrintErrorsHwnd:
  103. //|   Does the actual work.  Needed as separate function for those
  104. //|   function which are not woking directly with a ci struct.
  105. //| Parms:
  106. //|   hwnd        Output window
  107. //|   henv        Environment handle
  108. //|   hdbc        Connection handle
  109. //|   hstmt       Statement handle
  110. //*------------------------------------------------------------------------
  111. void PrintErrorsHwnd(HWND hwnd, SWORD handletype, SQLHANDLE handle)
  112. {
  113.    SWORD    sMsgNum = 0;
  114.    char     szState[7]="";
  115.    char     szMessage[RTN_MSG_SIZE];
  116.    SDWORD   pfNative=0;
  117.  
  118.    while(GetSQLState(handletype, handle, &sMsgNum,
  119.                      szState, &pfNative, szMessage) != NULL)
  120.       szWrite(hwnd,
  121.               GetidsString(idsErrorString, szErrOut, sizeof(szErrOut)),
  122.               (LPSTR)szState,
  123.               (LPSTR)szMessage);
  124. }
  125.  
  126.  
  127. //*------------------------------------------------------------------------
  128. //| DisplayErrors:
  129. //|   This will take all of the errors from the ODBC handles and display
  130. //|   them using message box.  This is usually done when there is no
  131. //|   output window to write them to.
  132. //| Parms:
  133. //|   hwnd        Window handle to own the message box
  134. //|   title       The title for the message box
  135. //|   henv        Environment handle to look on
  136. //|   hdbc        Connection handle to look at
  137. //|   hstmt       Statement handle to look at
  138. //| Returns:
  139. //|   Nothing
  140. //*------------------------------------------------------------------------
  141. void DisplayErrors(HWND hwnd, LPSTR title, SWORD handletype, SQLHANDLE handle)
  142. {
  143.    SWORD    sMsgNum = 0;
  144.    char     szState[7]="";
  145.    char     szMessage[RTN_MSG_SIZE];
  146.    SDWORD   pfNative=0;
  147.  
  148.    while(GetSQLState(handletype, handle, &sMsgNum,
  149.                      szState, &pfNative, szMessage) != NULL)
  150.     {
  151.     if(  szMessageBox((hwnd) ? hwnd : GetActiveWindow(),
  152.                    MB_ICONEXCLAMATION,
  153.                    title,
  154.                    GetidsString(idsMsgErrorString, szErrOut, sizeof(szErrOut)),
  155.                    (LPSTR)szState,
  156.                    (LPSTR)szMessage) )
  157.                          break;
  158.     }
  159. }
  160.