home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32DBPING.PAK / DBPING.C next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  17.4 KB  |  553 lines

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