Click here to Skip to main content
15,884,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code I have works great except for one problem. It seems that occasionally about:blank pops up and GetHostByName() can't habdle it and the code stops. Is there a way that I can adjust this code to handle this problem by either making the current URL www.google.com or perhaps just have GetHostByName skip over it? Here is the code.

C++
void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
 	wVersionRequested = MAKEWORD( 2, 2 );
	err = WSAStartup( wVersionRequested, &wsaData );
	
	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());
		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 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;
		
	SysFreeString(bstr);
}
Posted

gethostbyname is deprecated. Switch to getaddrinfo.

Your problem is probably that you're referencing a NULL pointer. inet_ntoa() returns NULL if it fails to convert an address.

Also, the string returned by inet_ntoa() is ephemeral. It is not valid after the call finishes. You need to copy the string before you can use it rather than referring to a pointer to that string. Something like this:

char myaddress[100] = { 0 };
strcpy( myaddress, inet_ntoa( sockaddr.sin_addr ) );


Step through your code in the debugger to confirm.

-PaulH
 
Share this answer
 
v2
Comments
Member 7766180 9-Nov-11 16:28pm    
Your right. It does return a NULL. How can I make it return 0.0.0.0 if its NULL? I will take your advice about the getaddrinfo as well. Also I did the copying of the string as advised. Thank you.
PS. now that I have the string copied can I reference this string from my CPP file. The string is in a header file.
Paul Heil 9-Nov-11 16:32pm    
You cannot make it return anything other than what it is documented to return. You can check to see if your string is empty after the copy and set it to 0.0.0.0, if you like.
Member 7766180 9-Nov-11 16:46pm    
Thank you. I will try that.
Paul Heil 9-Nov-11 16:34pm    
Yes, you can reference the copied string At any time during its lifetime.
Member 7766180 9-Nov-11 16:47pm    
OK I tried this...

addr2 = inet_ntoa(sockAddr2.sin_addr);
char currentaddress[100] = { 0 };
strcpy( currentaddress, inet_ntoa(sockAddr2.sin_addr ) );
printf("\n Current Website IP:%s", currentaddress);

//char getCurrAdd(char currentaddress);
//{
//return currentaddress;
//}

But it says that return value type doesn't match function type.
Hint, what do you think the result of this statement
Quote:
wsURL.find(L"://");
would be when you feed it "about:blank"? What would you do with that result?

If you don't know the answer off the top of your head, use the debugger / breakpoint to find out.

-------------------------
Edit based on replies to this solution
-------------------------

You asked
Quote:
Is there a way that I can adjust this code to handle this problem
and I gave you a hint of just where to put that "adjustment". That "Find" would be a good place to look for other things. After all, the reason you have a "Find" in there is to clean up the "HTTP://" which you assume is present in the URL. It is that assumption that is invalid for this case ("about:blank") and that's just the place to check for things that don't match your assumption. Have at it.

PS, the reason your code hangs up or blows up or whatever on "about:blank" is that your assumptions about what the url looks like are false. You can spend a lot of time looking at where it blew up or you can spend your time productively fixing why it blew up (the assumptions).

And yes, as the other responder pointed out, you are not checking for errors / null pointer returns from some of these functions and that can often lead to blowing up.
 
Share this answer
 
v3
Comments
Member 7766180 9-Nov-11 14:27pm    
Actually it's returning about:blank..what seems to be hanging the code are these lines...

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

Perhaps on this line...
addr = inet_ntoa(sockAddr.sin_addr);
I could do...
if addr = NULL then
addr = 0.0.0.0
else
addr = addr
end if
Member 7766180 9-Nov-11 16:29pm    
Thank you Chuck. Gettin there!

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