Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.9 KiB

  1. /*
  2. * targetdll.cxx
  3. *
  4. *
  5. * Copyright (c) 1998-1999 Microsoft Corporation
  6. *
  7. * PURPOSE: Implements the DLL entry points.
  8. *
  9. *
  10. * OWNER: ptousig
  11. */
  12. #ifndef _SNAPINLIST_FILE
  13. #error _SNAPINLIST_FILE not defined
  14. #endif
  15. #ifndef _FREGISTERTYPELIB
  16. #define _FREGISTERTYPELIB TRUE // define this equal to FALSE to avoid registering the typelib.
  17. #endif //_FREGISTERTYPELIB
  18. #ifndef SNAPIN_COM_OBJECTS
  19. #define SNAPIN_COM_OBJECTS
  20. #endif
  21. extern "C"
  22. {
  23. BOOL WINAPI _CRT_INIT( HANDLE hInstance, DWORD nReason, LPVOID pReserved );
  24. }
  25. #define DECLARE_SNAPIN(_snapin) \
  26. template CComponentDataTemplate<_snapin, CComponent, &CLSID_ComponentData_##_snapin>; \
  27. typedef CComponentDataTemplate<_snapin, CComponent, &CLSID_ComponentData_##_snapin> t_ComponentData_##_snapin; \
  28. typedef CSnapinAboutTemplate<_snapin, &CLSID_SnapinAbout_##_snapin> t_SnapinAbout_##_snapin;
  29. #include _SNAPINLIST_FILE
  30. // Declare the ATL COM object map.
  31. BEGIN_OBJECT_MAP(ObjectMap)
  32. #define DECLARE_SNAPIN(_snapin) \
  33. OBJECT_ENTRY(CLSID_ComponentData_##_snapin, t_ComponentData_##_snapin) \
  34. OBJECT_ENTRY(CLSID_SnapinAbout_##_snapin, t_SnapinAbout_##_snapin) \
  35. #include _SNAPINLIST_FILE
  36. SNAPIN_COM_OBJECTS
  37. END_OBJECT_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // DLL Entry Point
  40. extern "C"
  41. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  42. {
  43. SC sc;
  44. CBaseFrame::s_hinst = hInstance;
  45. if (dwReason == DLL_PROCESS_ATTACH)
  46. {
  47. //
  48. // since _CRT_INIT will create global variables/objects we must make sure
  49. // that it is called before we initialize ourselves.
  50. //
  51. if (!_CRT_INIT(hInstance, dwReason, lpReserved ))
  52. {
  53. sc = ScFromWin32(::GetLastError());
  54. goto Error;
  55. }
  56. CMMCFrame::Initialize(hInstance, NULL, NULL, SW_RESTORE);
  57. _Module.Init(ObjectMap, hInstance);
  58. DisableThreadLibraryCalls(hInstance);
  59. }
  60. else if (dwReason == DLL_PROCESS_DETACH)
  61. {
  62. _Module.Term();
  63. CMMCFrame::Uninitialize();
  64. //
  65. // since _CRT_INIT will destroy global variables/objects we must make sure
  66. // that it is called after we have unitialized ourselves.
  67. //
  68. if (!_CRT_INIT(hInstance, dwReason, lpReserved ))
  69. {
  70. sc = ScFromWin32(::GetLastError());
  71. goto Error;
  72. }
  73. }
  74. else if (!_CRT_INIT(hInstance, dwReason, lpReserved ))
  75. {
  76. sc = ScFromWin32(::GetLastError());
  77. goto Error;
  78. }
  79. return TRUE; // ok
  80. Error:
  81. MMCErrorBox(sc);
  82. return FALSE;
  83. }
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Used to determine whether the DLL can be unloaded by OLE
  86. STDAPI DllCanUnloadNow(void)
  87. {
  88. CMMCFrame::Initialize();
  89. return(_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. // Returns a class factory to create an object of the requested type
  93. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  94. {
  95. CMMCFrame::Initialize();
  96. return _Module.GetClassObject(rclsid, riid, ppv);
  97. }
  98. HRESULT RegisterSnapins(BOOL fRegister)
  99. {
  100. HRESULT hr = S_OK;
  101. // Register/unregister all snapins.
  102. BEGIN_SNAPIN_MAP()
  103. #define DECLARE_SNAPIN(_snapin) SNAPIN_ENTRY(t_ComponentData_##_snapin, fRegister)
  104. #include _SNAPINLIST_FILE
  105. END_SNAPIN_MAP()
  106. return hr;
  107. }
  108. /////////////////////////////////////////////////////////////////////////////
  109. // DllRegisterServer - Adds entries to the system registry
  110. STDAPI DllRegisterServer(void)
  111. {
  112. HRESULT hr = S_OK;
  113. CMMCFrame::Initialize();
  114. hr = RegisterSnapins(TRUE); // registers all snap-ins.
  115. if (FAILED(hr))
  116. goto Error;
  117. hr = _Module.RegisterServer(_FREGISTERTYPELIB); // registers object, typelib and all interfaces in typelib
  118. if (hr)
  119. goto Error;
  120. CMMCFrame::Uninitialize(); // hack to avoid a multithreaded uninit in DllMain
  121. Cleanup:
  122. return hr;
  123. Error:
  124. goto Cleanup;
  125. }
  126. /////////////////////////////////////////////////////////////////////////////
  127. // DllUnregisterServer - Removes entries from the system registry
  128. STDAPI DllUnregisterServer(void)
  129. {
  130. HRESULT hr = S_OK;
  131. CMMCFrame::Initialize();
  132. hr = RegisterSnapins(FALSE); // unregisters all snap-ins.
  133. if (FAILED(hr))
  134. goto Error;
  135. hr = _Module.UnregisterServer();// does not delete the registry keys.
  136. if (FAILED(hr))
  137. goto Error;
  138. CMMCFrame::Uninitialize(); // hack to avoid a multithreaded uninit in DllMain
  139. Cleanup:
  140. return hr;
  141. Error:
  142. goto Cleanup;
  143. }