Leaked source code of windows server 2003
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.

224 lines
4.6 KiB

  1. // CSecStor.cpp : Implementation of DLL Exports.
  2. // You will need the NT SUR Beta 2 SDK or VC 4.2 in order to build this
  3. // project. This is because you will need MIDL 3.00.15 or higher and new
  4. // headers and libs. If you have VC 4.2 installed, then everything should
  5. // already be configured correctly.
  6. // Note: Proxy/Stub Information
  7. // To build a separate proxy/stub DLL,
  8. // run nmake -f ISecStorps.mak in the project directory.
  9. #include "stdafx.h"
  10. #include "resource.h"
  11. #include "initguid.h"
  12. #include "pstypes.h"
  13. #include "pstorec.h"
  14. #include "CSecStr1.h"
  15. #define IID_DEFINED
  16. #include "PStorec_i.c"
  17. #include "unicode.h"
  18. #include <wincrypt.h>
  19. #include "pstprv.h" // MODULE_RAISE_COUNT
  20. BOOL
  21. RaiseRefCount(
  22. VOID
  23. );
  24. BOOL
  25. LowerRefCount(
  26. VOID
  27. );
  28. LONG g_lRefCount = 1;
  29. HMODULE g_hModule = NULL;
  30. CComModule _Module;
  31. BEGIN_OBJECT_MAP(ObjectMap)
  32. OBJECT_ENTRY(CLSID_CPStore, CPStore)
  33. END_OBJECT_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // DLL Entry Point
  36. extern "C"
  37. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  38. {
  39. if (dwReason == DLL_PROCESS_ATTACH)
  40. {
  41. _Module.Init(ObjectMap, hInstance);
  42. DisableThreadLibraryCalls(hInstance);
  43. // begin HACK HACK HACK
  44. // fix for rpcrt4 load/free memory leak...
  45. // bug is actually in rpcrt4 dependencies: user32, advapi
  46. // load module. DON'T FREE IT, causes reload leaks
  47. LoadLibrary("rpcrt4.dll");
  48. // note: NT, Win95 srcs checked -- neither will overflow 4G refcount
  49. // end HACK HACK HACK
  50. RaiseRefCount();
  51. }
  52. else if (dwReason == DLL_PROCESS_DETACH) {
  53. _Module.Term();
  54. LowerRefCount();
  55. }
  56. return TRUE; // ok
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. // Used to determine whether the DLL can be unloaded by OLE
  60. STDAPI DllCanUnloadNow(void)
  61. {
  62. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  63. }
  64. /////////////////////////////////////////////////////////////////////////////
  65. // Returns a class factory to create an object of the requested type
  66. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  67. {
  68. return _Module.GetClassObject(rclsid, riid, ppv);
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // DllRegisterServer - Adds entries to the system registry
  72. STDAPI DllRegisterServer(void)
  73. {
  74. // registers object, typelib and all interfaces in typelib
  75. return _Module.RegisterServer(TRUE);
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // DllUnregisterServer - Removes entries from the system registry
  79. STDAPI DllUnregisterServer(void)
  80. {
  81. _Module.UnregisterServer();
  82. return S_OK;
  83. }
  84. //
  85. // overload new and delete so we don't need to bring in full CRT
  86. //
  87. #if 0
  88. void * __cdecl operator new(size_t cb)
  89. {
  90. return HeapAlloc(GetProcessHeap(), 0, cb);
  91. }
  92. void __cdecl operator delete(void * pv)
  93. {
  94. HeapFree(GetProcessHeap(), 0, pv);
  95. }
  96. #ifndef DBG
  97. void * __cdecl malloc(size_t cb)
  98. {
  99. return HeapAlloc(GetProcessHeap(), 0, cb);
  100. }
  101. void __cdecl free(void * pv)
  102. {
  103. HeapFree(GetProcessHeap(), 0, pv);
  104. }
  105. void * __cdecl realloc(void * pv, size_t cb)
  106. {
  107. if(pv == NULL)
  108. return malloc(cb);
  109. return HeapReAlloc(GetProcessHeap(), 0, pv, cb);
  110. }
  111. #endif
  112. #endif
  113. //
  114. // provide allocator for rule allocation routines.
  115. //
  116. LPVOID
  117. RulesAlloc(
  118. IN DWORD cb
  119. )
  120. {
  121. return CoTaskMemAlloc( cb );
  122. }
  123. VOID
  124. RulesFree(
  125. IN LPVOID pv
  126. )
  127. {
  128. CoTaskMemFree( pv );
  129. }
  130. BOOL
  131. RaiseRefCount(
  132. VOID
  133. )
  134. {
  135. WCHAR szFileName[ MAX_PATH + 1 ];
  136. HMODULE hModule;
  137. LONG i;
  138. BOOL fSuccess = TRUE;
  139. if(GetModuleFileNameU( NULL, szFileName, MAX_PATH ) == 0)
  140. return FALSE;
  141. for ( i = 0 ; i < MODULE_RAISE_COUNT ; i++ ) {
  142. hModule = LoadLibraryU(szFileName);
  143. if(hModule == NULL) {
  144. fSuccess = FALSE;
  145. break;
  146. }
  147. InterlockedIncrement( &g_lRefCount );
  148. }
  149. if(hModule != NULL)
  150. g_hModule = hModule;
  151. return fSuccess;
  152. }
  153. BOOL
  154. LowerRefCount(
  155. VOID
  156. )
  157. {
  158. BOOL fSuccess = TRUE;
  159. if( g_hModule == NULL )
  160. return FALSE;
  161. while ( InterlockedDecrement( &g_lRefCount ) > 0 ) {
  162. if(!FreeLibrary( g_hModule )) {
  163. fSuccess = FALSE;
  164. break;
  165. }
  166. }
  167. InterlockedIncrement( &g_lRefCount );
  168. return fSuccess;
  169. }