1
A
回答
0
#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".");
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);
}
相關問題
- 1. UNC指向本地計算機上某個文件夾的路徑
- 2. 從本地路徑或映射路徑獲取UNC路徑
- 3. php本地機器路徑
- 4. 使用unc路徑識別唯一使用unc路徑的機器
- 5. 遠程計算機的本地計算機的UNC路徑是什麼?
- 6. File.Copy從UNC路徑(同一服務器)UNC路徑查詢
- 7. Path.GetDirectoryName UNC路徑
- 8. 檢查路徑是UNC路徑還是本地路徑的正確方法是什麼?
- 9. 「找不到路徑的一部分」尋找一個UNC路徑
- 10. forfiles與UNC路徑
- 11. Response.Redirect到UNC路徑
- 12. wcf svclog unc路徑?
- 13. 找出ASP.NET請求是否來自本地機器
- 14. 查找本地路徑getUnlockedImageUri
- 15. 將HTTP路徑轉換爲UNC路徑?
- 16. 將路徑轉換爲UNC路徑
- 17. confirmation_url指向本地主機
- 18. 如何測試主機名是否指本地機器
- 19. 輪包腳本指向本地Python路徑
- 20. 如何將UNC轉換爲本地路徑
- 21. 在另一臺服務器上查找UNC路徑?
- 22. Win32 ShellExecute和UNC路徑
- 23. Vim緩慢UNC路徑
- 24. Amazon S3和UNC路徑
- 25. 從unc路徑Acess exe
- 26. UNC路徑.exists()返回false
- 27. 使shlex.split尊重UNC路徑
- 28. UNC路徑文件URI
- 29. 如何(快速)檢查UNC路徑是否可用
- 30. 驗證是否路徑指向另一個路徑給定目錄
的[PathIsSameRoot](http://msdn.microsoft.com/en-us/library/windows/desktop/bb773687%28v=vs.85%29.aspx)函數聽起來有前途的。不過,我不知道它是否接受UNC路徑。 – 2012-04-05 18:30:53