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.

363 lines
9.9 KiB

  1. // pdlcnfig.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "pdlcnfig.h"
  5. #include "OutPage.h"
  6. #include "SetPage.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CPdlConfigApp
  14. BEGIN_MESSAGE_MAP(CPdlConfigApp, CWinApp)
  15. //{{AFX_MSG_MAP(CPdlConfigApp)
  16. // NOTE - the ClassWizard will add and remove mapping macros here.
  17. // DO NOT EDIT what you see in these blocks of generated code!
  18. //}}AFX_MSG
  19. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  20. END_MESSAGE_MAP()
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CPdlConfigApp construction
  23. CPdlConfigApp::CPdlConfigApp()
  24. {
  25. // TODO: add construction code here,
  26. // Place all significant initialization in InitInstance
  27. }
  28. /////////////////////////////////////////////////////////////////////////////
  29. // The one and only CPdlConfigApp object
  30. CPdlConfigApp theApp;
  31. /////////////////////////////////////////////////////////////////////////////
  32. // Test for installation of the service.
  33. LONG CPdlConfigApp::PerfLogServiceStatus()
  34. {
  35. HKEY hKeyLogService;
  36. LONG lStatus;
  37. // try opening the key to the service
  38. lStatus = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
  39. TEXT("SYSTEM\\CurrentControlSet\\Services\\PerfDataLog"),
  40. 0,
  41. KEY_READ | KEY_WRITE,
  42. &hKeyLogService);
  43. if (lStatus == ERROR_SUCCESS) {
  44. // don't keep the key open
  45. RegCloseKey (hKeyLogService);
  46. }
  47. return lStatus;
  48. }
  49. LONG CPdlConfigApp::ServiceFilesCopied()
  50. {
  51. HANDLE hFile = INVALID_HANDLE_VALUE;
  52. LONG lStatus = ERROR_SUCCESS;
  53. TCHAR szFullPathName[MAX_PATH];
  54. ExpandEnvironmentStrings (
  55. TEXT("%windir%\\system32\\pdlsvc.exe"),
  56. szFullPathName, MAX_PATH);
  57. hFile = CreateFile (
  58. szFullPathName,
  59. GENERIC_READ,
  60. FILE_SHARE_READ | FILE_SHARE_WRITE,
  61. NULL,
  62. OPEN_EXISTING,
  63. FILE_ATTRIBUTE_NORMAL,
  64. NULL);
  65. if (hFile == INVALID_HANDLE_VALUE) {
  66. lStatus = GetLastError();
  67. } else {
  68. CloseHandle (hFile);
  69. lStatus = ERROR_SUCCESS;
  70. }
  71. return lStatus;
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CPdlConfigApp initialization
  75. LONG CPdlConfigApp::CreatePerfDataLogService ()
  76. {
  77. LONG lStatus = ERROR_SUCCESS;
  78. HKEY hKeyServices = NULL;
  79. HKEY hKeyPerfLog = NULL;
  80. HKEY hKeyPerfAlert = NULL;
  81. DWORD dwWaitLimit = 20;
  82. SC_HANDLE hSC;
  83. SC_HANDLE hLogService;
  84. // create service
  85. hSC = OpenSCManager (NULL, NULL, SC_MANAGER_CREATE_SERVICE);
  86. if (hSC == NULL) {
  87. // display error message
  88. lStatus = GetLastError();
  89. }
  90. if (lStatus == ERROR_SUCCESS) {
  91. hLogService = CreateService (hSC,
  92. TEXT("PerfDataLog"),
  93. TEXT("Performance Data Log"),
  94. SERVICE_ALL_ACCESS,
  95. SERVICE_WIN32_OWN_PROCESS,
  96. SERVICE_DEMAND_START,
  97. SERVICE_ERROR_NORMAL,
  98. TEXT("%systemroot%\\system32\\pdlsvc.exe"),
  99. NULL,
  100. NULL,
  101. NULL,
  102. NULL,
  103. NULL);
  104. if (hLogService == NULL) {
  105. lStatus = GetLastError();
  106. } else {
  107. lStatus = ERROR_SUCCESS;
  108. CloseServiceHandle (hLogService);
  109. }
  110. if (lStatus == ERROR_SUCCESS) {
  111. // wait until the registry is updated before continuing
  112. while (lStatus = RegOpenKeyEx (
  113. HKEY_LOCAL_MACHINE,
  114. TEXT("System\\CurrentControlSet\\Services\\PerfDataLog"),
  115. 0,
  116. KEY_READ | KEY_WRITE,
  117. &hKeyPerfLog) != ERROR_SUCCESS) {
  118. Sleep (500); // wait .5 seconds and try again
  119. if (--dwWaitLimit == 0) {
  120. AfxMessageBox (IDS_SC_CREATE_ERROR);
  121. break;
  122. }
  123. }
  124. if (lStatus == ERROR_SUCCESS) {
  125. RegCloseKey (hKeyPerfLog);
  126. }
  127. }
  128. CloseServiceHandle (hSC);
  129. }
  130. return lStatus;
  131. }
  132. LONG CPdlConfigApp::InitPerfDataLogRegistry ()
  133. {
  134. LONG lStatus = ERROR_SUCCESS;
  135. HKEY hKeyPerfLog = NULL;
  136. HKEY hKeyLogQueries = NULL;
  137. HKEY hKeyLogQueriesDefault = NULL;
  138. HKEY hKeyPerfAlert = NULL;
  139. HKEY hKeyAlertQueries = NULL;
  140. HKEY hKeyAlertQueriesDefault = NULL;
  141. HKEY hKeyEventLogApplication = NULL;
  142. HKEY hKeyEventLogPerfDataLog = NULL;
  143. HKEY hKeyEventLogPerfDataAlert = NULL;
  144. DWORD dwValue;
  145. DWORD dwDisposition;
  146. // open registry key
  147. lStatus = RegOpenKeyEx (
  148. HKEY_LOCAL_MACHINE,
  149. TEXT("System\\CurrentControlSet\\Services\\PerfDataLog"),
  150. 0,
  151. KEY_READ | KEY_WRITE,
  152. &hKeyPerfLog);
  153. ASSERT (lStatus == ERROR_SUCCESS);
  154. if (lStatus != ERROR_SUCCESS) goto Close_And_Exit;
  155. // add registry subkeys for Log Queries
  156. lStatus = RegCreateKeyEx (
  157. hKeyPerfLog,
  158. TEXT("Log Queries"),
  159. 0,
  160. NULL,
  161. REG_OPTION_NON_VOLATILE,
  162. KEY_READ | KEY_WRITE,
  163. NULL,
  164. &hKeyLogQueries,
  165. &dwDisposition);
  166. ASSERT (lStatus == ERROR_SUCCESS);
  167. if (lStatus != ERROR_SUCCESS) goto Close_And_Exit;
  168. lStatus = RegCreateKeyEx (
  169. hKeyLogQueries,
  170. TEXT("Default"),
  171. 0,
  172. NULL,
  173. REG_OPTION_NON_VOLATILE,
  174. KEY_READ | KEY_WRITE,
  175. NULL,
  176. &hKeyLogQueriesDefault,
  177. &dwDisposition);
  178. ASSERT (lStatus == ERROR_SUCCESS);
  179. if (lStatus != ERROR_SUCCESS) goto Close_And_Exit;
  180. // open registry key
  181. lStatus = RegOpenKeyEx (
  182. HKEY_LOCAL_MACHINE,
  183. TEXT("System\\CurrentControlSet\\Services\\EventLog\\Application"),
  184. 0,
  185. KEY_READ | KEY_WRITE,
  186. &hKeyEventLogApplication);
  187. ASSERT (lStatus == ERROR_SUCCESS);
  188. if (lStatus != ERROR_SUCCESS) goto Close_And_Exit;
  189. // add registry subkeys for event log
  190. lStatus = RegCreateKeyEx (
  191. hKeyEventLogApplication,
  192. TEXT("PerfDataLog"),
  193. 0,
  194. NULL,
  195. REG_OPTION_NON_VOLATILE,
  196. KEY_READ | KEY_WRITE,
  197. NULL,
  198. &hKeyEventLogPerfDataLog,
  199. &dwDisposition);
  200. ASSERT (lStatus == ERROR_SUCCESS);
  201. if (lStatus != ERROR_SUCCESS) goto Close_And_Exit;
  202. lStatus = RegSetValueEx (hKeyEventLogPerfDataLog,
  203. TEXT("EventMessageFile"),
  204. 0,
  205. REG_SZ,
  206. (BYTE *)TEXT("%systemroot%\\system32\\pdlsvc.exe"),
  207. (lstrlen(TEXT("%systemroot%\\system32\\pdlsvc.exe"))+1) * sizeof (TCHAR));
  208. ASSERT (lStatus == ERROR_SUCCESS);
  209. if (lStatus != ERROR_SUCCESS) goto Close_And_Exit;
  210. dwValue = 7;
  211. lStatus = RegSetValueEx (hKeyEventLogPerfDataLog,
  212. TEXT("TypesSupported"),
  213. 0,
  214. REG_DWORD,
  215. (BYTE *)&dwValue,
  216. sizeof (DWORD));
  217. ASSERT (lStatus == ERROR_SUCCESS);
  218. if (lStatus != ERROR_SUCCESS) goto Close_And_Exit;
  219. Close_And_Exit:
  220. if (hKeyPerfLog != NULL) RegCloseKey (hKeyPerfLog);
  221. if (hKeyLogQueries != NULL) RegCloseKey (hKeyLogQueries);
  222. if (hKeyLogQueriesDefault != NULL) RegCloseKey (hKeyLogQueriesDefault);
  223. if (hKeyEventLogApplication != NULL) RegCloseKey (hKeyEventLogApplication);
  224. if (hKeyEventLogPerfDataLog != NULL) RegCloseKey (hKeyEventLogPerfDataLog);
  225. return lStatus;
  226. }
  227. /////////////////////////////////////////////////////////////////////////////
  228. // CPdlConfigApp initialization
  229. BOOL CPdlConfigApp::InitInstance()
  230. {
  231. LONG lServiceStatus;
  232. CString csMessage;
  233. BOOL bReturn = TRUE;
  234. // Standard initialization
  235. // If you are not using these features and wish to reduce the size
  236. // of your final executable, you should remove from the following
  237. // the specific initialization routines you do not need.
  238. #ifdef _AFXDLL
  239. Enable3dControls(); // Call this when using MFC in a shared DLL
  240. #else
  241. Enable3dControlsStatic(); // Call this when linking to MFC statically
  242. #endif
  243. lServiceStatus = PerfLogServiceStatus();
  244. if (lServiceStatus == ERROR_FILE_NOT_FOUND) {
  245. lServiceStatus = ServiceFilesCopied();
  246. if (lServiceStatus == ERROR_SUCCESS) {
  247. if (AfxMessageBox (IDS_QUERY_INSTALL,
  248. MB_OKCANCEL | MB_ICONQUESTION) == IDOK) {
  249. lServiceStatus = CreatePerfDataLogService();
  250. if (lServiceStatus == ERROR_SUCCESS) {
  251. lServiceStatus = InitPerfDataLogRegistry();
  252. }
  253. if (lServiceStatus != ERROR_SUCCESS) {
  254. csMessage.FormatMessage (lServiceStatus);
  255. AfxMessageBox (csMessage);
  256. bReturn = FALSE;
  257. } else {
  258. // the service was successfully installed so display
  259. // message
  260. AfxMessageBox (IDS_SERVICE_INSTALLED);
  261. }
  262. } else {
  263. // the service is not installed and the user doesn't
  264. // want it installed so exit
  265. bReturn = FALSE;
  266. }
  267. } else {
  268. // then the service has not yet been installed so bail
  269. AfxMessageBox (IDS_SERVICE_NOT_INSTALLED);
  270. bReturn = FALSE;
  271. }
  272. } else if (lServiceStatus == ERROR_ACCESS_DENIED) {
  273. AfxMessageBox (IDS_ACCESS_DENIED, MB_OK | MB_ICONEXCLAMATION);
  274. bReturn = FALSE;
  275. } else if (lServiceStatus != ERROR_SUCCESS) {
  276. AfxMessageBox (IDS_REGISTRY_ERROR, MB_OK | MB_ICONEXCLAMATION);
  277. bReturn = FALSE;
  278. } else {
  279. bReturn = TRUE;
  280. }
  281. if (bReturn) {
  282. CPropertySheet PSheet;
  283. COutputPropPage POutput;
  284. CSettingsPropPage PSettings;
  285. m_pMainWnd = &PSheet;
  286. csMessage.LoadString (IDS_PROPERTY_SHEET_CAPTION);
  287. PSheet.SetTitle(csMessage);
  288. PSheet.AddPage(&PSettings);
  289. PSheet.AddPage(&POutput);
  290. INT_PTR nResponse = PSheet.DoModal();
  291. if (nResponse == IDOK)
  292. {
  293. // TODO: Place code here to handle when the dialog is
  294. // dismissed with OK
  295. }
  296. else if (nResponse == IDCANCEL)
  297. {
  298. // TODO: Place code here to handle when the dialog is
  299. // dismissed with Cancel
  300. }
  301. bReturn = FALSE;
  302. }
  303. // Since the dialog has been closed, return FALSE so that we exit the
  304. // application, rather than start the application's message pump.
  305. return bReturn;
  306. }