Windows 找出UNC路径是否指向本地计算机

Windows 找出UNC路径是否指向本地计算机,windows,winapi,Windows,Winapi,有没有简单的方法来判断UNC路径是否指向本地计算机。 我发现了以下问题 是否有任何WIN32 API也可以这样做 这个功能听起来很有前途。不过,我不知道它是否接受UNC路径。 #include <windows.h> #include <WinSock.h> #include <string> #include <algorithm> #pragma comment(lib, "wsock32.lib") using namespace std

有没有简单的方法来判断UNC路径是否指向本地计算机。 我发现了以下问题

是否有任何WIN32 API也可以这样做

这个功能听起来很有前途。不过,我不知道它是否接受UNC路径。
#include <windows.h>
#include <WinSock.h>
#include <string>
#include <algorithm>

#pragma comment(lib, "wsock32.lib")

using namespace std;

std::wstring ExtractHostName( const std::wstring &share )
{
if (share.size() < 3 )
    return L"";
size_t pos = share.find(  L"\\", 2 );
wstring server = ( pos != string::npos ) ? share.substr( 2, pos - 2 ) : share.substr( 2 );
transform( server.begin(),server.end(), server.begin(), tolower); 
return server;
}

bool IsIP( const std::wstring &server )
{
size_t invalid = server.find_first_not_of(  L"0123456789." );
bool fIsIP = ( invalid == string::npos );
return fIsIP;
}

bool IsLocalIP( const std::wstring &ipToCheck )
{

if ( ipToCheck == L"127.0.0.1" )
    return true;    

WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 1, 1 );
if ( WSAStartup( wVersionRequested, &wsaData ) != 0 )
    return false;

bool fIsLocal = false;

char hostName[255];
if( gethostname ( hostName, sizeof(hostName)) == 0 )
{
    PHOSTENT hostinfo;      
    if(( hostinfo = gethostbyname(hostName)) != NULL )
    {
        for (int i = 0; hostinfo->h_addr_list[i]; i++)
        {
            char *ip = inet_ntoa(*( struct in_addr *)hostinfo->h_addr_list[i]);
            wchar_t wcIP[100]={0};
            ::MultiByteToWideChar(CP_ACP, 0, ip, -1, wcIP, _countof(wcIP));
            if (ipToCheck == wcIP) 
            {
                fIsLocal = true;
                break;
            }
        }
    }
}
WSACleanup();
return fIsLocal;
}

bool IsLocalHost( const std::wstring &server )
{
if (server == L"localhost")
    return true;    

bool fIsLocalHost = false;

wchar_t buffer[MAX_PATH]={0};
DWORD dwSize =  _countof(buffer);
BOOL fRet = GetComputerName( buffer, &dwSize );
transform( buffer, buffer + dwSize, buffer, tolower); 
fIsLocalHost = ( server == buffer );
return fIsLocalHost;
}

bool ShareIsLocal( const std::wstring &share )
{
wstring server = ExtractHostName( share );
bool fIsIp = IsIP( server );
if ( fIsIp )
    return IsLocalIP( server );
else
    return IsLocalHost( server );
}