2011-11-09 140 views
4

我想要使用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 *」

+0

作爲目前寫的,你想通過wsURL.c_str()來gethostbyname函數。你不是想傳遞LogURL.c_str()嗎? –

回答

1

這似乎是工作。評論歡迎。

int Newlength = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0, NULL, NULL); 
std::string NewLogURL(Newlength+1, 0); 
int Newresult = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &NewLogURL[0],Newlength+1, NULL, NULL); 

    HOSTENT *pHostEnt; 
    int **ppaddr; 
    SOCKADDR_IN sockAddr; 
    char* addr; 

    pHostEnt = gethostbyname(NewLogURL.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); 
0

調用WideCharToMultiByte是Win32 API調用,這是否在一天結束的參數不符,儘管這取決於哪些框架你使用(MFC,WTL等),可能有更好的方法。

+0

這裏沒有MFC。這是我在代碼中的更深層次,它的工作原理。但是,當我嘗試使用LogURL時,我無法將std字符串轉換爲const char。 int length = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wsURL.c_str(),-1,NULL,0,NULL,NULL); \t \t std :: string LogURL(length + 1,0); (CP_ACP,WC_COMPOSITECHECK,wsURL.c_str(),-1,&LogURL [0],length + 1,NULL,NULL);}結果如下:int result = WideCharToMultiByte \t \t myfile <<「\ n當前網址:」<< LogURL; \t \t myfile <<「\ n當前網站IP:」<< addr; –

+0

Yee Gads,對不起,格式化這個問題!看看我添加它的問題,它在那裏更加清晰。 –

3

我喜歡使用wcstombs(),因爲它非常易於使用。

試試這個示例:

char *str = new char[4046]; 
wchar_t array[] = L"Hello World"; 
wcstombs(str, array, 12); 
std::cout << str; 

這是你怎麼也得爲wchar_t轉換成char *。

編輯

發生在你的代碼:

char* addr = new char[4046]; 
wcstombs(wsURL, addr, wsURL.size()); 
pHostEnt = gethostbyname(addr); 
+0

我在看這個,我不確定你的意思?你能否在我的背景下進一步解釋,以便我能理解。謝謝。 –

+0

@ME:描述模板類basic_string與wchar_t類型的元素作爲wstring的特化的類型。 – karthik

+0

@ME:所以我們可以使用wcstombs函數將wchar_t轉換爲const char *。在我的答案的Edit部分中,我建議您的問題的解決方案。 – karthik

0
/***** This code is well done *****/ 

    #include... 
    #include... 

    int wmain(int argc, wchar_t *argv[]) 
    { 
     ... 
     ... 
     char *path = new char[255]; 
     wcstombs(path, argv[2], 255); 
     IplImage *img; 
    if (img = cvLoadImage (path, 1)) 
     { 
      Mat input_img = Mat (img); 
      imshow ("haha",input_img); 
      waitKey(0); 
     } 
     ... 
     ... 
     //wcout<<endl<<argv[2]; 
    }