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.

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