home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_12 / 2n12020a < prev    next >
Text File  |  1991-06-06  |  7KB  |  275 lines

  1. #include "windows.h"
  2. #include "frontend.h"
  3. #include "xqlcalls.h"
  4.  
  5. /*  Main Window Process */
  6. long FAR PASCAL MainWndProc(HWND,unsigned,WORD,LONG);
  7. BOOL FAR PASCAL SQLDlgProc (HWND,unsigned,WORD,LONG);
  8.  
  9. void ExecSQL (void);
  10. int  FetchRecords (void);
  11.  
  12. char appName[] = "FRONTEND";
  13. char msgS [80];              /* Message box text */
  14. int status;       /* Returned status of XQL call */
  15.  
  16. int PASCAL WinMain (HANDLE hInstance,
  17.                     HANDLE hPrevInstance,
  18.                     LPSTR  lpszCmdLine,
  19.                     int    nCmdShow)
  20. {
  21.  
  22.  WNDCLASS MainWndClass;
  23.  MSG      msg;
  24.  HWND     hMainWnd;
  25.  
  26.  if (!hPrevInstance)
  27.   {
  28.     MainWndClass.style         = CS_HREDRAW |
  29.                                  CS_VREDRAW;
  30.     MainWndClass.lpfnWndProc   = MainWndProc;
  31.     MainWndClass.cbClsExtra    = 0;
  32.     MainWndClass.cbWndExtra    = 0;
  33.     MainWndClass.hInstance     = hInstance;
  34.     MainWndClass.hIcon         = LoadIcon (NULL,
  35.                                    IDI_APPLICATION);
  36.     MainWndClass.hCursor       = LoadCursor (NULL,
  37.                                          IDC_ARROW);
  38.     MainWndClass.hbrBackground = GetStockObject
  39.                                       (WHITE_BRUSH);
  40.     MainWndClass.lpszMenuName  = NULL;
  41.     MainWndClass.lpszClassName = appName;
  42.  
  43.     if (!RegisterClass (&MainWndClass))
  44.        return (FALSE);
  45.   }
  46.  arrowCursor = MainWndClass.hCursor;
  47.  waitCursor  = LoadCursor (NULL, IDC_WAIT);
  48.  
  49.  hMainWnd = CreateWindow (appName,
  50.               "FE APP",
  51.               WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  52.               GetSystemMetrics (SM_CXSCREEN) / 10,
  53.               GetSystemMetrics (SM_CXSCREEN) / 10,
  54.               3 * GetSystemMetrics (SM_CXSCREEN) / 4,
  55.               3 * GetSystemMetrics (SM_CXSCREEN) / 4,
  56.               NULL,
  57.               NULL,
  58.               hInstance,
  59.               NULL);
  60.  
  61.  ShowWindow (hMainWnd, nCmdShow);
  62.  UpdateWindow (hMainWnd);
  63.  
  64.  while (GetMessage (&msg, NULL, 0, 0))
  65.   {
  66.      TranslateMessage (&msg);
  67.      DispatchMessage (&msg);
  68.   }
  69.  return (msg.wParam);
  70. }
  71.  
  72. /*--------------------------------------------------*/
  73. long FAR PASCAL MainWndProc (HWND hWnd,
  74.                              unsigned message,
  75.                              WORD wParam, LONG lParam)
  76. {
  77.    static HANDLE hInstance;
  78.  
  79.    switch (message)
  80.     {
  81.       case WM_CREATE :
  82.            hDC = GetDC (hWnd);
  83.            SelectObject (hDC, GetStockObject
  84.                                (SYSTEM_FIXED_FONT));
  85.            ReleaseDC (hWnd, hDC);
  86.  
  87.            SetCursor (waitCursor);                         
  88.            status = XQLLogin (userid, password,
  89.                               ddpath, datapath,
  90.                               sReserved, iReserved);
  91.            SetCursor (arrowCursor);
  92.            if (status)
  93.             {
  94.                sprintf (msgS,
  95.                 "A login error occurred, status: %d",
  96.                 status);
  97.                MessageBox (NULL, msgS, " ERROR ",
  98.                         MB_OK | MB_ICONEXCLAMATION);
  99.             }
  100.            else
  101.             {
  102.                hInstance = ((LPCREATESTRUCT)
  103.                              lParam)->hInstance;
  104.                procAddr  = MakeProcInstance
  105.                              (SQLDlgProc, hInstance);
  106.                DialogBox (hInstance, "SQLBox",
  107.                                      hWnd, procAddr);
  108.                FreeProcInstance (procAddr);
  109.                XQLFree (cursorID);
  110.                XQLLogout ();
  111.                XQLStop ();
  112.             }
  113.  
  114.            SendMessage (hWnd, WM_CLOSE, 0, 0L);
  115.            return 0;
  116.  
  117.       case WM_CLOSE :
  118.            DestroyWindow (hWnd);
  119.            return 0;
  120.  
  121.       case WM_DESTROY :
  122.            PostQuitMessage (0);
  123.            return 0;
  124.  
  125.      }
  126.    return (DefWindowProc (hWnd, message,
  127.                           wParam, lParam));
  128.  }
  129.  
  130. /*--------------------------------------------------*/
  131. BOOL FAR PASCAL SQLDlgProc (HWND hDlg,
  132.                             unsigned message,
  133.                             WORD wParam, LONG lParam)
  134.  
  135. {
  136.  switch (message)
  137.   {
  138.    case WM_INITDIALOG :
  139.      SetFocus (GetDlgItem (hDlg, X_IN_WIN));
  140.      return (TRUE);
  141.  
  142.    case WM_COMMAND :
  143.      switch (wParam)
  144.       {
  145.        case X_SEND :
  146.  
  147.          GetDlgItemText (hDlg, X_IN_WIN, (LPSTR)
  148.                           statement, MAXSTATELEN-1);
  149.          SetCursor (waitCursor);
  150.          ExecSQL ();
  151.          SetCursor (arrowCursor);
  152.          if (status < 0)  /* Informative stat code */
  153.            {
  154.             sprintf (msgS,
  155.                    "Statement executed successfully");
  156.             SetDlgItemText (hDlg, X_OUT_WIN, msgS);
  157.            }
  158.  
  159.          else if (status > 0) /* Error! */
  160.            {
  161.             sprintf (msgS,
  162.                "Compile Error - status - %d", status);
  163.             SetDlgItemText (hDlg, X_OUT_WIN, msgS);
  164.            }
  165.  
  166.          else /* Operation was SELECT */
  167.            {
  168.             buffer = (char *) LocalAlloc
  169.                              (LPTR, MAXSTATELEN);
  170.             status = FetchRecords ();
  171.             SetDlgItemText (hDlg, X_OUT_WIN, "");
  172.             if (status)
  173.               {
  174.                sprintf (msgS,
  175.                  "Fetch Error - status - %d", status);
  176.                SetDlgItemText (hDlg, X_OUT_WIN, msgS);
  177.               }
  178.  
  179.             else
  180.               {
  181.                hDC = GetDC (hDlg);
  182.                SelectObject (hDC, GetStockObject
  183.                                  (SYSTEM_FIXED_FONT));
  184.                ReleaseDC (hDlg, hDC);
  185.                SetDlgItemText (hDlg, X_OUT_WIN,
  186.                                buffer);
  187.               }
  188.  
  189.             LocalFree ((LOCALHANDLE) buffer);
  190.  
  191.            }
  192.  
  193.            SetFocus (GetDlgItem (hDlg, X_IN_WIN));
  194.  
  195.            return (TRUE);
  196.  
  197.        case X_CLEAR :
  198.            SetDlgItemText (hDlg, X_IN_WIN,  "");
  199.            SetDlgItemText (hDlg, X_OUT_WIN, "");
  200.            SetFocus (GetDlgItem (hDlg, X_IN_WIN));
  201.            return (TRUE);
  202.  
  203.        case X_END_DLG:
  204.            EndDialog (hDlg, 0);
  205.            return (TRUE);
  206.       }
  207.    default :
  208.       break;
  209.   }
  210.  return (FALSE);
  211. }
  212.  
  213.  
  214. /*--------------------------------------------------*/
  215. void ExecSQL (void)
  216.  
  217. {
  218.    if (cursorID == -1)
  219.     {
  220.      status = XQLCursor ((LPWORD) &cursorID);
  221.      if (status)
  222.        return;
  223.     }
  224.  
  225.    statlen = strlen (statement);
  226.    status  = XQLCompile (cursorID, (LPWORD)
  227.                        &statlen, (LPBYTE) statement);
  228.  
  229.    return;
  230. }
  231.  
  232.  
  233. /*--------------------------------------------------*/
  234. int FetchRecords (void)
  235.  
  236. {
  237.   int option    = 1;
  238.   int ASCIIFlag = 1;
  239.   int spacing   = 2;
  240.  
  241.   int   count;
  242.   long  recLen, recCount;
  243.   int   bufSize;
  244.   char *p;
  245.  
  246.   bufSize  = MAXBUFLEN;
  247.   recCount = 10;
  248.  
  249.   status = XQLFetch (cursorID, option, &bufSize,
  250.                      buffer, &recCount,
  251.                      ASCIIFlag, spacing);
  252.  
  253.   if (status > 0)  /* Fetch was not successful */
  254.    {
  255.      LocalFree ((LOCALHANDLE) buffer);
  256.      return (status);
  257.    }
  258.  
  259.   p = buffer;
  260.   recLen = *(long *) p;  /* Get length */
  261.  
  262.   /* Make buffer printable */
  263.  
  264.   for (count = 0; count < recCount; count++)
  265.    {
  266.       (char) *p = (char) 13;
  267.       (char) *(p+1) = (char) 10;
  268.       p += (2 + recLen); 
  269.       recLen = *(long *) p;
  270.    }                                            
  271.   return (0);
  272. }
  273.  
  274.  
  275.