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.

169 lines
4.5 KiB

  1. #include <windows.h>
  2. #include <shlwapi.h>
  3. #include <advpub.h>
  4. #include "dllmain.h"
  5. #include "acctreg.h"
  6. #define ARRAYSIZE(_exp_) (sizeof(_exp_) / sizeof(_exp_[0]))
  7. CRITICAL_SECTION g_csDllMain={0};
  8. ULONG g_cRefDll=0;
  9. HINSTANCE g_hInst=NULL;
  10. void InitGlobalVars(void)
  11. {
  12. InitializeCriticalSection(&g_csDllMain);
  13. }
  14. void FreeGlobalVars(void)
  15. {
  16. DeleteCriticalSection(&g_csDllMain);
  17. }
  18. // --------------------------------------------------------------------------------
  19. // Dll Entry Point
  20. // --------------------------------------------------------------------------------
  21. EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
  22. {
  23. // Handle Attach - detach reason
  24. switch (dwReason)
  25. {
  26. case DLL_PROCESS_ATTACH:
  27. // Set global instance handle
  28. g_hInst = hInst;
  29. // Initialize Global Variables
  30. InitGlobalVars();
  31. // we don't care about thread-attach notifications, so
  32. // diable them, This is mondo-more efficient for creating
  33. // threads
  34. DisableThreadLibraryCalls(hInst);
  35. break;
  36. case DLL_PROCESS_DETACH:
  37. FreeGlobalVars();
  38. break;
  39. }
  40. return TRUE;
  41. }
  42. // --------------------------------------------------------------------------------
  43. // DllAddRef
  44. // --------------------------------------------------------------------------------
  45. ULONG DllAddRef(void)
  46. {
  47. return (ULONG)InterlockedIncrement((LPLONG)&g_cRefDll);
  48. }
  49. // --------------------------------------------------------------------------------
  50. // DllRelease
  51. // --------------------------------------------------------------------------------
  52. ULONG DllRelease(void)
  53. {
  54. return (ULONG)InterlockedDecrement((LPLONG)&g_cRefDll);
  55. }
  56. // --------------------------------------------------------------------------------
  57. // DllCanUnloadNow
  58. //
  59. // Ole will hit this now and again to see if it can free up our library
  60. // --------------------------------------------------------------------------------
  61. STDAPI DllCanUnloadNow(void)
  62. {
  63. HRESULT hr;
  64. EnterCriticalSection(&g_csDllMain);
  65. hr = g_cRefDll ? S_FALSE : S_OK;
  66. LeaveCriticalSection(&g_csDllMain);
  67. return hr;
  68. }
  69. // --------------------------------------------------------------------------------
  70. // Override new operator
  71. // --------------------------------------------------------------------------------
  72. void * __cdecl operator new(UINT cb)
  73. {
  74. LPVOID lpv;
  75. lpv = malloc(cb);
  76. return lpv;
  77. }
  78. // --------------------------------------------------------------------------------
  79. // Override delete operator
  80. // --------------------------------------------------------------------------------
  81. void __cdecl operator delete(LPVOID pv)
  82. {
  83. free(pv);
  84. }
  85. HRESULT CallRegInstall(HINSTANCE hInst, LPCSTR pszSection)
  86. {
  87. HRESULT hr = E_FAIL;
  88. HINSTANCE hAdvPack;
  89. REGINSTALL pfnri;
  90. char szDll[MAX_PATH];
  91. int cch;
  92. STRENTRY seReg[2];
  93. STRTABLE stReg;
  94. OSVERSIONINFO verinfo; // Version Check
  95. hAdvPack = LoadLibraryA("advpack.dll");
  96. if (NULL == hAdvPack)
  97. return(E_FAIL);
  98. // Get our location
  99. GetModuleFileName(hInst, szDll, ARRAYSIZE(szDll));
  100. // Get Proc Address for registration util
  101. pfnri = (REGINSTALL)GetProcAddress(hAdvPack, achREGINSTALL);
  102. if (NULL == pfnri)
  103. goto exit;
  104. // Setup special registration stuff
  105. // Do this instead of relying on _SYS_MOD_PATH which loses spaces under '95
  106. stReg.cEntries = 0;
  107. seReg[stReg.cEntries].pszName = "SYS_MOD_PATH";
  108. seReg[stReg.cEntries].pszValue = szDll;
  109. stReg.cEntries++;
  110. stReg.pse = seReg;
  111. // Call the self-reg routine
  112. hr = pfnri(hInst, pszSection, &stReg);
  113. exit:
  114. // Cleanup
  115. FreeLibrary(hAdvPack);
  116. return(hr);
  117. }
  118. // --------------------------------------------------------------------------------
  119. // DllRegisterServer
  120. // --------------------------------------------------------------------------------
  121. STDAPI DllRegisterServer(void)
  122. {
  123. HRESULT hr;
  124. // Register my self
  125. hr = CallRegInstall(g_hInst, "Reg");
  126. return(hr);
  127. }
  128. // --------------------------------------------------------------------------------
  129. // DllUnregisterServer
  130. // --------------------------------------------------------------------------------
  131. STDAPI DllUnregisterServer(void)
  132. {
  133. HRESULT hr;
  134. hr = CallRegInstall(g_hInst, "UnReg");
  135. return(hr);
  136. }