home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Profiler / heapmon / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  8.6 KB  |  338 lines

  1. // main.cpp
  2. //
  3. // Created 08/01/99
  4. //
  5. // (C)Copyright 1999 Microsoft Corporation, All rights reserved.
  6. //
  7.  
  8. #include "pch.hpp"
  9. #pragma hdrstop
  10.  
  11. #include "hpmonmgr.hpp"
  12. #include "guids.h"
  13.  
  14.  
  15. HINSTANCE g_hInst;
  16.  
  17. ULONG g_ModuleRefCount = 0;
  18.  
  19.  
  20. // Various constants used during self-registration
  21. const char *g_monitor_clsid     = "{CEFC6B90-5B5C-11d2-B067-006008039BF0}";
  22. const char *g_monitor_threading_model   = "Both"; // threading model ("Both" is required by JavaVM since it is free threaded)
  23. const char *g_monitor_description   = "Sample Java VM Heap Monitor."; // description of us
  24. const char *g_monitor_progid        = "SampleJavaHeapMonitor"; // program id
  25.  
  26. // We register as a subkey under the Java VM Monitors key
  27. // Create a subkey for our monitor and set several properties
  28. const char *g_monitor_JavaVM_key    = "SOFTWARE\\Microsoft\\Java VM\\Monitors\\SampleHeapMonitor";
  29.  
  30.  
  31. STDMETHODIMP
  32. DllRegisterServer(VOID)
  33. {
  34.     HRESULT hr = SELFREG_E_CLASS;
  35.     HKEY    hKey  = NULL;
  36.     HKEY    hKey2 = NULL;
  37.     HKEY    hKey3 = NULL;
  38.     DWORD   result;
  39.     char    module_path_name[MAX_PATH];
  40.  
  41.     // If we fail in the middle, the state of the registry entries
  42.     // is indeterminate (as per OLE specs.)
  43.  
  44.  
  45.     // Create HKEY_CLASSES_ROOT\<progid>...
  46.     result = ::RegCreateKey(HKEY_CLASSES_ROOT, g_monitor_progid, &hKey);
  47.     if (result != ERROR_SUCCESS) {
  48.     goto lExit;
  49.     }
  50.     // Set HKEY_CLASSES_ROOT\<progid> (default) = <desc>
  51.     result = ::RegSetValue(hKey, NULL, REG_SZ, g_monitor_description, lstrlen(g_monitor_description));
  52.     if (result != ERROR_SUCCESS) {
  53.     goto lExit;
  54.     }
  55.     // Create HKEY_CLASSES_ROOT\<progid>\CLSID...
  56.     result = ::RegCreateKey(hKey, TEXT("CLSID"), &hKey2);
  57.     if (result != ERROR_SUCCESS) {
  58.     goto lExit;
  59.     }
  60.     // Set HKEY_CLASSES_ROOT\<progid>\CLSID (default) = <clsid>
  61.     result = ::RegSetValue(hKey2, NULL, REG_SZ, g_monitor_clsid, lstrlen(g_monitor_clsid));
  62.     if (result != ERROR_SUCCESS) {
  63.     goto lExit;
  64.     }
  65.  
  66.     ::RegCloseKey(hKey);
  67.     ::RegCloseKey(hKey2);
  68.     hKey = NULL;
  69.     hKey2 = NULL;
  70.  
  71.  
  72.     // Create HKEY_CLASSES_ROOT\CLSID\...
  73.     result = ::RegCreateKey(HKEY_CLASSES_ROOT, TEXT("CLSID"), &hKey);
  74.     if (result != ERROR_SUCCESS) {
  75.     goto lExit;
  76.     }
  77.     // Create HKEY_CLASSES_ROOT\CLSID\<clsid> ...
  78.     result = ::RegCreateKey(hKey, g_monitor_clsid, &hKey2);
  79.     if (result != ERROR_SUCCESS) {
  80.     goto lExit;
  81.     }
  82.     // Set HKEY_CLASSES_ROOT\CLSID\<clsid> (default) = <desc>
  83.     result = ::RegSetValue(hKey2, NULL, REG_SZ, g_monitor_description, lstrlen(g_monitor_description));
  84.     if (result != ERROR_SUCCESS) {
  85.     goto lExit;
  86.     }
  87.     // Create HKEY_CLASSES_ROOT\CLSID\<clsid>\InprocServer32....
  88.     result = ::RegCreateKey(hKey2, "InprocServer32", &hKey3);
  89.     if (result != ERROR_SUCCESS) {
  90.     goto lExit;
  91.     }
  92.  
  93.     result = GetModuleFileName(g_hInst, module_path_name, sizeof(module_path_name)/sizeof(char));
  94.     if (result == 0) {  //No way to detect truncation from GetModuleFileName.
  95.     goto lExit;
  96.     }
  97.     // Set HKEY_CLASSES_ROOT\CLSID\<clsid>\InprocServer32 (default) = <module_name>
  98.     result = ::RegSetValue(hKey3, NULL, REG_SZ, module_path_name, lstrlen(module_path_name));
  99.     if (result != ERROR_SUCCESS) {
  100.     goto lExit;
  101.     }
  102.     // Set HKEY_CLASSES_ROOT\CLSID\<clsid>\InprocServer32\ThreadingModel = <tm>
  103.     result = ::RegSetValueEx(hKey3, "ThreadingModel", 0, REG_SZ, (BYTE*)g_monitor_threading_model, sizeof(g_monitor_threading_model));
  104.     if (result != ERROR_SUCCESS) {
  105.     goto lExit;
  106.     }
  107.  
  108.     ::RegCloseKey(hKey3);
  109.     hKey3 = NULL;
  110.  
  111.     // Create HKEY_CLASSES_ROOT\CLSID\<clsid>\ProgID...
  112.     result = ::RegCreateKey(hKey2, "ProgID", &hKey3);
  113.     if (result != ERROR_SUCCESS) {
  114.     goto lExit;
  115.     }
  116.     // Set HKEY_CLASSES_ROOT\CLSID\<clsid>\ProgID = <progid>
  117.     result = ::RegSetValue(hKey3, NULL, REG_SZ, g_monitor_progid, lstrlen(g_monitor_progid));
  118.     if (result != ERROR_SUCCESS) {
  119.     goto lExit;
  120.     }
  121.     ::RegCloseKey(hKey3);
  122.     hKey3 = NULL;
  123.  
  124.     ::RegCloseKey(hKey);
  125.     ::RegCloseKey(hKey2);
  126.     hKey = NULL;
  127.     hKey2 = NULL;
  128.  
  129.     // Now register us as a monitor with the Java VM
  130.     // Create HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Java VM\Monitors\<monitor>
  131.     result = ::RegCreateKey(HKEY_LOCAL_MACHINE, g_monitor_JavaVM_key, &hKey);
  132.     if (result != ERROR_SUCCESS) {
  133.     goto lExit;
  134.     }
  135.     // Set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Java VM\Monitors\<monitor> = <desc>
  136.     result = ::RegSetValue(hKey, NULL, REG_SZ, g_monitor_description, lstrlen(g_monitor_description));
  137.     if (result != ERROR_SUCCESS) {
  138.     goto lExit;
  139.     }
  140.  
  141.     // Set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Java VM\Monitors\<monitor>\CLSID = <clsid>
  142.     result = ::RegSetValueEx(hKey, "CLSID", 0, REG_SZ, (BYTE*)g_monitor_clsid, lstrlen(g_monitor_clsid));
  143.     if (result != ERROR_SUCCESS) {
  144.     goto lExit;
  145.     }
  146.  
  147.     ::RegCloseKey(hKey);
  148.     hKey = NULL;
  149.  
  150.     hr = S_OK;
  151.  
  152.   lExit:
  153.     if (hKey) {
  154.     ::RegCloseKey(hKey);
  155.     }
  156.     if (hKey2) {
  157.     ::RegCloseKey(hKey2);
  158.     }
  159.     if (hKey3) {
  160.     ::RegCloseKey(hKey3);
  161.     }
  162.     return hr;
  163.  
  164. }
  165.  
  166.  
  167. STDMETHODIMP
  168. DllUnregisterServer(VOID)
  169. {
  170.     HRESULT hr = SELFREG_E_CLASS;
  171.     DWORD   result;
  172.  
  173.     result = ::RegDeleteKey(HKEY_LOCAL_MACHINE, g_monitor_JavaVM_key);
  174.     if (result != ERROR_SUCCESS) {
  175.     goto lExit;
  176.     }
  177.     hr = S_OK;
  178.  
  179.   lExit:
  180.     return hr;
  181.  
  182. }
  183.  
  184.  
  185. class HeapMonitorManagerClassFactory : public IClassFactory
  186. {
  187. private:
  188.  
  189.     ULONG m_RefCount;
  190.  
  191. public:
  192.  
  193.     HeapMonitorManagerClassFactory ()
  194.     {
  195.         m_RefCount = 1;
  196.         InterlockedIncrement((LONG*)&g_ModuleRefCount);
  197.     }
  198.  
  199.     ~HeapMonitorManagerClassFactory ()
  200.     {
  201.         ASSERT(!m_RefCount);
  202.         InterlockedDecrement((LONG*)&g_ModuleRefCount);
  203.     }
  204.  
  205.     // IUnknown methods
  206.  
  207.     HRESULT STDMETHODCALLTYPE QueryInterface (
  208.         /* [in] */ REFIID riid,
  209.         /* [iid_is][out] */ void **ppvObject)
  210.     {
  211.         HRESULT hr = S_OK;
  212.     
  213.         if (   riid == IID_IUnknown
  214.             || riid == IID_IClassFactory)
  215.         {
  216.             *ppvObject = (IClassFactory*)this;
  217.             AddRef();
  218.         }
  219.         else
  220.         {
  221.             hr = E_NOINTERFACE;
  222.         }
  223.  
  224.         return hr;
  225.     }
  226.     
  227.     ULONG STDMETHODCALLTYPE AddRef ()
  228.     {
  229.         return InterlockedIncrement((LONG*)&m_RefCount);
  230.     }
  231.     
  232.     ULONG STDMETHODCALLTYPE Release ()
  233.     {
  234.         ULONG NewRefCount = (ULONG)InterlockedDecrement((LONG*)&m_RefCount);
  235.         if (!NewRefCount)
  236.             delete(this);
  237.         return NewRefCount;
  238.     }
  239.  
  240.     // IClassFactory methods
  241.  
  242.     HRESULT STDMETHODCALLTYPE CreateInstance (
  243.         /* [unique][in] */ IUnknown *pUnkOuter,
  244.         /* [in] */ REFIID riid,
  245.         /* [iid_is][out] */ void **ppvObject)
  246.     {
  247.         HRESULT hr = S_OK;
  248.  
  249.         if (!pUnkOuter)
  250.         {
  251.             HeapMonitorManager *pmgr = new(HeapMonitorManager());
  252.             if (pmgr != NULL)
  253.             {
  254.                 hr = pmgr->Initialize(g_hInst);
  255.                 if (SUCCEEDED(hr))
  256.                     hr = pmgr->QueryInterface(riid, (PVOID*)ppvObject);
  257.  
  258.                 pmgr->Release();
  259.             }
  260.             else
  261.             {
  262.                 hr = E_OUTOFMEMORY;
  263.             }
  264.         }
  265.         else
  266.         {
  267.             hr = CLASS_E_NOAGGREGATION;
  268.         }
  269.  
  270.         return hr;
  271.     }
  272.     
  273.     HRESULT STDMETHODCALLTYPE LockServer (
  274.         /* [in] */ BOOL fLock)
  275.     {
  276.         if (fLock)
  277.             InterlockedIncrement((LONG*)&g_ModuleRefCount);
  278.         else
  279.             InterlockedDecrement((LONG*)&g_ModuleRefCount);
  280.         return S_OK;
  281.     }
  282. };
  283.  
  284.  
  285. STDMETHODIMP
  286. DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
  287. {
  288.     HRESULT hr = S_OK;
  289.  
  290.     if (rclsid == CLSID_SampleJavaHeapMonitor)
  291.     {
  292.         HeapMonitorManagerClassFactory *pcf = new(HeapMonitorManagerClassFactory());
  293.         if (pcf != NULL)
  294.         {
  295.             hr = pcf->QueryInterface(riid, ppv);
  296.  
  297.             pcf->Release();
  298.         }
  299.         else
  300.         {
  301.             hr = E_OUTOFMEMORY;
  302.         }
  303.     }
  304.     else
  305.     {
  306.         hr = REGDB_E_CLASSNOTREG;
  307.     }
  308.     
  309.     return hr;
  310. }
  311.  
  312.  
  313. STDMETHODIMP
  314. DllCanUnloadNow(void)
  315. {
  316.     return g_ModuleRefCount == 0;
  317. }
  318.  
  319.  
  320. BOOL WINAPI DllMain (HINSTANCE hmod, DWORD dwReason, PVOID pvReserved)
  321. {
  322.     if (dwReason == DLL_PROCESS_ATTACH)
  323.     {
  324.         g_hInst = hmod;
  325.  
  326.         DisableThreadLibraryCalls(hmod);
  327.  
  328.         HeapMonitorManager::OnProcessAttach(hmod);
  329.     }
  330.     else if (dwReason == DLL_PROCESS_DETACH)
  331.     {
  332.         HeapMonitorManager::OnProcessDetach(hmod);
  333.     }
  334.  
  335.     return TRUE;
  336. }
  337.  
  338.