home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / wpj_mag / wpjv1n8.zip / GASH.ZIP / INSTANCE / TEST / TESTINS.C < prev    next >
C/C++ Source or Header  |  1993-08-16  |  6KB  |  237 lines

  1. /*
  2.  
  3. TESTINS.C - Tests Instance function
  4.             (C) 1993, Dennis CHUAH
  5.  
  6. */
  7.  
  8. #define STRICT
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include <toolhelp.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <instance.h>
  15. #include "res.h"
  16.  
  17.  
  18. /* Application specific messages */
  19. #define WM_GETMODULES        (WM_USER + 0x100)
  20. #define WM_SELCHANGED        (WM_USER + 0x101)
  21. #define WM_GETHINSTANCE        (WM_USER + 0x102)
  22.  
  23.  
  24. static char sBuffer[256];    // Temporary string storage
  25.  
  26.  
  27. BOOL CALLBACK DlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  28. static void _wm_getmodules (HWND hWnd);
  29. static void _wm_selchanged (HWND hWnd);
  30. static void _wm_gethinstance (HWND hWnd);
  31. static void _wm_command (HWND hWnd, int iCtrlID, WORD wCode);
  32.  
  33.  
  34. #pragma argsused
  35. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine,
  36.                     int nCmdShow)
  37.   {WNDCLASS wc;
  38.    HWND hWnd;
  39.    MSG msg;
  40.  
  41.    if (hPrev == NULL)
  42.      {/* Register dialog class */
  43.       wc.style = CS_HREDRAW | CS_VREDRAW;
  44.       wc.lpfnWndProc = DefDlgProc;
  45.       wc.cbClsExtra = 0;
  46.       wc.cbWndExtra = DLGWINDOWEXTRA;
  47.       wc.hInstance = hInstance;
  48.       wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (MAIN));
  49.       wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  50.       wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  51.       wc.lpszMenuName = NULL;
  52.       wc.lpszClassName = MAINCLASS;
  53.       RegisterClass (&wc);
  54.      }
  55.  
  56.    /* Create modeless dialog box */
  57.    hWnd = CreateDialog (hInstance, MAKEINTRESOURCE (MAIN), NULL, DlgProc);
  58.    ShowWindow (hWnd, nCmdShow);
  59.    UpdateWindow (hWnd);
  60.  
  61.    /* Message loop for modeless dialogbox */
  62.    while (GetMessage (&msg, NULL, 0, 0))
  63.      {if (!IsDialogMessage (hWnd, &msg))
  64.         {TranslateMessage (&msg);
  65.          DispatchMessage (&msg);
  66.         }
  67.      }
  68.  
  69.    return msg.wParam;
  70.   } // end WinMain
  71.  
  72.  
  73. /* Dialogbox procedure */
  74. BOOL CALLBACK DlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  75.   {switch (msg)
  76.      {case WM_INITDIALOG:
  77.          PostMessage (hWnd, WM_GETMODULES, 0, 0L);
  78.          break;
  79.  
  80.       case WM_GETMODULES:
  81.          _wm_getmodules (hWnd);
  82.          break;
  83.  
  84.       case WM_SELCHANGED:
  85.          _wm_selchanged (hWnd);
  86.          break;
  87.  
  88.       case WM_GETHINSTANCE:
  89.          _wm_gethinstance (hWnd);
  90.          break;
  91.  
  92.       case WM_COMMAND:
  93.          _wm_command (hWnd, wParam, HIWORD (lParam));
  94.          break;
  95.  
  96.       case WM_DESTROY:
  97.          PostQuitMessage (0);
  98.          break;
  99.  
  100.       default:
  101.          return FALSE;
  102.      } // end switch
  103.  
  104.    return TRUE;
  105.   } // end DlgProc
  106.  
  107.  
  108. // Fills the listbox with a list of modules in the system
  109. static void _wm_getmodules (HWND hWnd)
  110.   {HWND hList;
  111.    MODULEENTRY me;
  112.    BOOL retval;
  113.  
  114.    // Clear the listbox contents
  115.    hList = GetDlgItem (hWnd, IDLISTBOX);
  116.    (void) ListBox_ResetContent (hList);
  117.  
  118.    // Get the first module in the system list
  119.    me.dwSize = sizeof (MODULEENTRY);
  120.    retval = ModuleFirst (&me);
  121.  
  122.    // Get the rest of the modules
  123.    while (retval)
  124.      {sprintf (sBuffer, "hModule: 0x%04X, Name: %s", me.hModule, me.szModule);
  125.       (void) ListBox_AddString (hList, sBuffer);
  126.       retval = ModuleNext (&me);
  127.      }
  128.  
  129.    // Set the fist item in the listbox as the default
  130.    (void) ListBox_SetCurSel (hList, 0);
  131.    PostMessage (hWnd, WM_SELCHANGED, 0, 0L);
  132.   } // end _wm_getmodules
  133.  
  134.  
  135. // Fills the path and usage boxes with information pertaining to the
  136. //   currently selected module
  137. static void _wm_selchanged (HWND hWnd)
  138.   {MODULEENTRY me;
  139.    int selection;
  140.    HWND hList, hPath, hUsage;
  141.    HMODULE hModule;
  142.  
  143.    /* Get hModule from LISTBOX */
  144.    hList = GetDlgItem (hWnd, IDLISTBOX);
  145.    selection = ListBox_GetCurSel (hList);
  146.    if (selection == LB_ERR) return;
  147.    (void) ListBox_GetText (hList, selection, sBuffer);
  148.    sscanf (sBuffer + 11, "%04X", &hModule);
  149.    me.dwSize = sizeof (MODULEENTRY);
  150.  
  151.    // Get the HWND of other child controls
  152.    hPath = GetDlgItem (hWnd, IDPATH);
  153.    hUsage = GetDlgItem (hWnd, IDUSAGE);
  154.  
  155.    // Retrieve module information
  156.    if (ModuleFindHandle (&me, hModule) == NULL)
  157.      {Edit_SetText (hPath, "");
  158.       Edit_SetText (hUsage, "");
  159.      }
  160.    else
  161.      {Edit_SetText (hPath, me.szExePath);
  162.       sprintf (sBuffer, "%d", me.wcUsage);
  163.       Edit_SetText (hUsage, sBuffer);
  164.      } // endif
  165.   } // end _wm_selchanged
  166.  
  167.  
  168. // Displays the hInstance message box
  169. static void _wm_gethinstance (HWND hWnd)
  170.   {MODULEENTRY me;
  171.    int selection, len;
  172.    HWND hList;
  173.    HMODULE hModule;
  174.  
  175.    /* Get hModule from LISTBOX */
  176.    hList = GetDlgItem (hWnd, IDLISTBOX);
  177.    selection = ListBox_GetCurSel (hList);
  178.    if (selection == LB_ERR) return;
  179.    (void) ListBox_GetText (hList, selection, sBuffer);
  180.    sscanf (sBuffer + 11, "%04X", &hModule);
  181.    me.dwSize = sizeof (MODULEENTRY);
  182.  
  183.    // Retrieve module information
  184.    if (ModuleFindHandle (&me, hModule) == NULL)
  185.      {MessageBeep (0);
  186.       return;
  187.      }
  188.    len = strlen (me.szExePath);
  189.    /* Rudimentary way to check for a .DLL */
  190.    if (stricmp (me.szExePath + len - 4, ".DLL") != 0)
  191.      {sprintf (sBuffer, "hModule (0x%04X) does not refer to a .DLL", "",
  192.          hModule);
  193.       MessageBox (hWnd, sBuffer, "", MB_OK | MB_TASKMODAL);
  194.       return;
  195.      } // endif
  196.  
  197.    /* Display hInstance */
  198.    sprintf (sBuffer, "hModule = 0x%04X\n"
  199.                      "hInstance = 0x%04X",
  200.             hModule, GetDllModuleInstance (hModule));
  201.    MessageBox (hWnd, sBuffer, "", MB_OK | MB_TASKMODAL);
  202.   } // end _wm_gethinstance
  203.  
  204.  
  205. // Handles WM_COMMAND message from controls
  206. static void _wm_command (HWND hWnd, int iCtrlID, WORD wCode)
  207.   {switch (iCtrlID)
  208.      {case IDOK:
  209.       case IDCANCEL:
  210.          EndDialog (hWnd, 0);
  211.          break;
  212.  
  213.       case IDUPDATE:
  214.          PostMessage (hWnd, WM_GETMODULES, 0, 0L);
  215.          break;
  216.  
  217.       case IDGETHINSTANCE:
  218.          PostMessage (hWnd, WM_GETHINSTANCE, 0, 0L);
  219.          break;
  220.  
  221.       case IDLISTBOX:
  222.          switch (wCode)
  223.            {case LBN_DBLCLK:
  224.                PostMessage (hWnd, WM_GETHINSTANCE, 0, 0L);
  225.                break;
  226.  
  227.             case LBN_SELCHANGE:
  228.                PostMessage (hWnd, WM_SELCHANGED, 0, 0L);
  229.                break;
  230.            }
  231.          break;
  232.      } // end switch
  233.   } // end _wm_command
  234.  
  235.  
  236.  
  237.