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.

308 lines
6.6 KiB

  1. // regtrace.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "regtrace.h"
  5. #include "regtrdlg.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char BASED_CODE THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CRegTraceApp
  12. BEGIN_MESSAGE_MAP(CRegTraceApp, CWinApp)
  13. //{{AFX_MSG_MAP(CRegTraceApp)
  14. // NOTE - the ClassWizard will add and remove mapping macros here.
  15. // DO NOT EDIT what you see in these blocks of generated code!
  16. //}}AFX_MSG
  17. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  18. END_MESSAGE_MAP()
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CRegTraceApp construction
  21. CRegTraceApp::CRegTraceApp()
  22. {
  23. m_hRegKey = NULL;
  24. m_hRegMachineKey = NULL;
  25. m_szCmdLineServer[0] = '\0';
  26. }
  27. /////////////////////////////////////////////////////////////////////////////
  28. // The one and only CRegTraceApp object
  29. CRegTraceApp theApp;
  30. char
  31. CRegTraceApp::m_szDebugAsyncTrace[] = "SOFTWARE\\Microsoft\\MosTrace\\CurrentVersion\\DebugAsyncTrace";
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CRegTraceApp initialization
  34. BOOL CRegTraceApp::InitInstance()
  35. {
  36. // Standard initialization
  37. // If you are not using these features and wish to reduce the size
  38. // of your final executable, you should remove from the following
  39. // the specific initialization routines you do not need.
  40. Enable3dControls();
  41. LoadStdProfileSettings(); // Load standard INI file options (including MRU)
  42. //
  43. // check for cmd line param for remote server
  44. //
  45. m_szCmdLineServer[0] = '\0';
  46. if ( m_lpCmdLine && m_lpCmdLine[0] == '\\' && m_lpCmdLine[1] == '\\' )
  47. {
  48. LPSTR lpsz1, lpsz2;
  49. int i;
  50. for ( i=0, lpsz1=m_lpCmdLine, lpsz2=m_szCmdLineServer;
  51. i<sizeof(m_szCmdLineServer) && *lpsz1 && *lpsz1 != ' ';
  52. i++, *lpsz2++ = *lpsz1++ ) ;
  53. *lpsz2 = '\0';
  54. }
  55. //
  56. // if the user specified the local machine; skip remote stuff
  57. //
  58. char szLocalMachine[sizeof(m_szCmdLineServer)];
  59. DWORD dwSize = sizeof(szLocalMachine);
  60. GetComputerName( szLocalMachine, &dwSize );
  61. //
  62. // skip the \\ prefix
  63. //
  64. if ( lstrcmpi( szLocalMachine, m_szCmdLineServer+2 ) == 0 )
  65. {
  66. m_szCmdLineServer[0] = '\0';
  67. }
  68. //
  69. // make usre this succeeds before calling Page constructors
  70. //
  71. LONG lError = OpenTraceRegKey();
  72. if ( lError != ERROR_SUCCESS )
  73. {
  74. PVOID lpsz;
  75. CString szFormat;
  76. CString szCaption;
  77. CString szText;
  78. //
  79. // user aborted
  80. //
  81. if ( lError == -1 )
  82. {
  83. return FALSE;
  84. }
  85. FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
  86. FORMAT_MESSAGE_FROM_SYSTEM,
  87. (LPCVOID)NULL,
  88. lError,
  89. MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ),
  90. (LPTSTR)&lpsz,
  91. 16,
  92. NULL );
  93. szCaption.LoadString( IDS_ERROR_CAPTION );
  94. szFormat.LoadString( IDS_ERROR_TEXT );
  95. szText.Format( (LPCTSTR)szFormat, lpsz, lError );
  96. LocalFree( lpsz );
  97. MessageBeep(0);
  98. MessageBox( NULL, szText, NULL, MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL );
  99. return FALSE;
  100. }
  101. CString szCaption;
  102. if ( IsRemoteMsnServer() )
  103. {
  104. CString szFormat;
  105. szFormat.LoadString( IDS_REMOTE_CAPTION );
  106. szCaption.Format( (LPCTSTR)szFormat, GetRemoteServerName() );
  107. }
  108. else
  109. {
  110. szCaption.LoadString( IDS_TRACE_CAPTION );
  111. }
  112. CRegPropertySheet dlg( (LPCTSTR)szCaption );
  113. CRegTracePage TracesPage;
  114. CRegOutputPage OutputPage;
  115. CRegThreadPage ThreadPage;
  116. if (TracesPage.InitializePage() &&
  117. OutputPage.InitializePage() &&
  118. ThreadPage.InitializePage() )
  119. {
  120. dlg.AddPage( &TracesPage );
  121. dlg.AddPage( &OutputPage );
  122. dlg.AddPage( &ThreadPage );
  123. dlg.DoModal();
  124. }
  125. CloseTraceRegKey();
  126. // Since the dialog has been closed, return FALSE so that we exit the
  127. // application, rather than start the application's message pump.
  128. return FALSE;
  129. }
  130. DWORD ConnectThread( CConnectDlg *lpConnectDlg )
  131. {
  132. LONG lError;
  133. HKEY hRegMachineKey;
  134. lError = RegConnectRegistry(App.GetRemoteServerName(),
  135. HKEY_LOCAL_MACHINE,
  136. &hRegMachineKey );
  137. App.SetRemoteRegKey( hRegMachineKey );
  138. lpConnectDlg->PostMessage( WM_COMMAND, IDOK, NULL );
  139. return (DWORD)lError;
  140. }
  141. LONG CRegTraceApp::OpenTraceRegKey()
  142. {
  143. DWORD dwDisposition;
  144. LONG lError;
  145. //
  146. // check for cache of the remote hkey value
  147. //
  148. if ( IsRemoteMsnServer() )
  149. {
  150. DWORD dwThreadId;
  151. HANDLE hThread = ::CreateThread(NULL,
  152. 0,
  153. (LPTHREAD_START_ROUTINE)ConnectThread,
  154. (LPVOID)&m_dlgConnect,
  155. 0,
  156. &dwThreadId );
  157. if ( hThread == NULL )
  158. {
  159. return GetLastError();
  160. }
  161. if ( m_dlgConnect.DoModal() == IDCANCEL )
  162. {
  163. return -1;
  164. }
  165. WaitForSingleObject( hThread, INFINITE );
  166. GetExitCodeThread( hThread, (LPDWORD)&lError );
  167. CloseHandle( hThread );
  168. if ( lError != ERROR_SUCCESS )
  169. {
  170. return lError;
  171. }
  172. }
  173. HKEY hRoot = IsRemoteMsnServer() ?
  174. m_hRegMachineKey :
  175. HKEY_LOCAL_MACHINE;
  176. return RegCreateKeyEx( hRoot,
  177. m_szDebugAsyncTrace,
  178. 0,
  179. NULL,
  180. REG_OPTION_NON_VOLATILE,
  181. KEY_READ|KEY_WRITE,
  182. NULL,
  183. &m_hRegKey,
  184. &dwDisposition );
  185. }
  186. BOOL CRegTraceApp::CloseTraceRegKey()
  187. {
  188. BOOL bRC = RegCloseKey( m_hRegKey ) == ERROR_SUCCESS;
  189. if ( IsRemoteMsnServer() && m_hRegMachineKey != NULL )
  190. {
  191. bRC == RegCloseKey( m_hRegMachineKey ) == ERROR_SUCCESS && bRC;
  192. }
  193. return bRC;
  194. }
  195. BOOL CRegTraceApp::GetTraceRegDword( LPTSTR pszValue, LPDWORD pdw )
  196. {
  197. DWORD cbData = sizeof( DWORD );
  198. DWORD dwType = REG_DWORD;
  199. return RegQueryValueEx(m_hRegKey,
  200. pszValue,
  201. NULL,
  202. &dwType,
  203. (LPBYTE)pdw,
  204. &cbData ) == ERROR_SUCCESS && dwType == REG_DWORD;
  205. }
  206. BOOL CRegTraceApp::GetTraceRegString( LPTSTR pszValue, CString& sz )
  207. {
  208. DWORD dwType = REG_DWORD;
  209. char szTemp[MAX_PATH+1];
  210. DWORD cbData = sizeof(szTemp);
  211. BOOL bRC;
  212. bRC = RegQueryValueEx( m_hRegKey,
  213. pszValue,
  214. NULL,
  215. &dwType,
  216. (LPBYTE)szTemp,
  217. &cbData ) == ERROR_SUCCESS && dwType == REG_SZ;
  218. if ( bRC )
  219. {
  220. sz = szTemp;
  221. }
  222. return bRC;
  223. }
  224. BOOL CRegTraceApp::SetTraceRegDword( LPTSTR pszValue, DWORD dwData )
  225. {
  226. return RegSetValueEx( m_hRegKey,
  227. pszValue,
  228. NULL,
  229. REG_DWORD,
  230. (LPBYTE)&dwData,
  231. sizeof( DWORD ) ) == ERROR_SUCCESS;
  232. }
  233. BOOL CRegTraceApp::SetTraceRegString( LPTSTR pszValue, CString& sz )
  234. {
  235. return RegSetValueEx( m_hRegKey,
  236. pszValue,
  237. NULL,
  238. REG_SZ,
  239. (LPBYTE)(LPCTSTR)sz,
  240. sz.GetLength()+1 ) == ERROR_SUCCESS;
  241. }