home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / odbcsdk / samples / crsrdemo / crsrdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-07  |  10.2 KB  |  364 lines

  1. /*--------------------------------------------------------------------------
  2.   Crsrdemo.C --- Cursors main file
  3.  
  4.   Description:
  5.           This sample is spread across four files, each named for the role
  6.         the contained functions play.  Each file header contains a brief
  7.         description of its purpose and the routines it contains.
  8.  
  9.         CRSRDEMO.C contains the standard functions used in a Windows program
  10.         (such as, WinMain) plus two functions shared between all the files.
  11.         These functions are:
  12.  
  13.            DoMessage       - Issue a message
  14.            EndInstance     - Clean up an instance of this program
  15.            InitApplication - Prepare the first instance of this program
  16.            InitInstance    - Prepare an instance of this program
  17.            ODBCError       - Retrieve and display an ODBC error
  18.            WinMain         - Main Windows entry point
  19.  
  20.   This code is furnished on an as-is basis as part of the ODBC SDK and is
  21.   intended for example purposes only.
  22.  
  23. --------------------------------------------------------------------------*/
  24.  
  25. /* Includes --------------------------------------------------------------*/
  26. #include    "headers.h"
  27.  
  28. #pragma warning(disable:4001)
  29. #define    INCL_GLOBAL
  30. #include    "resource.h"
  31. #include    "crsrdemo.h"
  32.  
  33.  
  34. // Prototypes --------------------------------------------------------------
  35. void INTFUNC EndInstance(void);
  36. BOOL INTFUNC InitApplication(void);
  37. BOOL INTFUNC InitInstance(int);
  38. int  INTFUNC WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  39.  
  40.  
  41. /* DoMessage ---------------------------------------------------------------
  42.     Description: Issue a message
  43. --------------------------------------------------------------------------*/
  44. void INTFUNC DoMessage(HWND hwnd, UINT id)
  45. {
  46.     char    sz[cbSTRLEN];
  47.  
  48.     LoadString(g_hinst, id, sz, sizeof(sz));
  49.     MessageBox(hwnd, sz, g_szTITLE, MB_ICONINFORMATION | MB_OK);
  50.     return;
  51. }
  52.  
  53.  
  54. /* EndInstance -------------------------------------------------------------
  55.     Description: Free instance related data
  56. --------------------------------------------------------------------------*/
  57. void INTFUNC EndInstance(void)
  58. {
  59.     if (g_hfontName)  DeleteObject(g_hfontName);
  60.     if (g_hfontData)  DeleteObject(g_hfontData);
  61.  
  62.     if (g_hbrWin)     DeleteObject(g_hbrWin);
  63.     if (g_hbrBtn)     DeleteObject(g_hbrBtn);
  64.     if (g_hbrScroll)  DeleteObject(g_hbrScroll);
  65.  
  66.     Ctl3dUnregister(g_hinst);
  67.     return;
  68. }
  69.  
  70.  
  71. /* InitApplication ---------------------------------------------------------
  72.     Description: Prepare application by registering window classes
  73. --------------------------------------------------------------------------*/
  74. BOOL INTFUNC InitApplication(void)
  75. {
  76.     WNDCLASS  wc;
  77.  
  78.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  79.     wc.lpfnWndProc   = FrameProc;
  80.     wc.cbClsExtra    = 0;
  81.     wc.cbWndExtra    = 0;
  82.     wc.hInstance     = g_hinst;
  83.     wc.hIcon         = LoadIcon(g_hinst, MAKEINTRESOURCE(IDR_MAIN));
  84.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  85.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
  86.     wc.lpszMenuName  = NULL;
  87.     wc.lpszClassName = szFRAMECLASS;
  88.  
  89.     if (!RegisterClass(&wc))
  90.         return FALSE;
  91.  
  92.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  93.     wc.lpfnWndProc   = ChildProc;
  94.     wc.cbClsExtra    = 0;
  95.     wc.cbWndExtra    = sizeof(LPCHILD);
  96.     wc.hInstance     = g_hinst;
  97.     wc.hIcon         = LoadIcon(g_hinst, MAKEINTRESOURCE(IDR_CHILD));
  98.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  99.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  100.     wc.lpszMenuName  = NULL;
  101.     wc.lpszClassName = szCHILDCLASS;
  102.  
  103.     if (!RegisterClass(&wc))
  104.         return FALSE;
  105.  
  106.     return TRUE;
  107. }
  108.  
  109.  
  110. /* InitInstance ------------------------------------------------------------
  111.     Description: Prepare instance by initializing global variables and
  112.                  creating main frame window
  113. --------------------------------------------------------------------------*/
  114. BOOL INTFUNC InitInstance(int nCmdShow)
  115. {
  116.     RECT        rc;
  117.     HDC            hdc;
  118.     HFONT        hfont;
  119.     TEXTMETRIC    tm;
  120.     char        sz[cbSTRLEN];
  121.     SIZE        size;
  122.  
  123.     // Initialize global variables
  124.     g_hwnd       =
  125.     g_hwndClient = NULL;
  126.  
  127.     g_haccel     = NULL;
  128.  
  129.     g_hmenuInit        =
  130.     g_hmenuInitWindow  =
  131.     g_hmenuFrame       =
  132.     g_hmenuFrameWindow =
  133.     g_hmenuChild       =
  134.     g_hmenuChildWindow = NULL;
  135.  
  136.     g_hfontName =
  137.     g_hfontData = NULL;
  138.  
  139.     g_hbrWin    =
  140.     g_hbrBtn    =
  141.     g_hbrScroll = NULL;
  142.  
  143.     g_henv = SQL_NULL_HENV; 
  144.     g_hdbc = SQL_NULL_HDBC;
  145.  
  146.     g_haccel = LoadAccelerators(g_hinst, MAKEINTRESOURCE(IDR_MAIN));
  147.  
  148.     g_hmenuInit  = LoadMenu(g_hinst, MAKEINTRESOURCE(IDR_INIT));
  149.     g_hmenuFrame = LoadMenu(g_hinst, MAKEINTRESOURCE(IDR_MAIN));
  150.     g_hmenuChild = LoadMenu(g_hinst, MAKEINTRESOURCE(IDR_CHILD));
  151.  
  152.     g_hmenuInitWindow  = GetSubMenu(g_hmenuInit,  IDM_WINDOWINIT);
  153.     g_hmenuFrameWindow = GetSubMenu(g_hmenuFrame, IDM_WINDOWFRAME);
  154.     g_hmenuChildWindow = GetSubMenu(g_hmenuChild, IDM_WINDOWCHILD);
  155.  
  156.     g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
  157.     g_cyHScroll = GetSystemMetrics(SM_CYHSCROLL);
  158.  
  159.     LoadString(g_hinst, IDS_TABLE,      g_szTable,      sizeof(g_szTable));
  160.     LoadString(g_hinst, IDR_MAIN,       g_szTITLE,      sizeof(g_szTITLE));
  161.     LoadString(g_hinst, IDS_NOROW,      g_szNoRow,      sizeof(g_szNoRow));
  162.     LoadString(g_hinst, IDS_ROWERROR,    g_szRowError,    sizeof(g_szRowError));
  163.     LoadString(g_hinst, IDS_NULL,       g_szNull,       sizeof(g_szNull));
  164.     LoadString(g_hinst, IDS_ROWDELETED, g_szRowDeleted, sizeof(g_szRowDeleted));
  165.     LoadString(g_hinst, IDS_UNKNOWN,    g_szUnknown,    sizeof(g_szUnknown));
  166.  
  167.     // Create main window in upper 3/4 of desktop
  168.     GetWindowRect(GetDesktopWindow(), &rc);
  169.  
  170.     g_hwnd = CreateWindow(szFRAMECLASS,
  171.                           g_szTITLE,
  172.                           WS_OVERLAPPEDWINDOW,
  173.                         rc.left,
  174.                         rc.top,
  175.                         rc.right - rc.left,
  176.                         ((rc.bottom - rc.top) / 4) * 3,
  177.                           HWND_DESKTOP,
  178.                           g_hmenuInit,
  179.                           g_hinst,
  180.                           NULL);
  181.     if (!g_hwnd)
  182.         return FALSE;
  183.  
  184.     // Create fonts used in painting child windows
  185.     hdc = GetDC(g_hwnd);
  186.  
  187.     g_hfontName = CreateFont((GetDeviceCaps(hdc, LOGPIXELSY) * cPOINTS) / 72,
  188.                             0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0,
  189.                             szFONT);
  190.  
  191.     hfont = SelectObject(hdc, g_hfontName);
  192.  
  193.     GetTextMetrics(hdc, &tm);
  194.     g_cx = tm.tmMaxCharWidth;
  195.     g_cy = tm.tmHeight + tm.tmInternalLeading;
  196.  
  197.     g_hfontData = CreateFont((GetDeviceCaps(hdc, LOGPIXELSY) * cPOINTS) / 72,
  198.                             0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0,
  199.                             szFONT);
  200.  
  201.     SelectObject(hdc, g_hfontData);
  202.  
  203.     // Determine font size characteristics
  204.     GetTextMetrics(hdc, &tm);
  205.     g_cx = max(g_cx, tm.tmMaxCharWidth);
  206.     g_cy = max(g_cy, tm.tmHeight + tm.tmInternalLeading);
  207.  
  208.     GetTextExtentPoint(hdc, szRECORD, lstrlen(szRECORD), &size);
  209.     g_cxRecord = size.cx;
  210.  
  211.     wsprintf(sz, szRECNUM, 999999);
  212.  
  213.     GetTextExtentPoint(hdc, sz, lstrlen(sz), &size);
  214.     g_cxRecnum = size.cx;
  215.  
  216.     SelectObject(hdc, hfont);
  217.  
  218.     ReleaseDC(g_hwnd, hdc);
  219.  
  220.     // Allocate brushes
  221.     g_hbrWin    = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  222.     g_hbrBtn    = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
  223.     g_hbrScroll = CreateSolidBrush(GetSysColor(COLOR_SCROLLBAR));
  224.  
  225.     // Allocate ODBC environment and connection handles
  226.     if (ENVError(g_hwnd, SQLAllocEnv(&g_henv)))
  227.         return FALSE;
  228.     if (ENVError(g_hwnd, SQLAllocConnect(g_henv, &g_hdbc)))
  229.         return FALSE;
  230.  
  231.     // Always use the cursor library
  232.     if (DBCError(g_hwnd, SQLSetConnectOption(    g_hdbc,
  233.                                         SQL_ODBC_CURSORS, 
  234.                                         SQL_CUR_USE_ODBC)))
  235.     {
  236.         return FALSE;
  237.     }
  238.  
  239.     
  240.     
  241.  
  242.     // Complete variable initialization
  243.     g_cbName          = 0;
  244.     g_fConnected      = FALSE;
  245.     g_fAsyncSupported = FALSE;
  246.     g_szDSN[0]        = '\0';
  247.     g_cChild          = 0;
  248.     g_cCursor         = 0;
  249.  
  250.     g_mrows      = 1000;
  251.  
  252.     g_hwndClient = GetWindow(g_hwnd, GW_CHILD);
  253.  
  254.     Ctl3dRegister(g_hinst);
  255. #ifndef WIN32
  256.     Ctl3dAutoSubclass(g_hinst);
  257. #endif
  258.  
  259.     // Set initial menu state
  260.     AdjustMenus();
  261.  
  262.     // Show frame window
  263.     ShowWindow(g_hwnd, nCmdShow);
  264.     UpdateWindow(g_hwnd);
  265.     return TRUE;
  266. }
  267.  
  268.  
  269. /* ODBCError ---------------------------------------------------------------
  270.     Description: Fetch and display an ODBC error message
  271.                  NOTE: SQL_NO_DATA_FOUND and SQL_STILL_EXECUTING are
  272.                        not considered errors
  273. --------------------------------------------------------------------------*/
  274. BOOL INTFUNC ODBCError(HWND hwnd, HENV henv, HDBC hdbc, HSTMT hstmt, RETCODE rc)
  275. {    
  276.     if (rc == SQL_SUCCESS)
  277.         return FALSE;
  278.         
  279.     if (rc == SQL_NO_DATA_FOUND) {
  280.         DoMessage(hwnd, IDS_NODATAFOUND);
  281.         return FALSE;
  282.     }
  283.     
  284.     if (rc == SQL_STILL_EXECUTING) {
  285.         DoMessage(hwnd, IDS_STILLEXEC);
  286.         return FALSE;
  287.     }
  288.     
  289.     {    SDWORD    fNative;
  290.         SWORD    cbError;
  291.         LPSTR    lpszFmt;
  292.         LPSTR    lpszSQLState;
  293.         LPSTR    lpszError;
  294.         LPSTR    lpsz;
  295.  
  296.         // Allocate storage
  297.         lpsz = AllocPtr(1024 + cbSTRLEN + 6 + SQL_MAX_MESSAGE_LENGTH);
  298.                              
  299.         lpszFmt      = lpsz + 1024;
  300.         lpszSQLState = lpszFmt + cbSTRLEN;
  301.         lpszError    = lpszSQLState + 6;
  302.         LoadString(g_hinst, IDS_MSGFMT, lpszFmt, cbSTRLEN);
  303.  
  304.         // Retrieve and display errors until there are no more
  305.         while (SQLError(henv, hdbc, hstmt,
  306.                         (UCHAR FAR *)lpszSQLState,
  307.                         &fNative,
  308.                         (UCHAR FAR *)lpszError,
  309.                         SQL_MAX_MESSAGE_LENGTH-1,
  310.                         &cbError) != SQL_NO_DATA_FOUND) {
  311.             if (lstrcmpi(lpszSQLState, szDATATRUNC)) {
  312.                 wsprintf(lpsz, lpszFmt, lpszSQLState, fNative, lpszError);
  313.  
  314.                 MessageBox(hwnd, lpsz, g_szTITLE,
  315.                     strncmp(lpszSQLState, "01", 2) ?
  316.                         MB_ICONSTOP | MB_OK : MB_ICONINFORMATION | MB_OK);
  317.             }
  318.         }
  319.     
  320.         // Free storage
  321.         FreePtr(lpsz);
  322.     }
  323.     
  324.     return (!SUCCESS(rc));
  325. }
  326.  
  327.  
  328. /* WinMain -----------------------------------------------------------------
  329.     Description: Standard WinMain function
  330. --------------------------------------------------------------------------*/
  331. int INTFUNC WinMain(
  332.                 HINSTANCE    hinstCur,
  333.                 HINSTANCE    hinstPrev,
  334.                 LPSTR        lpszCmdLine,
  335.                 int            nCmdShow)
  336. {
  337.     MSG        msg;
  338.  
  339.     UNREF_PARAM(lpszCmdLine);
  340.     g_hinst = hinstCur;
  341.  
  342.     if (!hinstPrev)
  343.         if (!InitApplication())
  344.             return (FALSE);
  345.  
  346.     if (!InitInstance(nCmdShow)) {
  347.         EndInstance();
  348.         return (FALSE);
  349.     }
  350.  
  351.     while (GetMessage(&msg, (HWND)NULL, (UINT)NULL, (UINT)NULL))
  352.         if (!TranslateMDISysAccel(g_hwndClient, &msg) &&
  353.             !TranslateAccelerator(g_hwnd, g_haccel, &msg) &&
  354.             (!(g_hwndChildDialog) || (!IsDialogMessage(g_hwndChildDialog, &msg))))
  355.          {
  356.             TranslateMessage(&msg);
  357.             DispatchMessage(&msg);
  358.         }
  359.  
  360.     EndInstance();
  361.  
  362.     return msg.wParam;
  363. }
  364.