我想要使用GetHostByName()這需要一個const char *。我的網址位於一個成本爲wchar_t *格式的變量中。我如何轉換這個GetHostByName可以使用它?代碼。轉const const wchar_t *爲const char *
BSTR bstr;
pBrowser->get_LocationURL(&bstr);
std::wstring wsURL;
wsURL = bstr;
size_t DSlashLoc = wsURL.find(L"://");
if (DSlashLoc != wsURL.npos)
{
wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 3);
}
DSlashLoc = wsURL.find(L"www.");
if (DSlashLoc == 0)
{
wsURL.erase(wsURL.begin(), wsURL.begin() + 4);
}
DSlashLoc = wsURL.find(L"/");
if (DSlashLoc != wsURL.npos)
{
wsURL.erase(DSlashLoc);
}
wprintf(L"\n Current Website URL: %s\n\n", wsURL.c_str());
HOSTENT *pHostEnt;
int **ppaddr;
SOCKADDR_IN sockAddr;
char* addr;
pHostEnt = gethostbyname(wsURL.c_str());
ppaddr = (int**)pHostEnt->h_addr_list;
sockAddr.sin_addr.s_addr = **ppaddr;
addr = inet_ntoa(sockAddr.sin_addr);
printf("\n Current Website IP:%s", addr);
int length = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0, NULL, NULL);
std::string LogURL(length+1, 0);
int result = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &LogURL[0],length+1, NULL, NULL);
myfile << "\n Current Website URL:" << LogURL;
myfile << "\n Current Website IP:"<< addr;
這是我得到的錯誤。 智能感知:類型的「常量爲wchar_t *」的說法是有型「爲const char *」
作爲目前寫的,你想通過wsURL.c_str()來gethostbyname函數。你不是想傳遞LogURL.c_str()嗎? –