home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / ptk / i386 / sqltest3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  24.4 KB  |  706 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: SqlTest3.c
  4.         Copyright (C) 1988-1995 Microsoft Corp.
  5.  
  6.     PURPOSE: SqlTest sample Windows applications
  7.  
  8.     FUNCTIONS:
  9.  
  10.     WinMain() - calls initialization function, processes message loop
  11.     SqlTestInit() - initializes window data and registers window
  12.     SqlTestWndProc() - processes messages
  13.     AboutSQL() - processes messages for "About" dialog box
  14.     SelectSQL() - processes input of author name
  15.     ConnectSQL() - processes input of server name and connects to server
  16.  
  17.     COMMENTS:
  18.  
  19.     Windows can have several copies of your application running at the
  20.     same time.  The variable hInst keeps track of which instance this
  21.     application is so that processing will be to the correct window.
  22.  
  23.     You only need to initialize the application once.  After it is
  24.     initialized, all other copies of the application will use the same
  25.     window class, and do not need to be separately initialized.
  26.  
  27. ****************************************************************************/
  28.  
  29. #include "windows.h"            /* required for all Windows applications*/
  30. #define DBMSWIN                /* needed to define environment         */
  31. #include "stdio.h"
  32. #include "string.h"
  33. #include "sqlfront.h"            /* standard dblib include file        */
  34. #include "sqldb.h"            /* standard dblib include file        */
  35. #include "sqltest3.h"            /* specific to this program            */
  36.  
  37. PDBPROCESS dbproc = (PDBPROCESS)NULL;
  38.                     /* dbprocess pointer for dblib connection*/
  39. HANDLE hInst;                /* current instance                */
  40. HWND ghWnd;                /* global window handle for handlers    */
  41. HWND errhWnd;                /* global window handle for current error*/
  42.  
  43. /****************************************************************************
  44.  
  45.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  46.  
  47.     PURPOSE: calls initialization function, processes message loop
  48.  
  49.     COMMENTS:
  50.  
  51.     This will initialize the window class if it is the first time this
  52.     application is run.  It then creates the window, and processes the
  53.     message loop until a PostQuitMessage is received.  It exits the
  54.     application by returning the value passed by the PostQuitMessage.
  55.  
  56. ****************************************************************************/
  57.  
  58. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  59. {
  60.     HWND hWnd;                     /* window handle             */
  61.     MSG msg;                     /* message                 */
  62.  
  63.  
  64.     if (!hPrevInstance)            /* Has application been initialized? */
  65.         if (!SqlTestInit(hInstance))
  66.             return (NULL);        /* Exits if unable to initialize     */
  67.  
  68.     hInst = hInstance;            /* Saves the current instance         */
  69.  
  70.     hWnd = CreateWindow("SQL Test",          /* window class         */
  71.         "SQL Server Sample Windows Application",  /* window name         */
  72.         WS_OVERLAPPEDWINDOW,              /* window style         */
  73.         CW_USEDEFAULT,                  /* x position             */
  74.         CW_USEDEFAULT,                  /* y position             */
  75.         CW_USEDEFAULT,                  /* width             */
  76.         CW_USEDEFAULT,                  /* height             */
  77.         NULL,                      /* parent handle         */
  78.         NULL,                      /* menu or child ID         */
  79.         hInstance,                  /* instance             */
  80.         NULL);                      /* additional info         */
  81.  
  82.     if (!hWnd)                      /* Was the window created? */
  83.         return (NULL);
  84.  
  85.     ghWnd = hWnd;                  /* set global handle         */
  86.     errhWnd = hWnd;
  87.  
  88.     ShowWindow(hWnd, nCmdShow);              /* Shows the window         */
  89.     UpdateWindow(hWnd);                  /* Sends WM_PAINT message  */
  90.  
  91.     while (GetMessage(&msg,       /* message structure                 */
  92.         NULL,           /* handle of window receiving the message */
  93.         NULL,           /* lowest message to examine             */
  94.         NULL))           /* highest message to examine         */
  95.     {
  96.         TranslateMessage(&msg);       /* Translates virtual key codes         */
  97.         DispatchMessage(&msg);       /* Dispatches message to window         */
  98.     }
  99.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  100. }
  101.  
  102.  
  103. /****************************************************************************
  104.  
  105.     FUNCTION: SqlTestInit(HANDLE)
  106.  
  107.     PURPOSE: Initializes window data and registers window class
  108.  
  109.     COMMENTS:
  110.  
  111.     Sets up a structure to register the window class.  Structure includes
  112.     such information as what function will process messages, what cursor
  113.     and icon to use, etc.
  114.  
  115. ****************************************************************************/
  116.  
  117. BOOL SqlTestInit(HINSTANCE hInstance)
  118. {
  119.     HANDLE hMemory;                   /* handle to allocated memory */
  120.     PWNDCLASS pWndClass;               /* structure pointer         */
  121.     BOOL bSuccess;                   /* RegisterClass() result     */
  122.  
  123.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  124.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  125.  
  126.     pWndClass->style = NULL; /*CS_HREDRAW | CS_VREDRAW; */
  127.     pWndClass->lpfnWndProc = SqlTestWndProc;
  128.     pWndClass->hInstance = hInstance;
  129.     pWndClass->hIcon = LoadIcon(hInstance, "SQLITEST");
  130.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  131.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  132.     pWndClass->lpszMenuName = (LPSTR)"SQLTest";
  133.     pWndClass->lpszClassName = (LPSTR)"SQL Test";
  134.  
  135.     bSuccess = RegisterClass(pWndClass);
  136.  
  137.  
  138.     LocalUnlock(hMemory);                /* Unlocks the memory    */
  139.     LocalFree(hMemory);                    /* Returns it to Windows */
  140.     return (bSuccess);         /* Returns result of registering the window */
  141. }
  142.  
  143.  
  144. /****************************************************************************
  145.  
  146.     FUNCTION: SqlTestWndProc(HWND, unsigned, WORD, LONG)
  147.  
  148.     PURPOSE:  Processes messages
  149.  
  150.     MESSAGES:
  151.  
  152.     WM_SYSCOMMAND - system menu (About dialog box)
  153.     WM_CREATE     - create window
  154.     WM_DESTROY    - destroy window
  155.     WM_COMMAND    - application menus (Connect and Select dialog boxes
  156.  
  157.     COMMENTS:
  158.  
  159.     To process the ID_ABOUTSQL message, call MakeProcInstance() to get the
  160.     current instance address of the About() function.  Then call Dialog
  161.     box which will create the box according to the information in your
  162.     SqlTest.rc file and turn control over to the About() function.    When
  163.     it returns, free the intance address.
  164.     This same action will take place for the two menu items Connect and
  165.     Select.
  166.  
  167. ****************************************************************************/
  168.  
  169. long FAR PASCAL SqlTestWndProc(HWND hWnd, WORD message, WPARAM wParam, LPARAM lParam)
  170. {
  171.     FARPROC lpProcAbout;          /* pointer to the "About" function */
  172.     FARPROC lpProcSQL;              /* pointer to the Select/Connect   */
  173.                       /* functions                  */
  174.     HMENU hMenu;              /* handle to the System menu         */
  175.     static FARPROC lpdbwinMessageHandler; /* pointer to message handler         */
  176.     static FARPROC lpdbwinErrorHandler;   /* pointer to error handler         */
  177.  
  178.     switch (message)
  179.     {
  180.         case WM_SYSCOMMAND:        /* message: command from system menu */
  181.             if (wParam == ID_ABOUTSQL)
  182.             {
  183.                 lpProcAbout = MakeProcInstance(AboutSQL, hInst);
  184.  
  185.                 DialogBox(hInst,         /* current instance         */
  186.                     "ABOUTSQL",             /* resource to use         */
  187.                     hWnd,             /* parent handle         */
  188.                     lpProcAbout);         /* About() instance address */
  189.  
  190.                 FreeProcInstance(lpProcAbout);
  191.                 break;
  192.             }
  193.             else                /* Lets Windows process it         */
  194.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  195.  
  196.         case WM_CREATE:                /* message: window being created */
  197.  
  198.             /* Get the handle of the System menu */
  199.             hMenu = GetSystemMenu(hWnd, FALSE);
  200.  
  201.             /* Add a separator to the menu */
  202.             ChangeMenu(hMenu,                  /* menu handle         */
  203.                 NULL,                      /* menu item to change */
  204.                 NULL,                      /* new menu item         */
  205.                 NULL,                      /* menu identifier     */
  206.                 MF_APPEND | MF_SEPARATOR);          /* type of change         */
  207.  
  208.             /* Add new menu item to the System menu */
  209.             ChangeMenu(hMenu,                  /* menu handle         */
  210.                 NULL,                      /* menu item to change */
  211.                 "A&bout SQL Test...",              /* new menu item         */
  212.                 ID_ABOUTSQL,                  /* menu identifier     */
  213.                 MF_APPEND | MF_STRING);              /* type of change         */
  214.     
  215.             /* Now make the message and error    */
  216.             /* handler instances             */
  217.             dbinit();
  218.             lpdbwinMessageHandler =
  219.                 MakeProcInstance((FARPROC)dbwinMessageHandler, hInst);
  220.             lpdbwinErrorHandler =
  221.                 MakeProcInstance((FARPROC)dbwinErrorHandler, hInst);
  222.             
  223.             /* Install the instances into dblib */    
  224.             dbmsghandle(lpdbwinMessageHandler);
  225.             dberrhandle(lpdbwinErrorHandler);
  226.             break;
  227.     
  228.         case WM_COMMAND :            /* menu selections generate */
  229.             /* the WM_COMMAND message   */    
  230.             switch (wParam)            /* menu in WORD parameter   */
  231.             {
  232.                 case IDM_CONNECT :        /* connect to server        */
  233.                     lpProcSQL = MakeProcInstance(ConnectSQL, hInst);
  234.  
  235.                     DialogBox(hInst,        /* current instance         */
  236.                         "CONNECT",         /* resource to use         */
  237.                         hWnd,            /* parent handle         */
  238.                         lpProcSQL);         /* ConnectSQL() instance address */
  239.  
  240.                     FreeProcInstance(lpProcSQL);
  241.                     break;
  242.     
  243.                 case IDM_SELECT :        /* select an author        */
  244.                     lpProcSQL = MakeProcInstance(SelectSQL, hInst);
  245.  
  246.                     DialogBox(hInst,         /* current instance         */
  247.                         "SELECT",         /* resource to use         */
  248.                         hWnd,             /* parent handle         */
  249.                         lpProcSQL);         /* About() instance address */
  250.  
  251.                     FreeProcInstance(lpProcSQL);
  252.                     break;
  253.             }
  254.             break;
  255.     
  256.         case WM_DBRESULTS :            /* a select has been issued */
  257.             SqlTestProcessResults(hWnd);    /* process results        */
  258.             break;
  259.  
  260.         case WM_DESTROY:          /* message: window being destroyed */
  261.             dbexit();              /* free any active dbprocesses     */
  262.             FreeProcInstance(lpdbwinMessageHandler);    /* release handlers  */
  263.             FreeProcInstance(lpdbwinErrorHandler);
  264.             dbwinexit();
  265.             PostQuitMessage(0);
  266.             break;
  267.  
  268.         default:              /* Passes it on if unproccessed    */
  269.             return (DefWindowProc(hWnd, message, wParam, lParam));
  270.     }
  271.     return (NULL);
  272. }
  273.  
  274.  
  275. /****************************************************************************
  276.  
  277.     FUNCTION: AboutSQL(HWND, unsigned, WORD, LONG)
  278.  
  279.     PURPOSE:  Processes messages for "AboutSQL" dialog box
  280.  
  281.     MESSAGES:
  282.  
  283.     WM_INITDIALOG - initialize dialog box
  284.     WM_COMMAND    - Input received
  285.  
  286.     COMMENTS:
  287.  
  288.     No initialization is needed for this particular dialog box, but TRUE
  289.     must be returned to Windows.
  290.  
  291.     Wait for user to click on "Ok" button, then close the dialog box.
  292.  
  293. ****************************************************************************/
  294.  
  295. BOOL FAR PASCAL AboutSQL(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  296. {
  297.     switch (message)
  298.     {
  299.     case WM_INITDIALOG:           /* message: initialize dialog box */
  300.         return (TRUE);
  301.  
  302.     case WM_COMMAND:              /* message: received a command */
  303.         if (wParam == IDOK)
  304.         {          /* "OK" box selected?         */
  305.             EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  306.             return (TRUE);
  307.         }
  308.         break;
  309.     }
  310.     return (FALSE);                  /* Didn't process a message    */
  311. }
  312.  
  313.  
  314. /****************************************************************************
  315.  
  316.     FUNCTION: SelectSQL(HWND, WORD, WPARAM, LPARAM)
  317.  
  318.     PURPOSE:  Processes messages for "SelectSQL" dialog box
  319.  
  320.     MESSAGES:
  321.  
  322.     WM_INITDIALOG - initialize dialog box
  323.     WM_COMMAND    - Input received
  324.  
  325.     COMMENTS:
  326.  
  327.     No initialization is needed for this particular dialog box, but TRUE
  328.     must be returned to Windows.
  329.     
  330.     Let user input into edit control the name of an author (the select
  331.     IS case sensitive).  When user presses OK, format the select statement
  332.     then send it to the server and execute it via dbsqlexec(). If the
  333.     dbsqlexec() SUCCEED's post a WM_DBRESULTS message so the results
  334.     may be retrieved and processed.
  335.  
  336.     Wait for user to click on "Ok" button, then close the dialog box.
  337.  
  338. ****************************************************************************/
  339.  
  340. BOOL FAR PASCAL SelectSQL(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  341. {
  342.     char szSelectAuthor[41];          /* string for authors name        */
  343.     char szServerMess[45];          /* string for server response        */
  344.     char szAName[40];              /* format string for author        */
  345.     switch (message)
  346.     {
  347.         case WM_INITDIALOG:           /* message: initialize dialog box */
  348.             SendDlgItemMessage(hDlg,       /* limit input to 40 characters   */
  349.                 AUTHORNAME,EM_LIMITTEXT,40,0L);
  350.             return (TRUE);
  351.  
  352.         case WM_COMMAND:              /* message: received a command */
  353.             errhWnd = hDlg;
  354.             switch(wParam)
  355.             {
  356.                 case IDOK :              /* "OK" box selected?         */
  357.                     *szSelectAuthor = NULL;   /* Null author             */
  358.                 
  359.                     GetDlgItemText(hDlg,AUTHORNAME, /* get input name         */
  360.                         (LPSTR)szSelectAuthor,
  361.                         MAX_ANAME);
  362.                     if (dbproc == (PDBPROCESS)NULL) /* if not a valid process*/
  363.                     {
  364.                         /* No server to query        */
  365.                         MessageBox(hDlg,
  366.                             "No SQL Server Connected to Query",
  367.                             "SQL Test",MB_ICONHAND | MB_OK);
  368.                     }
  369.                     else
  370.                         if (*szSelectAuthor != NULL) /* if a name exists */
  371.                         {
  372.                             DBLOCKLIB();        /* lock down the library */
  373.                             /* format the select statement */
  374.                             dbcmd(dbproc,
  375.                                 (LPSTR)"select au_id, au_lname,"
  376.                                 "au_fname, phone, address, city, state, zip");
  377.                             dbcmd(dbproc, (LPSTR)" from authors");
  378.                             dbcmd(dbproc, (LPSTR)" where au_lname = ");
  379.                             sprintf(szAName,"'%s'",szSelectAuthor);
  380.                             dbcmd(dbproc,(LPSTR)szAName);
  381.                             if (dbsqlexec(dbproc) == FAIL)
  382.                             {
  383.                                 sprintf(szServerMess,    /* error, not in db */
  384.                                     "%s not found in database pubs",
  385.                                     szSelectAuthor);
  386.                                 MessageBox(hDlg,
  387.                                     (LPSTR)szServerMess,(LPSTR)"SQL Test",
  388.                                     MB_ICONHAND | MB_OK);
  389.                             }
  390.                             else    /* query SUCCEEDed so             */
  391.                             {    /* post message to process results    */
  392.                                 PostMessage(GetParent(hDlg),WM_DBRESULTS,0,0L);
  393.                             }
  394.                             DBUNLOCKLIB();        /* unlock library    */
  395.                         }
  396.                     EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  397.                     return (TRUE);
  398.                     break;
  399.                 
  400.                 case IDCANCEL :
  401.                     EndDialog(hDlg, NULL);          /* cancelled select */
  402.                     return(TRUE);
  403.                     break;
  404.         
  405.             }
  406.             break;
  407.     }
  408.     return (FALSE);                  /* Didn't process a message    */
  409. }
  410.  
  411.  
  412. /****************************************************************************
  413.  
  414.     FUNCTION: ConnectSQL(HWND, unsigned, WPARAM, LPARAM)
  415.  
  416.     PURPOSE:  Processes messages for "Connect" dialog box
  417.  
  418.     MESSAGES:
  419.  
  420.     WM_INITDIALOG - initialize dialog box
  421.     WM_COMMAND    - Input received
  422.  
  423.     COMMENTS:
  424.  
  425.     No initialization is needed for this particular dialog box, but TRUE
  426.     must be returned to Windows.
  427.  
  428.     Wait for user to click on "Ok" button, then close the dialog box.
  429.  
  430. ****************************************************************************/
  431.  
  432. BOOL FAR PASCAL ConnectSQL(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  433. {
  434.     char szSQLServer[31];
  435.     char szServerMess[81];
  436.     static PLOGINREC LoginRec;
  437.  
  438.     *szSQLServer = NULL;
  439.     switch (message)
  440.     {
  441.         case WM_INITDIALOG:           /* message: initialize dialog box*/
  442.             SendDlgItemMessage(hDlg,       /* limit input to 30 characters  */
  443.                 SQL_SERVER,EM_LIMITTEXT,30,0L);
  444.             return (TRUE);
  445.  
  446.         case WM_COMMAND:              /* message: received a command*/
  447.             errhWnd = hDlg;
  448.             switch(wParam)
  449.             {
  450.                 case IDOK :              /* "OK" box selected?        */
  451.                     GetDlgItemText(hDlg,SQL_SERVER,
  452.                         (LPSTR)szSQLServer,
  453.                         MAX_SERVERNAME); /* get Server name */
  454.                     if(*szSQLServer != NULL) /* was something input        */
  455.                     {
  456.                         DBLOCKLIB();        /* lock down library        */
  457.                         if (dbproc != (PDBPROCESS)NULL) /* if an active process close it */
  458.                             dbclose(dbproc);
  459.                         if ((LoginRec = dblogin()) != (PLOGINREC)NULL) /* get loginrec */
  460.                         {
  461.                             DBSETLUSER(LoginRec,(LPCSTR)"sa"); /* set user  */
  462.                             DBSETLVERSION(LoginRec,DBVER60); /* Request 6.0 server behavior */
  463.  
  464.                             /* now open the connection to server */
  465.                             if ((dbproc = dbopen(LoginRec,(LPCSTR)szSQLServer))
  466.                                 == (PDBPROCESS)NULL)
  467.                             {
  468.                                 /* if NULL couldn't connect    */
  469.                                 dbfreelogin(LoginRec);
  470.                             }
  471.                             else /* got connect so use the pubs database */
  472.                             {
  473.                                 dbuse(dbproc,(LPSTR)"pubs");
  474.                                 dbfreelogin(LoginRec);
  475.                             }
  476.                         }
  477.                         else /* memory allocation problem */
  478.                             MessageBox(hDlg, "Could not allocate Login Record","System Error", MB_ICONHAND | MB_OK);
  479.                         DBUNLOCKLIB(); /* done unlock library    */
  480.                     }
  481.                     EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  482.                     return (TRUE);
  483.                     break;
  484.                 
  485.                 case IDCANCEL :
  486.                     EndDialog(hDlg, NULL);
  487.                     return(TRUE);
  488.                     break;
  489.             }
  490.             break;
  491.     }
  492.     return (FALSE);                  /* Didn't process a message    */
  493. }
  494.  
  495.  
  496. /****************************************************************************
  497.  
  498.     FUNCTION: CheckForScroll(HWND, int, int, int)
  499.  
  500.     PURPOSE:  Check if next output line will be out of client area
  501.  
  502.     PARAMETERS:
  503.     hWnd - Handle to the window.
  504.     CurrentPosition - Current y coordinate for the line of
  505.         text just written to the client area.
  506.     Spacing - The height of the line (including the space
  507.         separating lines) of the text just written.
  508.         Length - The length of the line just written in device
  509.         units.
  510.  
  511.     RETURN:    Returns the Y coordinate for the next line of text.
  512.  
  513.     COMMENTS:
  514.  
  515.     Will determine if the next line of text will be out of the client
  516.     area.  If so will scroll the window for the next line.  Also validates
  517.     the current line of text so that a WM_PAINT will not clear it.
  518.  
  519. ****************************************************************************/
  520. int CheckForScroll(HWND hWnd, int CurrentPosition, int Spacing, int Length)
  521. {
  522.     RECT rect;                /* RECT structure for validation */
  523.     rect.top = CurrentPosition;     /* top of last line of text     */
  524.     rect.bottom = CurrentPosition+Spacing+1; /* bottom of last line     */
  525.     rect.left = 1;            /* left most column of line     */
  526.     rect.right = Length+1;        /* right most column of line     */
  527.     ValidateRect(hWnd,(LPRECT)&rect);   /* validate line so that it is   */
  528.         /* not blanked on next paint     */
  529.         
  530.     GetClientRect(hWnd,(LPRECT)&rect);    /* get rect for current client   */
  531.     if (CurrentPosition + (Spacing*2) > rect.bottom) /* will line fit     */
  532.     {
  533.         /* if not scroll window and      */
  534.         /* update client window         */
  535.         ScrollWindow(hWnd,0,-(Spacing+1),NULL,NULL);
  536.         UpdateWindow(hWnd);
  537.         return(CurrentPosition);
  538.     }
  539.     return(CurrentPosition+Spacing);
  540. }
  541.  
  542.  
  543. /****************************************************************************
  544.  
  545.     FUNCTION: SQLTestProcessResults(HWND)
  546.  
  547.     PURPOSE:  If a valid dbprocess is present process all results from pending
  548.         select statement, output each field to client area.  Whenever
  549.         a new line is written to client area it is checked to see if
  550.         the client area needs to be scrolled.
  551.  
  552.     PARAMETERS: hWnd - Handle to the window.
  553.  
  554.     RETURN:    Returns the Y coordinate for the next line of text.
  555.  
  556.     COMMENTS:
  557.     This function will bind the fields in the select statement
  558.     to local variables, format an output string then
  559.     write that string to the client area via TextOut.
  560.     It is called by the main message processing loop
  561.     SQLTestWndProc via the message WM_DBRESULTS.
  562.  
  563. ****************************************************************************/
  564. BOOL SqlTestProcessResults(HWND hWnd)
  565. {
  566.     HDC hDC;                /* display context         */
  567.     TEXTMETRIC tm;            /* text metric structure     */
  568.     char szId[12];            /* Author ID for binding     */
  569.     char szLastName[41];        /* Author last name for binding     */
  570.     char szFirstName[21];        /* Author first name for binding */
  571.     char szPhone[13];            /* Author phone for binding     */
  572.     char szAddress[41];            /* Author address for binding     */
  573.     char szCity[21];            /* Author city for binding     */
  574.     char szState[3];            /* Author state for binding     */
  575.     char szZip[6];            /* Author zipcode for binding     */
  576.     char szOutputString[81];        /* general output string     */
  577.     RETCODE result_code;        /* results code from dbresults     */
  578.     int Y;                /* Y coordinate for text output  */
  579.     int Spacing;            /* Spacing between lines     */
  580.     errhWnd = hWnd;
  581.  
  582.     hDC = GetDC(hWnd);            /* get display context         */
  583.     GetTextMetrics(hDC, (LPTEXTMETRIC)&tm); /* get font info         */
  584.     Spacing = tm.tmExternalLeading + tm.tmHeight; /* set up spacing     */
  585.     Y = 1;                /* start at line 1         */
  586.     if(dbproc == (PDBPROCESS)NULL)    /* if process null, no results     */
  587.     {
  588.         ReleaseDC(hWnd,hDC);        /* free resources and return     */
  589.         return(TRUE);
  590.     }
  591.     SendMessage(hWnd,WM_ERASEBKGND,hDC,0L); /* always erase background     */
  592.     UpdateWindow(hWnd);            /* force painting of window     */
  593.     DBLOCKLIB();            /* lock down library         */
  594.  
  595.     /* get all results from the query*/
  596.     while(((result_code = dbresults(dbproc)) != NO_MORE_RESULTS) && result_code != FAIL)
  597.     {
  598.         if(result_code == SUCCEED)    /* if results ready         */
  599.         {
  600.             /* Bind all data of interest     */
  601.             dbbind(dbproc,1,NTBSTRINGBIND, 12L, (LPSTR)szId);
  602.             dbbind(dbproc,2,NTBSTRINGBIND, 41L, (LPSTR)szLastName);
  603.             dbbind(dbproc,3,NTBSTRINGBIND, 21L, (LPSTR)szFirstName);
  604.             dbbind(dbproc,4,NTBSTRINGBIND, 13L, (LPSTR)szPhone);
  605.             dbbind(dbproc,5,NTBSTRINGBIND, 41L, (LPSTR)szAddress);
  606.             dbbind(dbproc,6,NTBSTRINGBIND, 21L, (LPSTR)szCity);
  607.             dbbind(dbproc,7,NTBSTRINGBIND, 3L, (LPSTR)szState);
  608.             dbbind(dbproc,8,NTBSTRINGBIND, 6L, (LPSTR)szZip);
  609.             while(dbnextrow(dbproc) != NO_MORE_ROWS) /* get all rows     */
  610.             {
  611.                 /* here we format each field and write it to client */
  612.                 /* area checking to see if the client area needs to */
  613.                 /* be scrolled after each line is written        */
  614.                 sprintf(szOutputString,"Author ID: %s",szId);
  615.                 TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  616.                 Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  617.  
  618.                 sprintf(szOutputString,"Last Name: %s",szLastName);
  619.                 TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  620.                 Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  621.  
  622.                 sprintf(szOutputString,"Address:   %s",szAddress);
  623.                 TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  624.                 Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  625.  
  626.                 sprintf(szOutputString,"City:      %s",szCity);
  627.                 TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  628.                 Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  629.  
  630.                 sprintf(szOutputString,"State:     %s",szState);
  631.                 TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  632.                 Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  633.  
  634.                 sprintf(szOutputString,"ZipCode:   %s",szZip);
  635.                 TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  636.                 Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  637.  
  638.                 sprintf(szOutputString,"Telephone: %s",szPhone);
  639.                 TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  640.                 Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  641.  
  642.                 Y = CheckForScroll(hWnd,Y,Spacing,0); /* add extra line     */
  643.                 /* after each results */
  644.             }
  645.         }
  646.     }
  647.  
  648.     DBUNLOCKLIB();                /* unlock library       */
  649.     ReleaseDC(hWnd,hDC);            /* free resource       */
  650.     return(TRUE);
  651. }
  652.  
  653.  
  654. /****************************************************************************
  655.  
  656.     FUNCTION: dbwinMessageHandler(PDBPROCESS, DBINT, DBSMALLINT, DBSMALLINT,
  657.         LPSTR)
  658.  
  659.     PURPOSE:  When the Data Server returns a message to dblib this function
  660.         will be called to process that message.  This function is
  661.         installed into dblib via MakeProcInstance.  It must be declared
  662.         as a FAR cdecl function, not as a FAR PASCAL function, unlike
  663.         other call back routines, as dblib conducts all of it's calls
  664.         in the cdecl fashion.  You must return 0 to dblib.
  665.  
  666.     RETURN:    Return 0
  667.  
  668.     COMMENTS:
  669.  
  670. ****************************************************************************/
  671.  
  672. int FAR __export dbwinMessageHandler(PDBPROCESS dbproc, DBINT msgno, DBSMALLINT msgstate, DBSMALLINT severity, LPSTR msgtext)
  673. {
  674.     MessageBox(errhWnd,msgtext,(LPSTR)"SQL DataServer Message",MB_OK);
  675.     return(0);
  676. }
  677.  
  678.  
  679. /****************************************************************************
  680.  
  681.     FUNCTION: dbwinErrorHandler(PDBPROCESS, int, int, int, LPSTR, LPSTR)
  682.  
  683.     PURPOSE:  When dblib returns an error message to the application this
  684.         function will be called to process that error.  This function is
  685.         installed into dblib via MakeProcInstance.  It must be declared
  686.         as a FAR cdecl function, not as a FAR PASCAL function, unlike
  687.         other call back routines, as dblib conducts all of it's calls
  688.         in the cdecl fashion.  You must return either INT_CANCEL,
  689.         INT_CONTINUE, or INT_EXIT to dblib.
  690.  
  691.     RETURN:    Return continuation code.
  692.  
  693.     COMMENTS:
  694.  
  695. ****************************************************************************/
  696.  
  697. int FAR __export dbwinErrorHandler(PDBPROCESS dbproc, int severity, int errno, int oserr, LPSTR dberrstr, LPSTR oserrstr)
  698. {
  699.     MessageBox(errhWnd,dberrstr,(LPSTR)"DB-LIBRARY error",MB_ICONHAND | MB_OK);
  700.  
  701.     if (oserr != DBNOERR)    /* os error    */
  702.         MessageBox(errhWnd,oserrstr,(LPSTR)"Operating-System error",MB_ICONHAND | MB_OK);
  703.  
  704.     return(INT_CANCEL);    /* cancel command */
  705. }
  706.