diff options
Diffstat (limited to 'src/ext/Util/ca/scanet.cpp')
| -rw-r--r-- | src/ext/Util/ca/scanet.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/ext/Util/ca/scanet.cpp b/src/ext/Util/ca/scanet.cpp new file mode 100644 index 00000000..11ee487d --- /dev/null +++ b/src/ext/Util/ca/scanet.cpp | |||
| @@ -0,0 +1,50 @@ | |||
| 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 | #include "scanet.h" | ||
| 5 | |||
| 6 | |||
| 7 | HRESULT GetDomainServerName(LPCWSTR pwzDomain, LPWSTR* ppwzServerName, ULONG flags) | ||
| 8 | { | ||
| 9 | DWORD er = ERROR_SUCCESS; | ||
| 10 | PDOMAIN_CONTROLLER_INFOW pDomainControllerInfo = NULL; | ||
| 11 | HRESULT hr = S_OK; | ||
| 12 | |||
| 13 | if (pwzDomain && *pwzDomain) | ||
| 14 | { | ||
| 15 | er = ::DsGetDcNameW(NULL, pwzDomain, NULL, NULL, flags, &pDomainControllerInfo); | ||
| 16 | if (RPC_S_SERVER_UNAVAILABLE == er) | ||
| 17 | { | ||
| 18 | // MSDN says, if we get the above error code, try again with the "DS_FORCE_REDISCOVERY" flag | ||
| 19 | er = ::DsGetDcNameW(NULL, pwzDomain, NULL, NULL, flags | DS_FORCE_REDISCOVERY, &pDomainControllerInfo); | ||
| 20 | } | ||
| 21 | |||
| 22 | if (ERROR_SUCCESS == er && pDomainControllerInfo->DomainControllerName) | ||
| 23 | { | ||
| 24 | // Skip the \\ prefix if present. | ||
| 25 | if ('\\' == *pDomainControllerInfo->DomainControllerName && '\\' == *pDomainControllerInfo->DomainControllerName + 1) | ||
| 26 | { | ||
| 27 | hr = StrAllocString(ppwzServerName, pDomainControllerInfo->DomainControllerName + 2, 0); | ||
| 28 | ExitOnFailure(hr, "failed to allocate memory for string"); | ||
| 29 | } | ||
| 30 | else | ||
| 31 | { | ||
| 32 | hr = StrAllocString(ppwzServerName, pDomainControllerInfo->DomainControllerName, 0); | ||
| 33 | ExitOnFailure(hr, "failed to allocate memory for string"); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | else | ||
| 37 | { | ||
| 38 | StrAllocString(ppwzServerName, pwzDomain, 0); | ||
| 39 | hr = HRESULT_FROM_WIN32(er); | ||
| 40 | ExitOnFailure(hr, "failed to contact domain %ls", pwzDomain); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | LExit: | ||
| 45 | if (pDomainControllerInfo) | ||
| 46 | { | ||
| 47 | ::NetApiBufferFree((LPVOID)pDomainControllerInfo); | ||
| 48 | } | ||
| 49 | return hr; | ||
| 50 | } | ||
