Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I'm trying to get an IP address from an URL. This works fine when I use "www.dog.com" but when I want to use a variable it doesn't work.

C++
HOSTENT *pHostEnt;
int  **ppaddr;
SOCKADDR_IN sockAddr;
char* addr;
		
std::wstring site;
site = wsURL.c_str();
pHostEnt = gethostbyname("site");
ppaddr = (int**)pHostEnt->h_addr_list;
sockAddr.sin_addr.s_addr = **ppaddr;
addr = inet_ntoa(sockAddr.sin_addr);
Posted
Comments
Sergey Alexandrovich Kryukov 8-Nov-11 19:13pm    
Always explain what does it mean "it does not work". Software developers don't say such words, they provide comprehensive issue report. Just forget using "it does not work".
--SA
Member 7766180 8-Nov-11 21:16pm    
Your right. I tried this and received this error.

The code.....
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);

1 IntelliSense: argument of type "const wchar_t *" is incompatible with parameter of type "const char *"

1 solution

How in the world is the API to know that "site" is the name of your variable and not the name of the host you are looking up?

pHostEnt = gethostbyname("site");


Do you see the quotation marks? What do you think that means?

No, I'm not going to give you the code that works, you'll not learn anything other than how to paste and you've demonstrated that you do that well.
 
Share this answer
 
Comments
Member 7766180 8-Nov-11 21:01pm    
Yes I see the quotation marks. You right, they don't belong there.
I tried this...
std::wstring site;
site = wsURL.c_str();
pHostEnt = gethostbyname(site);
and get this error.
1 IntelliSense: no suitable conversion function from "std::wstring" to "const char *" exists
Chuck O'Toole 8-Nov-11 22:42pm    
And after all your discussion with Richard in your other question (http://www.codeproject.com/Questions/280351/myfile-log-printing-weird) you should see that the error is telling you that you are using a "wide string" when the function takes a "char *" string. You've already done the conversion from string to wstring in that question, now do the reverse.

This is the problem with just cut / paste other people code and suggestions without understanding what is going on. You've now mixed wide and regular strings in your code and are having to deal with the consequences.
Member 7766180 8-Nov-11 23:24pm    
Like this?

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());

I'm trying Chuck. I really am! I think that I've learned alot, just a start.
Chuck O'Toole 8-Nov-11 23:26pm    
Are you asking if I think it will work? Wouldn't trying it be the better way of finding out?
Member 7766180 8-Nov-11 23:39pm    
Yes it works. Affirmation would be nice!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900