| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | |
| 6 | // Exit macros |
| 7 | #define UncExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_UNCUTIL, x, s, __VA_ARGS__) |
| 8 | #define UncExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_UNCUTIL, x, s, __VA_ARGS__) |
| 9 | #define UncExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_UNCUTIL, x, s, __VA_ARGS__) |
| 10 | #define UncExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_UNCUTIL, x, s, __VA_ARGS__) |
| 11 | #define UncExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_UNCUTIL, x, s, __VA_ARGS__) |
| 12 | #define UncExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_UNCUTIL, x, s, __VA_ARGS__) |
| 13 | #define UncExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_UNCUTIL, p, x, e, s, __VA_ARGS__) |
| 14 | #define UncExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_UNCUTIL, p, x, s, __VA_ARGS__) |
| 15 | #define UncExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_UNCUTIL, p, x, e, s, __VA_ARGS__) |
| 16 | #define UncExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_UNCUTIL, p, x, s, __VA_ARGS__) |
| 17 | #define UncExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_UNCUTIL, e, x, s, __VA_ARGS__) |
| 18 | #define UncExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_UNCUTIL, g, x, s, __VA_ARGS__) |
| 19 | |
| 20 | DAPI_(HRESULT) UncConvertFromMountedDrive( |
| 21 | __inout LPWSTR *psczUNCPath, |
| 22 | __in LPCWSTR sczMountedDrivePath |
| 23 | ) |
| 24 | { |
| 25 | HRESULT hr = S_OK; |
| 26 | DWORD dwLength = 0; |
| 27 | DWORD er = ERROR_SUCCESS; |
| 28 | LPWSTR sczDrive = NULL; |
| 29 | |
| 30 | // Only copy drive letter and colon |
| 31 | hr = StrAllocString(&sczDrive, sczMountedDrivePath, 2); |
| 32 | UncExitOnFailure(hr, "Failed to copy drive"); |
| 33 | |
| 34 | // ERROR_NOT_CONNECTED means it's not a mapped drive |
| 35 | er = ::WNetGetConnectionW(sczDrive, NULL, &dwLength); |
| 36 | if (ERROR_MORE_DATA == er) |
| 37 | { |
| 38 | er = ERROR_SUCCESS; |
| 39 | |
| 40 | hr = StrAlloc(psczUNCPath, dwLength); |
| 41 | UncExitOnFailure(hr, "Failed to allocate string to get raw UNC path of length %u", dwLength); |
| 42 | |
| 43 | er = ::WNetGetConnectionW(sczDrive, *psczUNCPath, &dwLength); |
| 44 | if (ERROR_CONNECTION_UNAVAIL == er) |
| 45 | { |
| 46 | // This means the drive is remembered but not currently connected, this can mean the location is accessible via UNC path but not via mounted drive path |
| 47 | er = ERROR_SUCCESS; |
| 48 | } |
| 49 | UncExitOnWin32Error(er, hr, "::WNetGetConnectionW() failed with buffer provided on drive %ls", sczDrive); |
| 50 | |
| 51 | // Skip drive letter and colon |
| 52 | hr = StrAllocConcat(psczUNCPath, sczMountedDrivePath + 2, 0); |
| 53 | UncExitOnFailure(hr, "Failed to copy rest of database path"); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | if (ERROR_SUCCESS == er) |
| 58 | { |
| 59 | er = ERROR_NO_DATA; |
| 60 | } |
| 61 | |
| 62 | UncExitOnWin32Error(er, hr, "::WNetGetConnectionW() failed on drive %ls", sczDrive); |
| 63 | } |
| 64 | |
| 65 | LExit: |
| 66 | ReleaseStr(sczDrive); |
| 67 | |
| 68 | return hr; |
| 69 | } |