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.

189 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. perfmain.c
  5. Abstract:
  6. This file contains the DllMain function for the NTFRSPRF.dll.
  7. Author:
  8. Rohan Kumar [rohank] 15-Feb-1999
  9. Environment:
  10. User Mode Service
  11. Revision History:
  12. --*/
  13. //
  14. // The common header file which leads to the definition of the CRITICAL_SECTION
  15. // data structure and declares the globals FRS_ThrdCounter and FRC_ThrdCounter.
  16. //
  17. #include <perrepsr.h>
  18. //
  19. // If InitializeCriticalSection raises an exception, set the global boolean
  20. // (below) to FALSE.
  21. //
  22. BOOLEAN ShouldPerfmonCollectData = TRUE;
  23. #ifdef INCLLOGGING
  24. HANDLE hEventLog;
  25. BOOLEAN DoLogging = TRUE;
  26. #define NTFRSPERF \
  27. L"SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\NTFRSPerf"
  28. #define EVENTLOGDLL L"%SystemRoot%\\System32\\ntfrsres.dll"
  29. #endif // INCLLOGGING
  30. BOOL
  31. WINAPI
  32. DllMain(
  33. HINSTANCE hinstDLL,
  34. DWORD fdwReason,
  35. LPVOID fImpLoad
  36. )
  37. /*++
  38. Routine Description:
  39. The DllMain routine for the NTFRSPRF.dll.
  40. Arguments:
  41. hinstDLL - Instance handle of the DLL.
  42. fdwReason - The reason for this function to be called by the system.
  43. fImpLoad - Indicated whether the DLL was implicitly or explicitly loaded.
  44. Return Value:
  45. TRUE.
  46. --*/
  47. {
  48. DWORD flag, WStatus;
  49. DWORD TypesSupported = 7; // Types of EventLog messages supported.
  50. HKEY Key;
  51. switch(fdwReason) {
  52. case DLL_PROCESS_ATTACH:
  53. //
  54. // THe DLL is being mapped into the process's address space. When this
  55. // happens, initialize the CRITICAL_SECTION objects being used for
  56. // synchronization. We enclose the call to InitializeCriticalSection in
  57. // a try-except block cause its possible for it to raise a
  58. // STATUS_NO_MEMORY exception.
  59. //
  60. try {
  61. InitializeCriticalSection(&FRS_ThrdCounter);
  62. InitializeCriticalSection(&FRC_ThrdCounter);
  63. } except(EXCEPTION_EXECUTE_HANDLER) {
  64. ShouldPerfmonCollectData = FALSE;
  65. return(TRUE);
  66. }
  67. #ifdef INCLLOGGING
  68. //
  69. // Create/Open a Key under the Application key for logging purposes.
  70. // Here, the return status is intentionally not checked because even
  71. // if we fail, we return TRUE. EventLogging is not critically important.
  72. // Returning FALSE will cause the process loading this DLL to terminate.
  73. //
  74. WStatus = RegCreateKeyEx (HKEY_LOCAL_MACHINE,
  75. NTFRSPERF,
  76. 0L,
  77. NULL,
  78. REG_OPTION_NON_VOLATILE,
  79. KEY_ALL_ACCESS,
  80. NULL,
  81. &Key,
  82. &flag);
  83. if (WStatus != ERROR_SUCCESS) {
  84. DoLogging = FALSE;
  85. break;
  86. }
  87. //
  88. // Set the values EventMessageFile and TypesSupported. Return value is
  89. // intentionally not checked (see above).
  90. //
  91. WStatus = RegSetValueEx(Key,
  92. L"EventMessageFile",
  93. 0L,
  94. REG_EXPAND_SZ,
  95. (BYTE *)EVENTLOGDLL,
  96. (1 + wcslen(EVENTLOGDLL)) * sizeof(WCHAR));
  97. if (WStatus != ERROR_SUCCESS) {
  98. DoLogging = FALSE;
  99. RegCloseKey(Key);
  100. break;
  101. }
  102. WStatus = RegSetValueEx(Key,
  103. L"TypesSupported",
  104. 0L,
  105. REG_DWORD,
  106. (BYTE *)&TypesSupported,
  107. sizeof(DWORD));
  108. if (WStatus != ERROR_SUCCESS) {
  109. DoLogging = FALSE;
  110. RegCloseKey(Key);
  111. break;
  112. }
  113. //
  114. // Close the key
  115. //
  116. RegCloseKey(Key);
  117. //
  118. // Get the handle used to report errors in the event log. Return value
  119. // is intentionally not checked (see above).
  120. //
  121. hEventLog = RegisterEventSource((LPCTSTR)NULL,
  122. (LPCTSTR)L"NTFRSPerf");
  123. if (hEventLog == NULL) {
  124. DoLogging = FALSE;
  125. }
  126. #endif // INCLLOGGING
  127. break;
  128. case DLL_THREAD_ATTACH:
  129. //
  130. // A thread is being created. Nothing to do.
  131. //
  132. break;
  133. case DLL_THREAD_DETACH:
  134. //
  135. // A thread is exiting cleanly. Nothing to do.
  136. //
  137. break;
  138. case DLL_PROCESS_DETACH:
  139. //
  140. // The DLL is being unmapped from the process's address space. Free up
  141. // the resources.
  142. //
  143. if (ShouldPerfmonCollectData) {
  144. DeleteCriticalSection(&FRS_ThrdCounter);
  145. DeleteCriticalSection(&FRC_ThrdCounter);
  146. }
  147. #ifdef INCLLOGGING
  148. if (DoLogging) {
  149. DeregisterEventSource(hEventLog);
  150. }
  151. #endif // INCLLOGGING
  152. break;
  153. }
  154. return(TRUE);
  155. }