home *** CD-ROM | disk | FTP | other *** search
- // main.cpp
- //
- // Created 08/01/99
- //
- // (C)Copyright 1999 Microsoft Corporation, All rights reserved.
- //
-
- #include "pch.hpp"
- #pragma hdrstop
-
- #include "hpmonmgr.hpp"
- #include "guids.h"
-
-
- HINSTANCE g_hInst;
-
- ULONG g_ModuleRefCount = 0;
-
-
- // Various constants used during self-registration
- const char *g_monitor_clsid = "{CEFC6B90-5B5C-11d2-B067-006008039BF0}";
- const char *g_monitor_threading_model = "Both"; // threading model ("Both" is required by JavaVM since it is free threaded)
- const char *g_monitor_description = "Sample Java VM Heap Monitor."; // description of us
- const char *g_monitor_progid = "SampleJavaHeapMonitor"; // program id
-
- // We register as a subkey under the Java VM Monitors key
- // Create a subkey for our monitor and set several properties
- const char *g_monitor_JavaVM_key = "SOFTWARE\\Microsoft\\Java VM\\Monitors\\SampleHeapMonitor";
-
-
- STDMETHODIMP
- DllRegisterServer(VOID)
- {
- HRESULT hr = SELFREG_E_CLASS;
- HKEY hKey = NULL;
- HKEY hKey2 = NULL;
- HKEY hKey3 = NULL;
- DWORD result;
- char module_path_name[MAX_PATH];
-
- // If we fail in the middle, the state of the registry entries
- // is indeterminate (as per OLE specs.)
-
-
- // Create HKEY_CLASSES_ROOT\<progid>...
- result = ::RegCreateKey(HKEY_CLASSES_ROOT, g_monitor_progid, &hKey);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Set HKEY_CLASSES_ROOT\<progid> (default) = <desc>
- result = ::RegSetValue(hKey, NULL, REG_SZ, g_monitor_description, lstrlen(g_monitor_description));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Create HKEY_CLASSES_ROOT\<progid>\CLSID...
- result = ::RegCreateKey(hKey, TEXT("CLSID"), &hKey2);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Set HKEY_CLASSES_ROOT\<progid>\CLSID (default) = <clsid>
- result = ::RegSetValue(hKey2, NULL, REG_SZ, g_monitor_clsid, lstrlen(g_monitor_clsid));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
-
- ::RegCloseKey(hKey);
- ::RegCloseKey(hKey2);
- hKey = NULL;
- hKey2 = NULL;
-
-
- // Create HKEY_CLASSES_ROOT\CLSID\...
- result = ::RegCreateKey(HKEY_CLASSES_ROOT, TEXT("CLSID"), &hKey);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Create HKEY_CLASSES_ROOT\CLSID\<clsid> ...
- result = ::RegCreateKey(hKey, g_monitor_clsid, &hKey2);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Set HKEY_CLASSES_ROOT\CLSID\<clsid> (default) = <desc>
- result = ::RegSetValue(hKey2, NULL, REG_SZ, g_monitor_description, lstrlen(g_monitor_description));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Create HKEY_CLASSES_ROOT\CLSID\<clsid>\InprocServer32....
- result = ::RegCreateKey(hKey2, "InprocServer32", &hKey3);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
-
- result = GetModuleFileName(g_hInst, module_path_name, sizeof(module_path_name)/sizeof(char));
- if (result == 0) { //No way to detect truncation from GetModuleFileName.
- goto lExit;
- }
- // Set HKEY_CLASSES_ROOT\CLSID\<clsid>\InprocServer32 (default) = <module_name>
- result = ::RegSetValue(hKey3, NULL, REG_SZ, module_path_name, lstrlen(module_path_name));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Set HKEY_CLASSES_ROOT\CLSID\<clsid>\InprocServer32\ThreadingModel = <tm>
- result = ::RegSetValueEx(hKey3, "ThreadingModel", 0, REG_SZ, (BYTE*)g_monitor_threading_model, sizeof(g_monitor_threading_model));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
-
- ::RegCloseKey(hKey3);
- hKey3 = NULL;
-
- // Create HKEY_CLASSES_ROOT\CLSID\<clsid>\ProgID...
- result = ::RegCreateKey(hKey2, "ProgID", &hKey3);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Set HKEY_CLASSES_ROOT\CLSID\<clsid>\ProgID = <progid>
- result = ::RegSetValue(hKey3, NULL, REG_SZ, g_monitor_progid, lstrlen(g_monitor_progid));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- ::RegCloseKey(hKey3);
- hKey3 = NULL;
-
- ::RegCloseKey(hKey);
- ::RegCloseKey(hKey2);
- hKey = NULL;
- hKey2 = NULL;
-
- // Now register us as a monitor with the Java VM
- // Create HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Java VM\Monitors\<monitor>
- result = ::RegCreateKey(HKEY_LOCAL_MACHINE, g_monitor_JavaVM_key, &hKey);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- // Set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Java VM\Monitors\<monitor> = <desc>
- result = ::RegSetValue(hKey, NULL, REG_SZ, g_monitor_description, lstrlen(g_monitor_description));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
-
- // Set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Java VM\Monitors\<monitor>\CLSID = <clsid>
- result = ::RegSetValueEx(hKey, "CLSID", 0, REG_SZ, (BYTE*)g_monitor_clsid, lstrlen(g_monitor_clsid));
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
-
- ::RegCloseKey(hKey);
- hKey = NULL;
-
- hr = S_OK;
-
- lExit:
- if (hKey) {
- ::RegCloseKey(hKey);
- }
- if (hKey2) {
- ::RegCloseKey(hKey2);
- }
- if (hKey3) {
- ::RegCloseKey(hKey3);
- }
- return hr;
-
- }
-
-
- STDMETHODIMP
- DllUnregisterServer(VOID)
- {
- HRESULT hr = SELFREG_E_CLASS;
- DWORD result;
-
- result = ::RegDeleteKey(HKEY_LOCAL_MACHINE, g_monitor_JavaVM_key);
- if (result != ERROR_SUCCESS) {
- goto lExit;
- }
- hr = S_OK;
-
- lExit:
- return hr;
-
- }
-
-
- class HeapMonitorManagerClassFactory : public IClassFactory
- {
- private:
-
- ULONG m_RefCount;
-
- public:
-
- HeapMonitorManagerClassFactory ()
- {
- m_RefCount = 1;
- InterlockedIncrement((LONG*)&g_ModuleRefCount);
- }
-
- ~HeapMonitorManagerClassFactory ()
- {
- ASSERT(!m_RefCount);
- InterlockedDecrement((LONG*)&g_ModuleRefCount);
- }
-
- // IUnknown methods
-
- HRESULT STDMETHODCALLTYPE QueryInterface (
- /* [in] */ REFIID riid,
- /* [iid_is][out] */ void **ppvObject)
- {
- HRESULT hr = S_OK;
-
- if ( riid == IID_IUnknown
- || riid == IID_IClassFactory)
- {
- *ppvObject = (IClassFactory*)this;
- AddRef();
- }
- else
- {
- hr = E_NOINTERFACE;
- }
-
- return hr;
- }
-
- ULONG STDMETHODCALLTYPE AddRef ()
- {
- return InterlockedIncrement((LONG*)&m_RefCount);
- }
-
- ULONG STDMETHODCALLTYPE Release ()
- {
- ULONG NewRefCount = (ULONG)InterlockedDecrement((LONG*)&m_RefCount);
- if (!NewRefCount)
- delete(this);
- return NewRefCount;
- }
-
- // IClassFactory methods
-
- HRESULT STDMETHODCALLTYPE CreateInstance (
- /* [unique][in] */ IUnknown *pUnkOuter,
- /* [in] */ REFIID riid,
- /* [iid_is][out] */ void **ppvObject)
- {
- HRESULT hr = S_OK;
-
- if (!pUnkOuter)
- {
- HeapMonitorManager *pmgr = new(HeapMonitorManager());
- if (pmgr != NULL)
- {
- hr = pmgr->Initialize(g_hInst);
- if (SUCCEEDED(hr))
- hr = pmgr->QueryInterface(riid, (PVOID*)ppvObject);
-
- pmgr->Release();
- }
- else
- {
- hr = E_OUTOFMEMORY;
- }
- }
- else
- {
- hr = CLASS_E_NOAGGREGATION;
- }
-
- return hr;
- }
-
- HRESULT STDMETHODCALLTYPE LockServer (
- /* [in] */ BOOL fLock)
- {
- if (fLock)
- InterlockedIncrement((LONG*)&g_ModuleRefCount);
- else
- InterlockedDecrement((LONG*)&g_ModuleRefCount);
- return S_OK;
- }
- };
-
-
- STDMETHODIMP
- DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
- {
- HRESULT hr = S_OK;
-
- if (rclsid == CLSID_SampleJavaHeapMonitor)
- {
- HeapMonitorManagerClassFactory *pcf = new(HeapMonitorManagerClassFactory());
- if (pcf != NULL)
- {
- hr = pcf->QueryInterface(riid, ppv);
-
- pcf->Release();
- }
- else
- {
- hr = E_OUTOFMEMORY;
- }
- }
- else
- {
- hr = REGDB_E_CLASSNOTREG;
- }
-
- return hr;
- }
-
-
- STDMETHODIMP
- DllCanUnloadNow(void)
- {
- return g_ModuleRefCount == 0;
- }
-
-
- BOOL WINAPI DllMain (HINSTANCE hmod, DWORD dwReason, PVOID pvReserved)
- {
- if (dwReason == DLL_PROCESS_ATTACH)
- {
- g_hInst = hmod;
-
- DisableThreadLibraryCalls(hmod);
-
- HeapMonitorManager::OnProcessAttach(hmod);
- }
- else if (dwReason == DLL_PROCESS_DETACH)
- {
- HeapMonitorManager::OnProcessDetach(hmod);
- }
-
- return TRUE;
- }
-
-