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.

83 lines
2.7 KiB

  1. //--------------------------------------------------------------------------
  2. // Dllmain.cpp
  3. //--------------------------------------------------------------------------
  4. #include "pch.hxx"
  5. #include "dllmain.h"
  6. #include "shared.h"
  7. //--------------------------------------------------------------------------
  8. // Globals
  9. //--------------------------------------------------------------------------
  10. IMalloc *g_pMalloc=NULL;
  11. HINSTANCE g_hInst=NULL;
  12. LONG g_cRef=0;
  13. LONG g_cLock=0;
  14. CRITICAL_SECTION g_csDllMain={0};
  15. //--------------------------------------------------------------------------
  16. // DllMain
  17. //--------------------------------------------------------------------------
  18. EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
  19. {
  20. // DLL_PROCESS_ATTACH
  21. if (DLL_PROCESS_ATTACH == dwReason)
  22. {
  23. g_hInst = hInst;
  24. CoGetMalloc(1, &g_pMalloc);
  25. InitializeCriticalSection(&g_csDllMain);
  26. DisableThreadLibraryCalls(hInst);
  27. }
  28. // DLL_PROCESS_DETACH
  29. else if (DLL_PROCESS_DETACH == dwReason)
  30. {
  31. DeleteCriticalSection(&g_csDllMain);
  32. SafeRelease(g_pMalloc);
  33. }
  34. // Done
  35. return(TRUE);
  36. }
  37. // --------------------------------------------------------------------------------
  38. // DllAddRef
  39. // --------------------------------------------------------------------------------
  40. ULONG DllAddRef(void)
  41. {
  42. return((ULONG)InterlockedIncrement(&g_cRef));
  43. }
  44. // --------------------------------------------------------------------------------
  45. // DllRelease
  46. // --------------------------------------------------------------------------------
  47. ULONG DllRelease(void)
  48. {
  49. return((ULONG)InterlockedDecrement(&g_cRef));
  50. }
  51. // --------------------------------------------------------------------------------
  52. // DllCanUnloadNow
  53. // --------------------------------------------------------------------------------
  54. STDAPI DllCanUnloadNow(void)
  55. {
  56. EnterCriticalSection(&g_csDllMain);
  57. HRESULT hr = (0 == g_cRef && 0 == g_cLock) ? S_OK : S_FALSE;
  58. LeaveCriticalSection(&g_csDllMain);
  59. return(hr);
  60. }
  61. // --------------------------------------------------------------------------------
  62. // DllRegisterServer
  63. // --------------------------------------------------------------------------------
  64. STDAPI DllRegisterServer(void)
  65. {
  66. return(CallRegInstall(g_hInst, g_hInst, c_szReg, NULL));
  67. }
  68. // --------------------------------------------------------------------------------
  69. // DllUnregisterServer
  70. // --------------------------------------------------------------------------------
  71. STDAPI DllUnregisterServer(void)
  72. {
  73. return(CallRegInstall(g_hInst, g_hInst, c_szUnReg, NULL));
  74. }