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.

220 lines
6.8 KiB

  1. //--------------------------------------------------------------------
  2. // DebugWPrintf - implementation
  3. // Copyright (C) Microsoft Corporation, 1999
  4. //
  5. // Created by: Louis Thomas (louisth), 2-7-99
  6. //
  7. // Debugging print routines
  8. //
  9. #include <nt.h>
  10. #include <ntrtl.h>
  11. #include <nturtl.h>
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #include <strsafe.h>
  16. /*
  17. bool g_bWindowCreated=false;
  18. HWND g_hwDbg=NULL;
  19. HWND g_hwOuter=NULL;
  20. HANDLE g_hThread=NULL;
  21. HANDLE g_hThreadReady=NULL;
  22. */
  23. /*
  24. //--------------------------------------------------------------------
  25. static LRESULT CALLBACK DwpWinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  26. switch (uMsg)
  27. {
  28. case WM_CREATE:
  29. *(HWND *)(((CREATESTRUCT *)lParam)->lpCreateParams)=CreateWindowEx(
  30. WS_EX_CLIENTEDGE,
  31. L"EDIT", // predefined class
  32. NULL, // no window title
  33. WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY,
  34. 0, 0, 0, 0, // set size in WM_SIZE message
  35. hwnd, // parent window
  36. NULL, // edit control ID
  37. (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
  38. NULL); // pointer not needed
  39. return 0;
  40. case WM_SETFOCUS:
  41. SetFocus(g_hwDbg);
  42. return 0;
  43. case WM_SIZE:
  44. // Make the edit control the size of the window's
  45. // client area.
  46. MoveWindow(g_hwDbg,
  47. 0, 0, // starting x- and y-coordinates
  48. LOWORD(lParam), // width of client area
  49. HIWORD(lParam), // height of client area
  50. TRUE); // repaint window
  51. return 0;
  52. case WM_DESTROY:
  53. PostQuitMessage(0);
  54. return 0;
  55. default:
  56. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  57. }
  58. return 0;
  59. }
  60. //--------------------------------------------------------------------
  61. static BOOL DwpRegWinClass(void) {
  62. WNDCLASSEX wcx;
  63. // Fill in the window class structure with parameters
  64. // that describe the main window.
  65. wcx.cbSize=sizeof(wcx); // size of structure
  66. wcx.style=CS_NOCLOSE; // redraw if size changes
  67. wcx.lpfnWndProc=DwpWinProc; // points to window procedure
  68. wcx.cbClsExtra=0; // no extra class memory
  69. wcx.cbWndExtra=0; // no extra window memory
  70. wcx.hInstance=GetModuleHandle(NULL); // handle to instance
  71. wcx.hIcon=LoadIcon(NULL, IDI_APPLICATION); // predefined app. icon
  72. wcx.hCursor=LoadCursor(NULL, IDC_ARROW); // predefined arrow
  73. wcx.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); // white background brush
  74. wcx.lpszMenuName=NULL; // name of menu resource
  75. wcx.lpszClassName=L"DwpWin"; // name of window class
  76. wcx.hIconSm=NULL; // small class icon
  77. // Register the window class.
  78. return RegisterClassEx(&wcx);
  79. }
  80. //--------------------------------------------------------------------
  81. static DWORD WINAPI DebugWPrintfMsgPump(void * pvIgnored) {
  82. MSG msg;
  83. DwpRegWinClass();
  84. g_hwOuter=CreateWindow(
  85. L"DwpWin",
  86. L"DebugWPrintf",
  87. // WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
  88. WS_VISIBLE | WS_OVERLAPPEDWINDOW,
  89. CW_USEDEFAULT,
  90. CW_USEDEFAULT,
  91. 300,
  92. 200,
  93. NULL,
  94. NULL,
  95. NULL,
  96. &g_hwDbg);
  97. if (g_hwOuter) {
  98. SetWindowLongPtr(g_hwOuter, GWLP_WNDPROC, (LONG_PTR)DwpWinProc);
  99. SetWindowPos(g_hwOuter, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
  100. }
  101. SetEvent(g_hThreadReady);
  102. if (g_hwOuter) {
  103. while (GetMessage(&msg, g_hwOuter, 0, 0 )>0) {
  104. TranslateMessage(&msg);
  105. DispatchMessage(&msg);
  106. }
  107. }
  108. return S_OK;
  109. }
  110. */
  111. //--------------------------------------------------------------------
  112. void DebugWPrintf_(const WCHAR * wszFormat, ...) {
  113. #if DBG
  114. HRESULT hr;
  115. WCHAR wszBuf[1024];
  116. va_list vlArgs;
  117. va_start(vlArgs, wszFormat);
  118. hr = StringCchVPrintf(wszBuf, sizeof(wszBuf)/sizeof(wszBuf[0]), wszFormat, vlArgs);
  119. va_end(vlArgs);
  120. if (FAILED(hr)) {
  121. // strsafe caught a possible overrun...
  122. return;
  123. }
  124. {
  125. UNICODE_STRING UnicodeString;
  126. ANSI_STRING AnsiString;
  127. NTSTATUS Status;
  128. RtlInitUnicodeString(&UnicodeString,wszBuf);
  129. Status = RtlUnicodeStringToAnsiString(&AnsiString,&UnicodeString,TRUE);
  130. if ( !NT_SUCCESS(Status) ) {
  131. AnsiString.Buffer = "";
  132. }
  133. KdPrintEx((DPFLTR_W32TIME_ID, DPFLTR_TRACE_LEVEL, AnsiString.Buffer));
  134. if ( NT_SUCCESS(Status) ) {
  135. RtlFreeAnsiString(&AnsiString);
  136. }
  137. }
  138. // do basic output
  139. // OutputDebugStringW(wszBuf);
  140. if (_fileno(stdout) >= 0)
  141. wprintf(L"%s", wszBuf);
  142. #endif
  143. /*
  144. // convert \n to \r\n
  145. unsigned int nNewlines=0;
  146. WCHAR * wszTravel=wszBuf;
  147. while (NULL!=(wszTravel=wcschr(wszTravel, L'\n'))) {
  148. wszTravel++;
  149. nNewlines++;
  150. }
  151. WCHAR * wszSource=wszBuf+wcslen(wszBuf);
  152. WCHAR * wszTarget=wszSource+nNewlines;
  153. while (nNewlines>0) {
  154. if (L'\n'==(*wszTarget=*wszSource)) {
  155. wszTarget--;
  156. *wszTarget=L'\r';
  157. nNewlines--;
  158. }
  159. wszTarget--;
  160. wszSource--;
  161. }
  162. // create a window if there is not one already
  163. if (false==g_bWindowCreated) {
  164. g_bWindowCreated=true;
  165. g_hThreadReady=CreateEvent(NULL, TRUE, FALSE, NULL);
  166. if (NULL!=g_hThreadReady) {
  167. DWORD dwThreadID;
  168. g_hThread=CreateThread(NULL, 0, DebugWPrintfMsgPump, NULL, 0, &dwThreadID);
  169. if (NULL!=g_hThread) {
  170. WaitForSingleObject(g_hThreadReady, INFINITE);
  171. }
  172. CloseHandle(g_hThreadReady);
  173. g_hThreadReady=NULL;
  174. }
  175. }
  176. if (NULL!=g_hwDbg) {
  177. SendMessage(g_hwDbg, EM_SETSEL, SendMessage(g_hwDbg, WM_GETTEXTLENGTH, 0, 0), -1);
  178. SendMessage(g_hwDbg, EM_REPLACESEL, FALSE, (LPARAM)wszBuf);
  179. }
  180. */
  181. }
  182. //--------------------------------------------------------------------
  183. void DebugWPrintfTerminate_(void) {
  184. /*
  185. MessageBox(NULL, L"Done.\n\nPress OK to close.", L"DebugWPrintfTerminate", MB_OK | MB_ICONINFORMATION);
  186. if (NULL!=g_hwOuter) {
  187. PostMessage(g_hwOuter, WM_CLOSE, 0,0);
  188. if (NULL!=g_hThread) {
  189. WaitForSingleObject(g_hThread, INFINITE);
  190. }
  191. }
  192. */
  193. }