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.

165 lines
4.9 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: network.cxx
  7. //
  8. // Contents: Network-related helper routines.
  9. //
  10. // Classes: None.
  11. //
  12. // Functions: None.
  13. //
  14. // History: 08-Jul-96 MarkBl Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "..\pch\headers.hxx"
  18. #pragma hdrstop
  19. #include "..\inc\debug.hxx"
  20. #include "..\inc\network.hxx"
  21. typedef DWORD (WINAPI * PWNETGETUNIVERSALNAMEW)(
  22. LPCWSTR,
  23. DWORD,
  24. LPVOID,
  25. LPDWORD);
  26. //+----------------------------------------------------------------------------
  27. //
  28. // Function: GetServerNameFromPath
  29. //
  30. // Synopsis: Return the server name, in UNC form, to which the path
  31. // resolves. If the path resolves locally, the server name
  32. // returned is NULL.
  33. //
  34. // Arguments: [pwszPath] -- Drive-based or UNC path.
  35. // [cbUNCPath] -- Caller allocated buffer size.
  36. // [wszUNCPath] -- Caller allocated buffer to temporarilly
  37. // store the full UNC path. A terminating
  38. // character will be written following the
  39. // server name, so don't expect to use
  40. // the UNC path afterward.
  41. // [ppwszServerName] -- Server name ptr. If non-NULL, it indexes
  42. // wszBuffer.
  43. //
  44. // Returns: S_OK - The path was local or the server name was obtained
  45. // sucessfully from the remote path.
  46. // E_FAIL - Something unexpected occurred (should never happen).
  47. // WNetGetUniversalName error (HRESULT) - When the path is a
  48. // remote path and this call fails for some reason.
  49. //
  50. //-----------------------------------------------------------------------------
  51. HRESULT
  52. GetServerNameFromPath(
  53. LPCWSTR pwszPath,
  54. DWORD cbBufferSize,
  55. WCHAR wszBuffer[],
  56. WCHAR ** ppwszServerName)
  57. {
  58. #define MPR_DLL TEXT("MPR.DLL")
  59. #define WNET_GET_UNIVERSAL "WNetGetUniversalNameW"
  60. schAssert(pwszPath != NULL);
  61. static TCHAR wszDoubleBackslash[] = TEXT("\\\\");
  62. PWNETGETUNIVERSALNAMEW pWNetGetUniversalNameW = NULL;
  63. WCHAR * pwszServerName;
  64. DWORD cbBufferSizeLocal = cbBufferSize;
  65. DWORD Status;
  66. HMODULE hMod;
  67. //
  68. // Is the path provided already a UNC path?
  69. //
  70. if (pwszPath[1] == L'\\')
  71. {
  72. wcscpy(wszBuffer, pwszPath);
  73. goto ParseServerName;
  74. }
  75. //
  76. // Dynamically load/unload mpr.dll to save memory in Win95. Yes, we'll
  77. // take a time hit, but we don't want mpr.dll loaded long-term.
  78. //
  79. hMod = LoadLibrary(MPR_DLL);
  80. if (hMod == NULL)
  81. {
  82. CHECK_HRESULT(HRESULT_FROM_WIN32(GetLastError()));
  83. return(HRESULT_FROM_WIN32(GetLastError()));
  84. }
  85. else
  86. {
  87. pWNetGetUniversalNameW = (PWNETGETUNIVERSALNAMEW)
  88. GetProcAddress(
  89. hMod,
  90. WNET_GET_UNIVERSAL);
  91. if (pWNetGetUniversalNameW == NULL)
  92. {
  93. FreeLibrary(hMod);
  94. CHECK_HRESULT(HRESULT_FROM_WIN32(GetLastError()));
  95. return(HRESULT_FROM_WIN32(GetLastError()));
  96. }
  97. }
  98. Status = pWNetGetUniversalNameW(pwszPath,
  99. UNIVERSAL_NAME_INFO_LEVEL,
  100. wszBuffer,
  101. &cbBufferSizeLocal);
  102. FreeLibrary(hMod);
  103. if (Status == NO_ERROR)
  104. {
  105. ParseServerName:
  106. pwszServerName = wszBuffer;
  107. if (cbBufferSizeLocal > sizeof(wszDoubleBackslash))
  108. {
  109. //
  110. // Isolate server name from full UNC resource path.
  111. //
  112. pwszServerName += (sizeof(wszDoubleBackslash) /
  113. sizeof(WCHAR)) - 1;
  114. for (WCHAR * pwsz = pwszServerName; *pwsz; pwsz++)
  115. {
  116. if (*pwsz == L'\\')
  117. {
  118. *pwsz = L'\0';
  119. break;
  120. }
  121. }
  122. *ppwszServerName = pwszServerName;
  123. }
  124. else
  125. {
  126. //
  127. // This should *never* occur.
  128. //
  129. schAssert(cbBufferSizeLocal > sizeof(wszDoubleBackslash));
  130. return(E_FAIL);
  131. }
  132. }
  133. else if (Status == ERROR_NOT_CONNECTED)
  134. {
  135. *ppwszServerName = NULL;
  136. }
  137. else
  138. {
  139. CHECK_HRESULT(HRESULT_FROM_WIN32(Status));
  140. return(HRESULT_FROM_WIN32(Status));
  141. }
  142. return(S_OK);
  143. }