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

  1. //*---------------------------------------------------------------------------------
  2. //| ODBC Sample Translation DLL and Tool
  3. //|
  4. //|  This code is furnished on an as-is basis as part of the ODBC SDK and is
  5. //|  intended for example purposes only.
  6. //|
  7. //|  Title: TRANSPEC.C
  8. //|
  9. //|  Purpose:
  10. //|        This program puts up a modal dialog which displays the current
  11. //|        libraries which are loaded on the system.  For TRANCHK.DLL,
  12. //|        the user is allowed to update the current options for the
  13. //|        loaded version.
  14. //|
  15. //|        Note that the function used for displaying DLLs are located in
  16. //|        TOOLHELP.DLL which is not available under Windows NT.  The 32-bit
  17. //|        version of this app therefore is not as interesting, but will still
  18. //|        allow access to the TRNSLCHK .ini options.
  19. //|
  20. //*-------------------------------------------------------------------------
  21. #define _DCL_OPT_STRS
  22.  
  23. #include <windows.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "options.h"
  27. #include "transpec.h"
  28.  
  29. #ifndef WIN32
  30. #include "W16MACRO.H"
  31. #include "toolhelp.h"
  32. #else
  33. #include <windowsx.h>
  34. #endif
  35.  
  36. VSZFile;
  37. #define NumItems(s) (sizeof(s) / sizeof(s[0]))
  38.  
  39.  
  40.  
  41. //------------------------------------------------------------------------
  42. // Globals
  43. //------------------------------------------------------------------------
  44. OPTIONS            optval;
  45. OPTIONS FAR *    opt=&optval;
  46. HINSTANCE         ghInst;
  47. HICON                hIcon;
  48. int                iHelpCnt=0;
  49.  
  50.  
  51. dCSEG(char)    szDLL[]                    = ".DLL";
  52. dCSEG(char) szVBX[]                    = ".VBX";
  53. dCSEG(char)    szud[]                    = "%u";
  54. dCSEG(char) szUnloadLibrary[]        = "Unload library %s?";
  55. dCSEG(char) szHelpFile[]            = "..\\HELP\\ODBCSMPL.HLP";
  56. dCSEG(char) szUNKNOWN[]                = "unknown";
  57.  
  58.  
  59. // 
  60. // Use following table on 32-bit to find processor type
  61. //
  62. #ifdef WIN32
  63. struct {
  64.     DWORD                dwType;                        // Processor flag
  65.     LPSTR                szPlatform;                    // Text name
  66.     } Platforms[] = {
  67.     PROCESSOR_INTEL_386,         "80386",
  68.     PROCESSOR_INTEL_486,            "80486",
  69.     PROCESSOR_INTEL_PENTIUM,    "Pentium",
  70.     PROCESSOR_INTEL_860,            "860",
  71.     PROCESSOR_MIPS_R2000,        "R2000",
  72.     PROCESSOR_MIPS_R3000,        "R3000",
  73.     PROCESSOR_MIPS_R4000,        "R4000",
  74.     PROCESSOR_ALPHA_21064,        "Alpha",
  75.     };
  76. #endif
  77.  
  78.  
  79.  
  80. //------------------------------------------------------------------------
  81. // Local function prototypes
  82. //------------------------------------------------------------------------
  83. int     WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  84. BOOL     WINAPI Init(HINSTANCE, HINSTANCE, LPSTR, int);
  85. int     WINAPI DoMain(HINSTANCE);
  86. BOOL     EXTFUN SpecWndProc(HWND, UINT, WPARAM, LPARAM);
  87. VOID     WINAPI SetVersionInfo(HWND hDlg);
  88. VOID     WINAPI DoRefreshStats(HWND hDlg);
  89. int    WINAPI RefreshLibraries(HWND hDlg);
  90. int    WINAPI GetHandleToLib(HWND hDlg, HINSTANCE FAR * hi);
  91. BOOL     WINAPI IsValidDLL(LPSTR instr);
  92. BOOL     WINAPI fUnloadWarning(HWND hwnd);
  93.  
  94.  
  95. //*------------------------------------------------------------------------
  96. //| WinMain:
  97. //|     Parameters:
  98. //|         hInstance     - Handle to current Data Segment
  99. //|         hPrevInstance - Handle to previous Data Segment (NULL if none)
  100. //|         lpszCmdLine   - Long pointer to command line info
  101. //|         nCmdShow      - Integer value specifying how to start app.,
  102. //|                            (Iconic [7] or Normal [1,5])
  103. //*------------------------------------------------------------------------
  104. int WINAPI WinMain (HINSTANCE hInstance,
  105.                     HINSTANCE hPrevInstance,
  106.                     LPSTR  lpszCmdLine,
  107.                     int    nCmdShow)
  108. {
  109. int nReturn;
  110.  
  111.     if (Init(hInstance, hPrevInstance,lpszCmdLine,nCmdShow)) 
  112.         nReturn = DoMain(hInstance);
  113.     return nReturn;
  114. }
  115.  
  116.  
  117.  
  118.  
  119. //*------------------------------------------------------------------------
  120. //| Init
  121. //|     Initialization for the program is done here:
  122. //|
  123. //*------------------------------------------------------------------------
  124. BOOL WINAPI Init(HINSTANCE hInstance,   HINSTANCE hPrevInstance,
  125.           LPSTR  lpszCmdLine, int    nCmdShow)
  126. {
  127.     HWND hwnd;
  128.  
  129.     // If it's already open, just maximize it or bring it to the top.
  130.     if(hPrevInstance) {
  131.         hwnd = FindWindow(NULL, szWinTitle);
  132.     if(!hwnd)
  133.         MessageBox(NULL, "Window not found by FindWindow", "FindWindow", MB_OK);
  134.         if(IsIconic(hwnd))
  135.             OpenIcon(hwnd);
  136.         else
  137.             BringWindowToTop(hwnd);
  138.         return FALSE;
  139.         }
  140.  
  141.     ghInst = hInstance;
  142.     hIcon = LoadIcon(hInstance, "BASEICON");
  143.  
  144.     return TRUE;
  145. }
  146.  
  147.  
  148.  
  149. //*------------------------------------------------------------------------
  150. //| DoMain:
  151. //|     This is the main loop for the application:
  152. //*------------------------------------------------------------------------
  153. int WINAPI DoMain(HINSTANCE hInstance)
  154. {
  155.     DLGPROC dlgproc;
  156.  
  157.     dlgproc = MakeProcInstance(SpecWndProc, hInstance);
  158.     if(-1 == DialogBox(hInstance, "TRANSPEC", NULL, dlgproc))
  159.             MessageBox(NULL, "Could not open dialog box.",
  160.             "Transpec", MB_ICONEXCLAMATION);
  161.     FreeProcInstance((FARPROC) dlgproc);    
  162.     return TRUE;
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. //*------------------------------------------------------------------------
  170. //| SpecDlgWndProc:
  171. //|     Handle the messages for the dialog proc.
  172. //*------------------------------------------------------------------------
  173. BOOL EXTFUN     SpecWndProc(HWND        hDlg,
  174.                                       UINT msg,
  175.                                      WPARAM        wParam,
  176.                                      LPARAM        lParam)
  177. {
  178.     switch(msg) {
  179.         // Initialize the default values for the dialog box, as well as
  180.         //    add valid values.
  181.         case WM_INITDIALOG:
  182.             {
  183.             opt = &optval;
  184.             GetIniInfo(opt, TRUE);
  185.             CenterDialog(hDlg);
  186.             SetVersionInfo(hDlg);
  187.             SendMessage(hDlg, USER_REINITDIALOG, 0, 0L);
  188.           }
  189.          return TRUE;
  190.          
  191.          
  192.         case WM_ACTIVATEAPP:
  193.         case USER_REINITDIALOG:
  194.             DoRefreshStats(hDlg);
  195.             return TRUE;
  196.  
  197.  
  198.  
  199. #ifndef WIN32
  200.         case USER_REFRESHLIBCOUNT:
  201.             {
  202.              HINSTANCE    hInst;
  203.              int            cnt;
  204.             char            tmpbuff[_SMALLBUFF];
  205.  
  206.              cnt = GetHandleToLib(hDlg, &hInst);
  207.             wsprintf(tmpbuff, szpd, cnt);
  208.             SetWindowText(GetDlgItem(hDlg, IDT_COUNT), tmpbuff);
  209.             wsprintf(tmpbuff, szud, hInst);
  210.             SetWindowText(GetDlgItem(hDlg, IDT_HMOD), tmpbuff);
  211.             }
  212.             return TRUE;
  213. #endif
  214.  
  215.  
  216.         case WM_COMMAND:
  217.             switch(GET_WM_COMMAND_ID(wParam, lParam)) {
  218.                 case IDOK:
  219.                     SendMessage(hDlg, USER_REFRESHLIBCOUNT, 0, 0L);
  220.                     return TRUE;
  221.  
  222.                 case IDB_CLOSE:
  223.                     WriteIniInfo(opt);
  224.                     SendMessage(hDlg, WM_CLOSE, 0, 0L);
  225.                     return TRUE;
  226.  
  227. #ifndef WIN32
  228.                 //----------------------------------------------------------
  229.                 //    Get the usage count of the current selection and
  230.                 //        call FreeLibrary until its gone.
  231.                 //----------------------------------------------------------
  232.                  case IDB_UNLOAD:
  233.                      {
  234.                      HINSTANCE        hInst;
  235.                      int                cnt, cursel;
  236.                      
  237.                     if(fUnloadWarning(hDlg)) {                // Make sure they know what they're doing
  238.                         cnt = GetHandleToLib(hDlg, &hInst);
  239.                         while(cnt--) 
  240.                             FreeLibrary(hInst);
  241.                         cursel = (int) SendDlgItemMessage(hDlg, IDL_LIBS, LB_GETCURSEL, 0, 0L);
  242.                         SendDlgItemMessage(hDlg, IDL_LIBS, LB_DELETESTRING,
  243.                                 (WPARAM)cursel--, 0L);
  244.                         SendDlgItemMessage(hDlg, IDL_LIBS, LB_SETCURSEL, 
  245.                                 (cursel >= 0) ? cursel : 0, 0L);
  246.                         SendMessage(hDlg, USER_REFRESHLIBCOUNT, 0, 0L);
  247.                         }
  248.                     }
  249.                     return TRUE;
  250. #endif
  251.  
  252.                 case IDB_OPTIONS:
  253.                     HandleOptions(hDlg, ghInst, opt);
  254.                     return TRUE;
  255.                     
  256.                 case IDB_HELP:
  257.                     iHelpCnt += WinHelp(hDlg,szHelpFile,HELP_CONTEXT,HLP_TRNSLCHK);
  258.                     return TRUE;
  259.  
  260.                 default:
  261.                     break;
  262.                 }
  263.  
  264. #ifndef WIN32
  265.             //----------------------------------------------------------------
  266.             //  If it wasn't a button, see if we are getting a notification
  267.             //        message that the user has changed selection.
  268.             //----------------------------------------------------------------
  269.             switch(GET_WM_COMMAND_CMD(wParam,lParam)) {
  270.                 case LBN_SELCHANGE:
  271.                     SendMessage(hDlg, USER_REFRESHLIBCOUNT, 0, 0L);
  272.                     return TRUE;
  273.             
  274.                 default:
  275.                     return FALSE;
  276.                 }
  277. #endif
  278.  
  279.             return TRUE;
  280.  
  281.  
  282.         case WM_CLOSE:
  283.             if(iHelpCnt)
  284.                 WinHelp(hDlg, szHelpFile, HELP_QUIT, 0L);
  285.             EndDialog(hDlg, FALSE);
  286.             return TRUE;
  287.  
  288.  
  289.         default:
  290.             return FALSE;
  291.         }
  292. }
  293.  
  294.  
  295.  
  296.  
  297. //*------------------------------------------------------------------------
  298. //| SetVersionInfo:
  299. //|    Retrieve info on this system and fill out dialog accordingly.
  300. //*------------------------------------------------------------------------
  301. VOID WINAPI SetVersionInfo(HWND hDlg)
  302. {
  303.     LPSTR                szCPU=(LPSTR)szUNKNOWN;
  304.     DWORD                dwVers;
  305.     WORD                wWinVer;
  306.     WORD                wPlatform;
  307.     char                szVersion[25];
  308.  
  309. #ifndef WIN32
  310.     DWORD                dwFlags;
  311. #else
  312.     SYSTEM_INFO        si;
  313.     int                dex;
  314. #endif
  315.  
  316.  
  317.  
  318.     //
  319.     // Display the version of Windows we're on
  320.     //
  321.     dwVers = GetVersion();
  322.     wWinVer = LOWORD(dwVers);
  323.     wPlatform = HIWORD(dwVers);
  324.  
  325.     wsprintf(szVersion, "Windows %d.%d", LOBYTE(wWinVer),
  326.                 HIBYTE(wWinVer));
  327. #ifdef WIN32
  328.     if(LOBYTE(wWinVer) <= 3) {
  329.         if(wPlatform & 0x8000)
  330.             lstrcat(szVersion, " (32s)");
  331.         else
  332.             lstrcat(szVersion, " (NT)");
  333.         }
  334. #endif
  335.     SetWindowText(GetDlgItem(hDlg, IDT_WINVER), szVersion);
  336.  
  337.  
  338.     //
  339.     // Now get the processor information
  340.     //
  341. #ifndef WIN32
  342.     dwFlags = GetWinFlags();
  343.     szCPU = (dwFlags & WF_CPU286) ? (LPSTR)"80286" :
  344.                 (dwFlags & WF_CPU386) ? (LPSTR)"80386" :
  345.                 (dwFlags & WF_CPU486) ? (LPSTR)"80486" :
  346.                             (LPSTR)szUNKNOWN;
  347. #else
  348.     GetSystemInfo(&si);
  349.     for(dex=0;  dex<NumItems(Platforms);  dex++)
  350.         if(si.dwProcessorType == Platforms[dex].dwType) {
  351.             szCPU = Platforms[dex].szPlatform;
  352.             break;
  353.             }
  354. #endif
  355.     SetWindowText(GetDlgItem(hDlg, IDT_CPU), szCPU);
  356. }
  357.  
  358.  
  359.  
  360.  
  361. //*------------------------------------------------------------------------
  362. //| DoRefreshStats:
  363. //|    Refresh the amount of available memory.
  364. //*------------------------------------------------------------------------
  365. VOID WINAPI DoRefreshStats(HWND hDlg)
  366. {
  367.     char                    szMem[_SMALLBUFF];
  368.     DWORD                    dwFree;
  369. #ifdef WIN32
  370.     MEMORYSTATUS        mst;
  371. #endif
  372.  
  373.     // Calculate free memory and display
  374. #ifndef WIN32
  375.     dwFree = GetFreeSpace(0) / 1024;
  376. #else
  377.     mst.dwLength = sizeof(mst);
  378.     GlobalMemoryStatus(&mst);
  379.     dwFree = mst.dwAvailVirtual / 1024;
  380. #endif
  381.     wsprintf((LPSTR)&szMem, "%lu", dwFree);
  382.     SetDlgItemText(hDlg, IDT_FREEMEM, (LPSTR)szMem);
  383.  
  384.  
  385. #ifndef WIN32
  386.     dwFree = GetFreeSystemResources(GFSR_SYSTEMRESOURCES);
  387.     _ultoa(dwFree, szMem, 10);
  388.     lstrcat(szMem, "%");
  389.     SetWindowText(GetDlgItem(hDlg, IDT_FREESYS), szMem);
  390.  
  391.     RefreshLibraries(hDlg);
  392. #endif
  393. }
  394.  
  395.  
  396. #ifndef WIN32
  397. //*------------------------------------------------------------------------
  398. //| RefreshLibraries:
  399. //|    This function enumerates the currently loaded libraries using the
  400. //|        ToolHelp functions.  Each is displayed in a list box.
  401. //*------------------------------------------------------------------------
  402. int WINAPI RefreshLibraries(HWND hDlg)
  403. {
  404.     MODULEENTRY md;
  405.     BOOL     flag;
  406.     int    count=0;
  407.  
  408.     _fmemset(&md, 0, sizeof(MODULEENTRY));
  409.     md.dwSize = sizeof(MODULEENTRY);
  410.  
  411.     // Kill all old entries.  Use ModuleFirst and ModuleNext to loop
  412.     //        through all of the loaded libraries.  For each one, add its
  413.     //        name to the listbox.
  414.     SendDlgItemMessage(hDlg, IDL_LIBS, LB_RESETCONTENT, 0, 0L);
  415.     flag = ModuleFirst(&md);
  416.     while(flag) {
  417.         if(IsValidDLL(md.szExePath)) {
  418.             ++count;
  419.             SendDlgItemMessage(hDlg, IDL_LIBS, LB_ADDSTRING,
  420.                             0, (LPARAM)(LPSTR)md.szExePath);
  421.             }
  422.         flag = ModuleNext(&md);
  423.         }
  424.     SendDlgItemMessage(hDlg, IDL_LIBS, LB_SETCURSEL, 0, 0L);
  425.     return count;
  426. }
  427.  
  428.  
  429.  
  430.  
  431.  
  432. //*------------------------------------------------------------------------
  433. //| GetHandleToLib:
  434. //|    This function gets the handle to the currently select module name
  435. //|        in the libraries list box.  It returns the load count of this
  436. //|        library.
  437. //*------------------------------------------------------------------------
  438. int WINAPI GetHandleToLib(HWND hDlg, HINSTANCE FAR * hi)
  439. {
  440.     MODULEENTRY     md;
  441.     int                count=0;
  442.     char                szModName[_MAX_PATH];
  443.     char                szFile[35];
  444.  
  445.     *szFile = '\0';
  446.     _fmemset(&md, 0, sizeof(MODULEENTRY));
  447.     md.dwSize = sizeof(MODULEENTRY);
  448.  
  449.     SendDlgItemMessage(hDlg, IDL_LIBS, LB_GETTEXT,
  450.             (WPARAM)SendDlgItemMessage(hDlg, IDL_LIBS, LB_GETCURSEL, 0, 0L),
  451.             (LPARAM)(LPSTR)szModName);
  452.     if(!*szModName)
  453.         return 0;
  454.         
  455.     // Get just the file name (no extension)
  456.     _splitpath(szModName, NULL, NULL, szFile, NULL);
  457.     if(!*szFile)
  458.         return 0;
  459.     
  460.     *hi = ModuleFindName(&md, szFile);
  461.  
  462.     return (*hi) ? md.wcUsage : 0;
  463. }
  464.  
  465.  
  466.  
  467.  
  468. //*------------------------------------------------------------------------
  469. //| IsValidDLL:
  470. //|    This function verifies that the the module name returned is a
  471. //|        .DLL file, since any module can be returned.
  472. //*------------------------------------------------------------------------
  473. BOOL WINAPI IsValidDLL(LPSTR instr)
  474. {
  475.     char        szext[5];
  476.  
  477.     _splitpath(instr, NULL, NULL, NULL, szext);
  478.     if(!lstrcmp(szext, szDLL) ||
  479.         !lstrcmp(szext, szVBX))
  480.         return TRUE;
  481.     else
  482.         return FALSE;
  483. }
  484.  
  485.  
  486.  
  487. //*------------------------------------------------------------------------
  488. //| WarnWndProc:
  489. //|     Handle the messages for the dialog proc.
  490. //*------------------------------------------------------------------------
  491. BOOL EXTFUN     WarnWndProc(HWND    hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  492. {
  493.     switch(msg) {
  494.         case WM_INITDIALOG:
  495.             CenterDialog(hDlg);
  496.             CheckDlgButton(hDlg, IDX_NEVERAGAIN, TRUE);
  497.          return TRUE;
  498.          
  499.  
  500.         case WM_COMMAND:
  501.             switch(GET_WM_COMMAND_ID(wParam, lParam)) {
  502.                 case IDOK:
  503.                     opt->fWarn = !IsDlgButtonChecked(hDlg, IDX_NEVERAGAIN);
  504.                     EndDialog(hDlg, IDOK);
  505.                     return TRUE;
  506.  
  507.                 case IDCANCEL:
  508.                     opt->fWarn = !IsDlgButtonChecked(hDlg, IDX_NEVERAGAIN);
  509.                     EndDialog(hDlg, IDCANCEL);
  510.                     return TRUE;
  511.                 }
  512.             break;
  513.         }
  514.     
  515.     return FALSE;
  516. }
  517.  
  518.  
  519. //*------------------------------------------------------------------------
  520. //| fUnloadWarning:
  521. //|    Ask user if they really want to unload the DLL.  We'll store a 
  522. //|    constant in the .ini file so they can ignore this message.
  523. //| Return TRUE if unload may continue
  524. //*------------------------------------------------------------------------
  525. BOOL WINAPI fUnloadWarning(HWND hwnd)
  526. {
  527.     DLGPROC     dlgproc;
  528.     BOOL        fRtn=TRUE;
  529.     int        iRtn;
  530.  
  531.     if(!opt->fWarn)                // No warning desired
  532.         return TRUE;
  533.  
  534.     dlgproc = MakeProcInstance(WarnWndProc, ghInst);
  535.     iRtn = DialogBox(ghInst, 
  536.             MAKEINTRESOURCE(IDD_WARNING), hwnd, dlgproc);
  537.             
  538.     if(iRtn == -1) {
  539.         MessageBox(NULL, "Could not open dialog box.",
  540.                 "IDD_WARNING", MB_ICONEXCLAMATION);
  541.         fRtn = FALSE;
  542.         }
  543.     else if(iRtn == IDCANCEL)
  544.         fRtn = FALSE;
  545.         
  546.     FreeProcInstance((FARPROC) dlgproc);    
  547.  
  548.     return fRtn;
  549. }
  550.  
  551. #endif
  552.  
  553.