关于:空白(Internet Explorer) [英] About:Blank (Internet Explorer)

查看:92
本文介绍了关于:空白(Internet Explorer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了一个问题外,我拥有的这段代码非常有用.似乎偶尔会出现about:blank,而GetHostByName()无法对其进行束缚,并且代码将停止.是否可以通过使当前网址为www.google.com或只是让GetHostByName跳过它来调整此代码以解决此问题的方法?这是代码.

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.

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

推荐答案

gethostbyname已弃用.切换到 getaddrinfo .

您的问题可能是您正在引用NULL指针. inet_ntoa()如果失败则返回NULL转换地址.

另外,inet_ntoa()返回的字符串是短暂的.通话结束后无效.您需要先复制字符串,然后才能使用它,而不是引用指向该字符串的指针.像这样的东西:

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



在调试器中单步执行代码以进行确认.

-PaulH



Step through your code in the debugger to confirm.

-PaulH


提示,您如何看待该语句的结果
Hint, what do you think the result of this statement
引用:

wsURL.find( L://");

wsURL.find(L"://");

是在您喂"about:blank"时?您将如何处理该结果?

如果您根本不知道答案,请使用调试器/断点进行查找.

-------------------------
根据对此解决方案的回复进行编辑
-------------------------

您问了

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

报价:

有没有一种方法可以调整此代码以解决此问题

Is there a way that I can adjust this code to handle this problem

,我给了您一点提示在哪里进行调整".该查找"将是寻找其他事物的好地方.毕竟,您在其中拥有查找"的原因是清理了您认为URL中存在的"HTTP://".正是这种假设在这种情况下是无效的("about:blank"),而这仅仅是检查与您的假设不符的事物的地方.吃吧.

PS,您的代码挂断或炸毁或"about:blank"上的内容之类的原因是,您对url看起来像的假设是错误的.您可以花大量时间查看哪里爆炸,也可以花时间有效地修复为什么爆炸(假设).

是的,正如其他响应者所指出的那样,您不是在检查错误/这些函数中的某些函数会返回空指针,这通常会导致崩溃.

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.


这篇关于关于:空白(Internet Explorer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆