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.

166 lines
4.4 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File:
  7. //
  8. // Contents:
  9. //
  10. // History: 12-09-97 HueiWang Modified from MSDN RPC Service Sample
  11. //
  12. //---------------------------------------------------------------------------
  13. #include <windows.h>
  14. #include <tchar.h>
  15. #include <stdio.h>
  16. #include "common.h"
  17. //---------------------------------------------------------------------------
  18. // FUNCTION: LogEvent( DWORD dwEventType,
  19. // DWORD dwIdEvent,
  20. // WORD cStrings,
  21. // LPTSTR *apwszStrings);
  22. //
  23. // PURPOSE: add the event to the event log
  24. //
  25. // INPUT: the event ID to report in the log, the number of insert
  26. // strings, and an array of null-terminated insert strings
  27. //
  28. // RETURNS: none
  29. //---------------------------------------------------------------------------
  30. HRESULT LogEvent(LPTSTR lpszSource,
  31. DWORD dwEventType,
  32. WORD wCatalog,
  33. DWORD dwIdEvent,
  34. WORD cStrings,
  35. TCHAR **apwszStrings)
  36. {
  37. HANDLE hAppLog=NULL;
  38. BOOL bSuccess=FALSE;
  39. WORD wElogType;
  40. wElogType = (WORD) dwEventType;
  41. if(hAppLog=RegisterEventSource(NULL, lpszSource))
  42. {
  43. bSuccess = ReportEvent(hAppLog,
  44. wElogType,
  45. wCatalog,
  46. dwIdEvent,
  47. NULL,
  48. cStrings,
  49. 0,
  50. (const TCHAR **) apwszStrings,
  51. NULL);
  52. DeregisterEventSource(hAppLog);
  53. }
  54. return((bSuccess) ? S_OK : GetLastError());
  55. }
  56. //---------------------------------------------------------------------------
  57. // FUNCTION: GetLastErrorText
  58. //
  59. // PURPOSE: copies error message text to string
  60. //
  61. // PARAMETERS:
  62. // lpszBuf - destination buffer
  63. // dwSize - size of buffer
  64. //
  65. // RETURN VALUE:
  66. // destination buffer
  67. //
  68. // COMMENTS:
  69. //
  70. LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize )
  71. {
  72. DWORD dwRet;
  73. LPTSTR lpszTemp = NULL;
  74. dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  75. NULL,
  76. GetLastError(),
  77. LANG_NEUTRAL,
  78. (LPTSTR)&lpszTemp,
  79. 0,
  80. NULL );
  81. // supplied buffer is not long enough
  82. if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) )
  83. lpszBuf[0] = TEXT('\0');
  84. else
  85. {
  86. lpszTemp[lstrlen(lpszTemp)-2] = _TEXT('\0'); //remove cr and newline character
  87. _sntprintf( lpszBuf, dwSize, _TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
  88. }
  89. if ( lpszTemp )
  90. LocalFree((HLOCAL) lpszTemp );
  91. return lpszBuf;
  92. }
  93. BOOL
  94. ConvertWszToBstr(
  95. OUT BSTR *pbstr,
  96. IN WCHAR const *pwc,
  97. IN LONG cb)
  98. {
  99. BOOL fOk = FALSE;
  100. BSTR bstr;
  101. do
  102. {
  103. bstr = NULL;
  104. if (NULL != pwc)
  105. {
  106. if (-1 == cb)
  107. {
  108. cb = wcslen(pwc) * sizeof(WCHAR);
  109. }
  110. bstr = SysAllocStringByteLen((char const *) pwc, cb);
  111. if (NULL == bstr)
  112. {
  113. break;
  114. }
  115. }
  116. if (NULL != *pbstr)
  117. {
  118. SysFreeString(*pbstr);
  119. }
  120. *pbstr = bstr;
  121. fOk = TRUE;
  122. } while (FALSE);
  123. return(fOk);
  124. }
  125. BOOL ConvertBstrToWsz(
  126. IN BSTR pbstr,
  127. OUT LPWSTR * pWsz)
  128. {
  129. LPWSTR wstrRequest = NULL;
  130. int wLen = 0;
  131. int returnvalue;
  132. if (pWsz != NULL)
  133. SysFreeString((BSTR) *pWsz);
  134. returnvalue = MultiByteToWideChar(GetACP(), 0, (LPCSTR) pbstr, -1, *pWsz, wLen);
  135. if (returnvalue == 0)
  136. {
  137. return FALSE;
  138. }
  139. else if (wLen == 0)
  140. {
  141. if((*pWsz = (LPWSTR) SysAllocStringLen (NULL, returnvalue)) == NULL)
  142. return FALSE;
  143. returnvalue = MultiByteToWideChar(GetACP(), 0, (LPCSTR) pbstr, -1, *pWsz, returnvalue);
  144. if (returnvalue == 0)
  145. {
  146. return FALSE;
  147. }
  148. else
  149. SysFreeString(pbstr);
  150. }
  151. return TRUE;
  152. }