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.

175 lines
4.3 KiB

  1. //
  2. // log.cpp
  3. //
  4. // Copyright (c) Microsoft Corp, 1997
  5. //
  6. // This file contains the code necessary to log error messages to the event
  7. // log of a remote machine (or a local machine, depending on the #defines
  8. // below).
  9. //
  10. // Revision History:
  11. //
  12. // Todds 11/13/97 Created
  13. // LarryWin 12/19/97 Modified to provide more error reporting
  14. //
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <winnetwk.h>
  18. #include "log.h"
  19. #pragma warning( disable : 4244) // signed/unsigned mismatch
  20. static BOOL g_IPCInit = FALSE;
  21. // can be defined by calling program; if not defaults to #define in log.h
  22. LPWSTR wszIPC_SHARE = NULL;
  23. LPWSTR wszTARGETMACHINE = NULL;
  24. void SetEventMachine(LPWSTR* pSZ_IPC_SHARE)
  25. {
  26. LPWSTR wszTemp = new wchar_t[80];
  27. wszIPC_SHARE = new wchar_t[100];
  28. wszTARGETMACHINE = new wchar_t[100];
  29. memset(wszTemp, 0, sizeof(wszTemp));
  30. memset(wszIPC_SHARE, 0, sizeof(wszIPC_SHARE));
  31. memset(wszTARGETMACHINE, 0, sizeof(wszTARGETMACHINE));
  32. wszIPC_SHARE = *pSZ_IPC_SHARE;
  33. wcscpy(wszTARGETMACHINE, wszIPC_SHARE);
  34. wcscpy(wszTemp, L"\\\\");
  35. wcscat(wszTemp, wszTARGETMACHINE);
  36. wcscpy(wszTARGETMACHINE, wszTemp);
  37. wcscpy(wszIPC_SHARE, wszTemp);
  38. wcscat(wszIPC_SHARE, L"\\ipc$");
  39. }
  40. void Event(DWORD dwEventType,
  41. LPWSTR wszErr,
  42. DWORD dwErr)
  43. {
  44. wprintf(L"%s", wszErr);
  45. if (!g_IPCInit)
  46. g_IPCInit = OpenIPCConnection();
  47. if (!g_IPCInit) return; // return if IPC connection not established
  48. ErrorToEventLog(
  49. dwEventType,
  50. wszErr,
  51. dwErr
  52. );
  53. }
  54. //
  55. // OpenIPCConnection()
  56. //
  57. // This function opens a \\larrywin1\ipc$ virtual connection to allow logging
  58. // to the event log of the remote machine.
  59. //
  60. // Returns:
  61. //
  62. // True | False, depending on whether or not IPC connection established.
  63. //
  64. BOOL OpenIPCConnection()
  65. {
  66. NETRESOURCE IPCConnection;
  67. DWORD dwRet;
  68. //
  69. // Set up Net Connection to \\todds7\ipc$
  70. //
  71. ZeroMemory(&IPCConnection, sizeof(NETRESOURCE));
  72. IPCConnection.dwType = RESOURCETYPE_ANY;
  73. IPCConnection.lpLocalName = NULL; // virtual connection
  74. if (wszIPC_SHARE != NULL) {
  75. IPCConnection.lpRemoteName = wszIPC_SHARE;
  76. } else {
  77. // get local machine name for share
  78. IPCConnection.lpRemoteName = SZ_IPC_SHARE;
  79. }
  80. IPCConnection.lpProvider = NULL; // use ntlm provider
  81. //
  82. // Try 3 times to establish connection, otherwise fail
  83. //
  84. for (DWORD dwTry = 0;((dwRet != NO_ERROR) && (dwTry < 3)) ; dwTry++)
  85. {
  86. dwRet = WNetAddConnection2(
  87. &IPCConnection,
  88. NULL,
  89. NULL,
  90. 0
  91. );
  92. }
  93. if (dwRet != NO_ERROR) {
  94. dwRet = GetLastError(); // For debugging
  95. return FALSE;
  96. }
  97. return TRUE;
  98. }
  99. BOOL ErrorToEventLog(DWORD dwEventType,
  100. LPWSTR lpszMsg,
  101. DWORD dwErr)
  102. {
  103. WCHAR szMsg[512];
  104. HANDLE hEventSource;
  105. LPWSTR lpszStrings[2];
  106. LPWSTR lpszCRLF = L"\n";
  107. if (wszTARGETMACHINE != NULL) {
  108. hEventSource = RegisterEventSourceW(
  109. wszTARGETMACHINE,
  110. SZ_TEST
  111. );
  112. } else {
  113. // get local machine name
  114. hEventSource = RegisterEventSourceW(
  115. SZ_TARGETMACHINE,
  116. SZ_TEST
  117. );
  118. }
  119. if(hEventSource == NULL)
  120. return FALSE;
  121. // wsprintfW(szMsg, L"%s error: %lu", SZ_TEST, dwErr);
  122. wsprintfW(szMsg, L": 0x%08x", dwErr);
  123. lpszStrings[0] = lpszMsg;
  124. lpszStrings[1] = szMsg;
  125. ReportEventW(hEventSource, // handle of event source
  126. dwEventType, // event type
  127. 0, // event category
  128. dwErr, // event ID
  129. NULL, // current user's SID
  130. 2, // strings in lpszStrings
  131. 0, // no bytes of raw data
  132. (LPCWSTR*)lpszStrings, // array of error strings
  133. NULL // no raw data
  134. );
  135. (VOID) DeregisterEventSource(hEventSource);
  136. OutputDebugStringW(lpszMsg);
  137. OutputDebugStringW(szMsg);
  138. OutputDebugStringW(lpszCRLF);
  139. return TRUE;
  140. }