home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / dbping.pak / DBPING.C next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  16.3 KB  |  501 lines

  1. // BDE - (C) Copyright 1994 by Borland International
  2.  
  3. #include <windows.h>
  4. #include <ctl3d.h>
  5. #include <idapi.h>
  6. #include <malloc.h>
  7. #include <string.h>
  8. #include "dbping.h"
  9.  
  10. // Declaration of global data
  11. HINSTANCE     hInst;
  12. HWND          hMainWnd;
  13. HCURSOR       hArrow, hWait;
  14.  
  15. // Function prototypes
  16. static BOOL InitApp(int nCmdShow);
  17. void reset_connect_dialog(HWND hWnd, BOOL bFirstReset, BOOL bResetAll);
  18. void try_to_connect(HWND hWnd);
  19. long FAR PASCAL _export MainWndProc(HWND, UINT, UINT, LONG);
  20. BOOL FAR PASCAL _export About(HWND, WORD, WORD, LONG);
  21.  
  22. //===============================================================
  23. //  Name:   WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  24. //
  25. //  Input:  hInstance     - The handle that represents the applications
  26. //                          unique instance ID.
  27. //          hPrevInstance - Indicates if this is the first instance of
  28. //                          the app.
  29. //          lpCmdLine     - Command line parameters (up to the app to
  30. //                          parse).
  31. //          nCmdShow - TRUE = Show as non-icon application
  32. //
  33. //  Desc:   Application entry point. Init the app, main
  34. //          window, and global variables.
  35. //================================================================
  36. int PASCAL
  37. WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  38.          int nCmdShow)
  39. {
  40.     MSG msg;
  41.     DBIResult   rslt;   // IDAPI return values
  42.  
  43.     // Avoid warning re: Parameter is never used
  44.     lpCmdLine = lpCmdLine;
  45.  
  46.     // Register CTL3D
  47.     Ctl3dRegister(hInstance);
  48.     Ctl3dAutoSubclass(hInstance);
  49.     Ctl3dColorChange();
  50.  
  51.     // Make this a single instance program
  52.     if (hPrevInstance)
  53.     {
  54.         MessageBox(GetFocus(), "This application is already running!",
  55.                    "PING Utility", MB_OK);
  56.         return FALSE;
  57.     }
  58.  
  59.     // Init ODAPI (quit if failure occurs) 
  60.     SetCursor(hWait);
  61.     if ((rslt = DbiInit(NULL)) != DBIERR_NONE)
  62.     {
  63.         if (rslt == DBIERR_CANTFINDODAPI)
  64.         {
  65.             MessageBox(GetFocus(), "Init( ) failed - DLL's not found."
  66.                        "\r\nCheck your path.", "PING Utility", MB_OK);
  67.         }
  68.         else
  69.         {
  70.             MessageBox(GetFocus(), "Init( ) failed!", "PING Utility",
  71.                        MB_OK);
  72.         }
  73.         return FALSE;
  74.     }
  75.     // Enable trace info if the debugging layer is enabled.
  76.     DbiDebugLayerOptions(DEBUGON | OUTPUTTOFILE, "DBPING.INF");
  77.  
  78.     SetCursor(hArrow);
  79.  
  80.     // Start the application 
  81.     hInst = hInstance;
  82.     hWait = LoadCursor(NULL, IDC_WAIT);
  83.     hArrow = LoadCursor(NULL, IDC_ARROW);
  84.     if (InitApp(nCmdShow) == FALSE)
  85.     {
  86.         return FALSE;
  87.     }
  88.  
  89.     // Process all event driven messages...
  90.     while (GetMessage(&msg, NULL, NULL, NULL))
  91.     {
  92.         if (!IsDialogMessage(hMainWnd, &msg))
  93.         {
  94.             TranslateMessage(&msg);
  95.             DispatchMessage(&msg);
  96.         }
  97.     }
  98.  
  99.     // Clean up and return
  100.     SetCursor(hWait);
  101.     DbiDebugLayerOptions(0, NULL);
  102.     DbiExit();
  103.     DestroyWindow(hMainWnd);
  104.     SetCursor(hArrow);
  105.  
  106.     // Unregister CTL3D
  107.     Ctl3dUnregister(hInstance);
  108.  
  109.     return msg.wParam;
  110. }
  111.  
  112. //=================================================================
  113. //  Name:   InitApp(nCmdShow);
  114. //
  115. //  Input:  nCmdShow - Whether to show the window after it is created
  116. //
  117. //  Return: TRUE - Init worked
  118. //          FALSE - Init failed
  119. //
  120. //  Desc:   Create the application window (in this case a
  121. //          dialog).
  122. //================================================================
  123. static BOOL
  124. InitApp (int nCmdShow)
  125. {
  126.     WNDCLASS    wc;
  127.     BOOL        ret = TRUE;
  128.  
  129.     // Init the application & create the needed windows 
  130.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  131.     wc.lpfnWndProc   = MainWndProc;
  132.     wc.cbClsExtra    = 0;
  133.     wc.cbWndExtra    = DLGWINDOWEXTRA;
  134.     wc.hInstance     = hInst;
  135.     wc.hIcon         = LoadIcon(hInst, MAKEINTRESOURCE(1100));
  136.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  137.     wc.hbrBackground = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
  138.     wc.lpszMenuName  = NULL;
  139.     wc.lpszClassName = "ConnectClass";
  140.  
  141.     if(!RegisterClass(&wc))
  142.     {
  143.         MessageBox(NULL, "RegisterClass failed!",  "System Error",
  144.                    MB_OK | MB_ICONHAND);
  145.         return FALSE;
  146.     }
  147.  
  148.     hMainWnd = CreateDialog(hInst, "MainDialog", NULL, NULL);
  149.     if (hMainWnd == NULL)
  150.     {
  151.         MessageBox(NULL, "CreateDialog failed!",  "System Error",
  152.                    MB_OK | MB_ICONHAND);
  153.         return FALSE;
  154.     }
  155.     
  156.     ShowWindow(hMainWnd, nCmdShow);
  157.     reset_connect_dialog(hMainWnd, TRUE, TRUE);
  158.     SetFocus(GetDlgItem(hMainWnd, IDL_DRIVERS));
  159.  
  160.     return ret;
  161. }
  162.  
  163. //===============================================================
  164. //  Name:   reset_connect_dialog(hWnd, bFirstReset, bResetAll);
  165. //
  166. //  Input:  hWnd - The dialog handle
  167. //          bFirstReset - TRUE means to fill the driver listbox
  168. //          bResetAll - TRUE means to repaint the entire dialog
  169. //                      FALSE means to repaint alias information
  170. //
  171. //  Return: None.
  172. //
  173. //  Desc:   Reset the "connect to the database" dialog based
  174. //          on the current driver selected.
  175. //================================================================
  176. void
  177. reset_connect_dialog (HWND hWnd, BOOL bFirstReset, BOOL bResetAll)
  178. {
  179.     hDBICur     hList;
  180.     DBDesc      DbData;
  181.     pFLDDesc    pCfgInfo;
  182.     WORD        ListIndex;
  183.     pCHAR       pCfgRecBuf;
  184.     pCHAR       Driver;
  185.     pCHAR       Alias;
  186.     
  187.     // Allocate resources 
  188.     Alias = (pCHAR)malloc(DBIMAXSCFLDLEN);
  189.     Driver = (pCHAR)malloc(DBIMAXSCFLDLEN);
  190.     pCfgRecBuf = (pCHAR)malloc(DBIMAXSCRECSIZE);
  191.     pCfgInfo = (pFLDDesc) malloc(sizeof(FLDDesc) * DBIMAXSCFIELDS);
  192.  
  193.     if (bFirstReset)
  194.     {
  195.         // Fill the listbox with the drivers available 
  196.         SendDlgItemMessage(hWnd, IDL_DRIVERS, LB_RESETCONTENT, 0, 0);
  197.         if (DbiOpenDriverList(&hList) == DBIERR_NONE)
  198.         {
  199.             while (DbiGetNextRecord(hList, dbiNOLOCK, (pBYTE)Driver, NULL)
  200.                    == DBIERR_NONE)
  201.             {
  202.                 SendDlgItemMessage(hWnd, IDL_DRIVERS, LB_INSERTSTRING, 0,
  203.                                    (LPARAM) Driver);
  204.             }
  205.             DbiCloseCursor(&hList);
  206.         }
  207.  
  208.         // Select the first item of the list boxes 
  209.         SendDlgItemMessage(hWnd, IDL_DRIVERS, LB_SETCURSEL, 0, 0);
  210.     }
  211.  
  212.     // Get the driver currently selected 
  213.     ListIndex = (WORD) SendDlgItemMessage(hWnd, IDL_DRIVERS, LB_GETCURSEL,
  214.                                           0, 0);
  215.     SendDlgItemMessage(hWnd, IDL_DRIVERS, LB_GETTEXT, (WPARAM) ListIndex,
  216.                        (LPARAM) Driver);
  217.  
  218.     // Reset the driver to "STANDARD" if Paradox or dBASE 
  219.     if ((strcmp(Driver, szDBASE) == GOOD_STR_COMPARE) ||
  220.         (strcmp(Driver, szPARADOX) == GOOD_STR_COMPARE))
  221.     {
  222.         strcpy(Driver, "STANDARD");
  223.     }
  224.  
  225.     // Leave the alias list alone if not resetting the listbox
  226.     if (bResetAll)
  227.     {
  228.         // Reset info displayed (based on the driver type) 
  229.         if (strcmp(Driver, "STANDARD") == GOOD_STR_COMPARE)
  230.         {
  231.             // Remove the user name & password if alias is STANDARD 
  232.             ShowWindow(GetDlgItem(hWnd, IDE_PASSWORD), SW_HIDE);
  233.             ShowWindow(GetDlgItem(hWnd, IDT_PASSWORD_HDR), SW_HIDE);
  234.         }
  235.         else
  236.         {
  237.             // Make sure all is displayed 
  238.             ShowWindow(GetDlgItem(hWnd, IDE_PASSWORD), SW_SHOW);
  239.             ShowWindow(GetDlgItem(hWnd, IDT_PASSWORD_HDR), SW_SHOW);
  240.         }
  241.  
  242.         // Clear & fill the alias listbox 
  243.         SendDlgItemMessage(hWnd, IDL_ALIASES, LB_RESETCONTENT, 0, 0);
  244.         if (DbiOpenDatabaseList(&hList) == DBIERR_NONE)
  245.         {
  246.             // Scan the list of aliases for matching drivers 
  247.             while (DbiGetNextRecord(hList, dbiNOLOCK, (pBYTE) &DbData,
  248.                                     NULL) == DBIERR_NONE)
  249.             {
  250.                 if (strcmp(DbData.szDbType, Driver) == GOOD_STR_COMPARE)
  251.                 {
  252.                     SendDlgItemMessage(hWnd, IDL_ALIASES, LB_INSERTSTRING,
  253.                                        0, (LPARAM) DbData.szName);
  254.                 }
  255.             }
  256.             DbiCloseCursor(&hList);
  257.             SendDlgItemMessage(hWnd, IDL_ALIASES, LB_SETCURSEL, 0, 0);
  258.         }
  259.     }
  260.  
  261.     // Reset the connection results list box 
  262.     SendDlgItemMessage(hWnd, IDL_RESULTS, LB_RESETCONTENT, 0, 0);
  263.  
  264.     // Don't forget to free up resources 
  265.     free(Alias);
  266.     free(Driver);
  267.     free(pCfgRecBuf);
  268.     free((pCHAR) pCfgInfo);
  269. }
  270.  
  271. //===============================================================
  272. //  Name:   try_to_connect(hWnd);
  273. //
  274. //  Input:  hWnd - The dialog handle
  275. //
  276. //  Return: None.
  277. //
  278. //  Desc:   Try to connect to a database based on the data
  279. //          selected in the main window.
  280. //================================================================
  281. void
  282. try_to_connect (HWND hWnd)
  283. {
  284.     hDBIDb      hDb;
  285.     DBIResult   rslt;
  286.     DBIErrInfo  ErrInfo;
  287.     WORD        ListIndex;
  288.     CHAR        Msg[DBIMAXMSGLEN+1];
  289.     pCHAR       Driver;
  290.     pCHAR       Alias;
  291.     pCHAR       Password;
  292.  
  293.     // Init the strings 
  294.     SetCursor(hWait);
  295.     Alias = (pCHAR)malloc(DBIMAXSCFLDLEN);
  296.     Driver = (pCHAR)malloc(DBIMAXSCFLDLEN);
  297.     Password = (pCHAR)malloc(DBIMAXSCFLDLEN);
  298.     Alias[0] = '\0';
  299.     Driver[0] = '\0';
  300.     Password[0] = '\0';
  301.  
  302.     // Get the selected driver 
  303.     ListIndex = (WORD) SendDlgItemMessage(hWnd, IDL_DRIVERS, LB_GETCURSEL,
  304.                                           0, 0);
  305.     SendDlgItemMessage(hWnd, IDL_DRIVERS, LB_GETTEXT, (WPARAM) ListIndex,
  306.                        (LPARAM) Driver);
  307.  
  308.     // Reset the driver type to NULL if standard database 
  309.     if ((strcmp(Driver, szDBASE) == GOOD_STR_COMPARE) ||
  310.         (strcmp(Driver, szPARADOX) == GOOD_STR_COMPARE))
  311.     {
  312.         Driver[0] = '\0';
  313.     }
  314.     else        // SQL database...
  315.     {
  316.         // SQL database needs the alias & password 
  317.         ListIndex =
  318.             (WORD) SendDlgItemMessage(hWnd, IDL_ALIASES, LB_GETCURSEL, 0,
  319.                                       0);
  320.         SendDlgItemMessage(hWnd, IDL_ALIASES, LB_GETTEXT, (WPARAM)
  321.                            ListIndex, (LPARAM) Alias);
  322.         SendDlgItemMessage(hWnd, IDE_PASSWORD, WM_GETTEXT, 80,
  323.                            (LPARAM) Password);
  324.     }
  325.  
  326.     // Try to open the database 
  327.     hDb = NULL;
  328.     SendDlgItemMessage(hWnd, IDL_RESULTS, LB_RESETCONTENT, 0, 0);
  329.     rslt = DbiOpenDatabase(Alias, Driver, dbiREADWRITE, dbiOPENSHARED,
  330.                            Password, 0, NULL, NULL, &hDb);
  331.     if (rslt == DBIERR_NONE)
  332.     {
  333.         DbiCloseDatabase(&hDb);
  334.         SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  335.                            (LPARAM) "Connection is available!");
  336.     }
  337.     else
  338.     {
  339.         // Failed to connect 
  340.         DbiGetErrorInfo(TRUE, &ErrInfo);
  341.         SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  342.                            (LPARAM) "ERROR - Failed to connect");
  343.         wsprintf(Msg, "    Error Category = %d      Error Code = %d",
  344.                  ErrCat(rslt), ErrCode(rslt));
  345.         SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  346.                            (LPARAM)Msg);
  347.         if (ErrInfo.szErrCode[0] != '\0')
  348.         {
  349.             wsprintf(Msg, "     -> ErrCode: %s", ErrInfo.szErrCode);
  350.             SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  351.                                (LPARAM)Msg);
  352.         }
  353.         if (ErrInfo.szContext1[0] != '\0')
  354.         {
  355.             wsprintf(Msg, "     -> Context1: %s", ErrInfo.szContext1);
  356.             SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  357.                                (LPARAM)Msg);
  358.         }
  359.         if (ErrInfo.szContext2[0] != '\0')
  360.         {
  361.             wsprintf(Msg, "     -> Context2: %s", ErrInfo.szContext2);
  362.             SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  363.                                (LPARAM)Msg);
  364.         }
  365.         if (ErrInfo.szContext3[0] != '\0')
  366.         {
  367.             wsprintf(Msg, "     -> Context3: %s", ErrInfo.szContext3);
  368.             SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  369.                                (LPARAM)Msg);
  370.         }
  371.         if (ErrInfo.szContext4[0] != '\0')
  372.         {
  373.             wsprintf(Msg, "     -> Context4: %s", ErrInfo.szContext4);
  374.             SendDlgItemMessage(hWnd, IDL_RESULTS, LB_ADDSTRING, 0,
  375.                                (LPARAM)Msg);
  376.         }
  377.     }
  378.  
  379.     // Free up resources
  380.     free(Alias);
  381.     free(Driver);
  382.     free(Password);
  383.     SetCursor(hArrow);
  384. }
  385.  
  386. //==============================================================
  387. //  Name:   MainWndProc(hWnd, msg, wParam, lParam);
  388. //
  389. //  Desc:   This routine will process all messaged for
  390. //          the primary application window. Included in this are all
  391. //          menu commands.
  392. //==============================================================
  393. long FAR PASCAL _export
  394. MainWndProc (HWND hWnd, UINT msg, UINT wParam, LONG lParam)
  395. {
  396.     HWND    hFocus;
  397.     FARPROC lpTempProc;
  398.     INT32   ret = FALSE;
  399.     HWND    hwnd;
  400.  
  401.     switch (msg)
  402.     {
  403.         case WM_CREATE:
  404.             PostMessage(hWnd, WM_COMMAND, ID_SUBCLASS, NULL);
  405.             ShowWindow(hWnd, FALSE);
  406.             break;
  407.         case WM_CTLCOLOR:
  408.             return (long)(WORD)Ctl3dCtlColorEx(msg, wParam, lParam);
  409.         case WM_SYSCOLORCHANGE:
  410.             Ctl3dColorChange();
  411.             break;
  412.         case WM_SETFOCUS:
  413.             SetFocus(GetDlgItem(hWnd, IDL_DRIVERS));
  414.         case WM_COMMAND:
  415.             hFocus = GetFocus();
  416.             switch(wParam)
  417.             {
  418.                 case IDL_DRIVERS:
  419.                     if (HIWORD(lParam) == LBN_SELCHANGE)
  420.                     {
  421.                         reset_connect_dialog(hWnd, FALSE, TRUE);
  422.                     }
  423.                     break;
  424.                 case IDL_ALIASES:
  425.                     if (HIWORD(lParam) == LBN_SELCHANGE)
  426.                     {
  427.                         reset_connect_dialog(hWnd, FALSE, FALSE);
  428.                     }
  429.                     break;
  430.                 case IDM_ABOUT:
  431.                     lpTempProc = MakeProcInstance((FARPROC) About, hInst);
  432.                     DialogBox(hInst, "About", hMainWnd, (DLGPROC)lpTempProc);
  433.                     FreeProcInstance((FARPROC) lpTempProc);
  434.                     break;
  435.                 case IDOK:
  436.                     try_to_connect(hWnd);
  437.                     break;
  438.                 case ID_SUBCLASS:
  439.                     // Can't do this within WM_CREATE
  440.                     hwnd = GetWindow(hWnd, GW_CHILD);
  441.                     while (hwnd != NULL)
  442.                     {
  443.                         Ctl3dSubclassCtl(hwnd);
  444.                         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
  445.                     }
  446.                     // Set a width for the horrizontal scroll bar
  447.                     SendMessage(GetDlgItem(hWnd, IDL_RESULTS),
  448.                                 LB_SETHORIZONTALEXTENT,
  449.                                 2000, NULL);
  450.                     ShowWindow(hWnd, TRUE);
  451.                     break;
  452.                 case IDCANCEL:
  453.                     PostQuitMessage(0);
  454.                     break;
  455.                 default:
  456.                     ret = DefWindowProc(hWnd, msg, wParam, lParam);
  457.             break;
  458.         }
  459.         SetFocus(hFocus);
  460.         break;
  461.     case WM_DESTROY:
  462.             PostQuitMessage(0);
  463.             break;
  464.     default:
  465.             ret = DefWindowProc(hWnd, msg, wParam, lParam);
  466.         break;
  467.     }
  468.     return ret;
  469. }
  470.  
  471. //==============================================================
  472. //  Name:   About(hDlg, msg, wParam, lParam);
  473. //
  474. //  Desc:   This routine will process all I/O for the
  475. //          ABOUT dialog.
  476. //==============================================================
  477. BOOL FAR PASCAL _export
  478. About (HWND hDlg, WORD msg, WORD wParam, LONG lParam)
  479. {
  480.     int     ret = FALSE;
  481.  
  482.     
  483.     switch (msg)
  484.     {
  485.         case WM_COMMAND:
  486.         switch (wParam)
  487.         {
  488.         case IDOK:
  489.                 case IDCANCEL:
  490.                     // Avoid warning re: parameter not used
  491.                     lParam = lParam;
  492.                     EndDialog(hDlg, TRUE);
  493.                     ret = TRUE;
  494.                     break;
  495.         }
  496.         break;
  497.     }
  498.     return ret;
  499. }
  500.  
  501.