Windows 将常量wchar\u t*转换为常量字符*

Windows 将常量wchar\u t*转换为常量字符*,windows,visual-c++,Windows,Visual C++,我正在尝试使用GetHostByName(),这需要一个常量字符*。我的URL位于一个cost wchar\u t*格式的变量中。如何将其转换为GetHostByName可以使用它?密码 BSTR bstr; pBrowser->get_LocationURL(&bstr); std::wstring wsURL; wsURL = bstr; size_t DSlashLoc = wsURL.find(L"://"); if (DSlashLoc != wsURL.npos)

我正在尝试使用GetHostByName(),这需要一个常量字符*。我的URL位于一个cost wchar\u 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;
BSTR-BSTR;
pBrowser->get_LocationURL(&bstr);
std::wstring wsURL;
wsURL=bstr;
size_t DSlashLoc=wsURL.find(L):/”;
if(DSlashLoc!=wsURL.npos)
{
擦除(wsURL.begin(),wsURL.begin()+DSlashLoc+3);
}
DSlashLoc=wsURL.find(L“www.”);
如果(DSlashLoc==0)
{
擦除(wsURL.begin(),wsURL.begin()+4);
}
DSlashLoc=wsURL.find(L/);
if(DSlashLoc!=wsURL.npos)
{
wsURL.erase(DSlashLoc);
}
wprintf(L“\n当前网站URL:%s\n\n”,wsURL.c_str());
霍森特*光能;
国际**ppaddr;
SOCKADDR_在SOCKADDR中;
char*addr;
pHostEnt=gethostbyname(wsURL.c_str());
ppaddr=(int**)光能->地址列表;
sockAddr.sin_addr.s_addr=**ppaddr;
地址=inet\u ntoa(sockAddr.sin\u地址);
printf(“\n当前网站IP:%s”,地址);
int length=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wsURL.c_str(),-1,NULL,0,NULL,NULL);
std::字符串LogURL(长度+1,0);
int result=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wsURL.c_str(),-1,&LogURL[0],长度+1,NULL,NULL);

myfileWideCharToMultiByte是一种在一天结束时执行此操作的Win32 API调用,不过根据您使用的框架(MFC、WTL等),可能有更好的方法。

我喜欢使用wcstombs(),因为它非常容易使用

请尝试以下示例:

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

这似乎奏效了。欢迎评论

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);
/****这段代码做得很好*****/
#包括。。。
#包括。。。
int wmain(int argc,wchar_t*argv[])
{
...
...
char*path=新字符[255];
wcstombs(路径,argv[2],255);
IplImage*img;
if(img=cvLoadImage(路径1))
{
Mat输入_img=Mat(img);
imshow(“哈哈”,输入\ img);
等待键(0);
}
...
...

//wcoutNo MFC在这里。这是我在代码中进一步介绍的内容,它可以工作。但是当我尝试使用LogURL时,我无法将std字符串转换为const char.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],长度+1,NULL,NULL);myfile Yee Gads,很抱歉,在格式化此文件时遇到问题!请看我添加的问题,上面的问题更清楚。我正在查看此文件,但不确定您的意思?请您在我的上下文中进一步解释,以便我能理解。谢谢。@ME:一个类型,它描述了模板类basic_字符串的特殊化,其中包含w类型的元素char\u t作为wstring@ME:因此我们可以使用wcstombs函数将wchar\u t转换为const char*。在我答案的编辑部分,我建议您的问题的解决方案。此行导致转换问题…wcstombs(wsURL,addr,wsURL.length());@ME:第三个参数是wsURL字符串的长度。因此,您必须计算长度并将该值作为当前写入的第三个参数传递。您正在尝试将wsURL.c_str()传递给gethostbyname函数。您不想改为传递LogURL.c_str()吗?
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);
    /***** 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];
    }