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.

123 lines
2.8 KiB

  1. //
  2. // NetUtil.cpp
  3. //
  4. #include "Util.h"
  5. #include "debug.h"
  6. #include <devguid.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. HRESULT WINAPI HrFromLastWin32Error()
  11. //+---------------------------------------------------------------------------
  12. //
  13. // Function: HrFromLastWin32Error
  14. //
  15. // Purpose: Converts the GetLastError() Win32 call into a proper HRESULT.
  16. //
  17. // Arguments:
  18. // (none)
  19. //
  20. // Returns: Converted HRESULT value.
  21. //
  22. // Author: danielwe 24 Mar 1997
  23. //
  24. // Notes: This is not inline as it actually generates quite a bit of
  25. // code.
  26. // If GetLastError returns an error that looks like a SetupApi
  27. // error, this function will convert the error to an HRESULT
  28. // with FACILITY_SETUP instead of FACILITY_WIN32
  29. //
  30. {
  31. DWORD dwError = GetLastError();
  32. HRESULT hr;
  33. // This test is testing SetupApi errors only (this is
  34. // temporary because the new HRESULT_FROM_SETUPAPI macro will
  35. // do the entire conversion)
  36. if (dwError & (APPLICATION_ERROR_MASK | ERROR_SEVERITY_ERROR))
  37. {
  38. hr = HRESULT_FROM_SETUPAPI(dwError);
  39. }
  40. else
  41. {
  42. hr = HrFromWin32Error(dwError);
  43. }
  44. return hr;
  45. }
  46. HRESULT WINAPI HrWideCharToMultiByte( const WCHAR* szwString, char** ppszString )
  47. //+---------------------------------------------------------------------------
  48. //
  49. // Function: HrWideCharToMultiByte
  50. //
  51. // Purpose:
  52. //
  53. // Arguments:
  54. //
  55. // Returns: S_OK on success, otherwise an error code
  56. //
  57. // Notes:
  58. //
  59. {
  60. HRESULT hr = E_POINTER;
  61. ASSERT( szwString );
  62. ASSERT( ppszString );
  63. if ( ppszString )
  64. {
  65. *ppszString = NULL;
  66. hr = E_INVALIDARG;
  67. if ( szwString )
  68. {
  69. int iLen = 0;
  70. iLen = WideCharToMultiByte( CP_ACP, 0, szwString, -1, NULL, NULL, NULL, NULL );
  71. if ( 0 < iLen )
  72. {
  73. char* pszName = new char[ iLen ];
  74. if ( NULL != pszName )
  75. {
  76. if ( WideCharToMultiByte( CP_ACP, 0, szwString, -1, pszName, iLen, NULL, NULL ) )
  77. {
  78. hr = S_OK;
  79. *ppszString = pszName;
  80. }
  81. else
  82. {
  83. hr = HrFromLastWin32Error( );
  84. delete [] pszName;
  85. }
  86. }
  87. else
  88. {
  89. hr = E_OUTOFMEMORY;
  90. }
  91. }
  92. else
  93. {
  94. hr = HrFromLastWin32Error( );
  95. }
  96. }
  97. }
  98. return hr;
  99. }
  100. #ifdef __cplusplus
  101. }
  102. #endif