【发布时间】:2011-11-09 02:33:42
【问题描述】:
我正在尝试使用 GetHostByName() 这需要一个 const char*。我的 URL 位于成本 wchar_t* 格式的变量中。如何转换它以便 GetHostByName 可以使用它?代码。
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;
这是我得到的错误。 IntelliSense:“const wchar_t *”类型的参数与“const char *”类型的参数不兼容
【问题讨论】:
-
正如目前所写的,您正在尝试将 wsURL.c_str() 传递给 gethostbyname 函数。你不想传递 LogURL.c_str() 吗?
标签: windows visual-c++