none
保存的ESP的值是不正确的跨函数调用。 RRS feed

  • 问题

  • 错误原文如下:The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

    我在调用微软的IE的COM组件解析HTML网页。我在网上查找资料,网友说那时堆栈平衡的问题。解决方案是把调用者的堆栈平衡的参数改成__stdcall。我修改函数声明,把堆栈平衡设置为 __stdcall。我还把工程属性的 CODE GENERATION 参数设置由默认的 __cdecl 改为 __stdcall。这些修改都没有解决问题。问题发生在以下代码中。当执行“item->getAttribute(...)”的时候错误发生。

    IHTMLElement* WeatherToolForMsn::FindElementForMshtml(IHTMLElement *e, BSTR tagName,BSTR attrName, BSTR attrValue)
    {
    	
    	ASSERT(tagName!=NULL);
    	ASSERT(wcslen(tagName)>0);
    	ASSERT(e!=NULL);
    	ASSERT((attrName==NULL&&attrValue==NULL)||(wcslen(attrName)>0&&wcslen(attrValue)>0));
    	VARIANT xh;
    	xh.vt=VT_I4;
    	long len=0;
    	IHTMLElementCollection* elems=NULL;
    	e->get_children((IDispatch**)&elems);
    	elems->get_length(&len);
    	IHTMLElement *result=NULL;
    	VARIANT value;
    	value.vt=VT_BSTR;
    	for (long i=0;i<len;i++)
    	{
    		IHTMLElement* item=NULL;
    		BSTR tag=NULL;
    		bool found=false;
    		xh.lVal=i;
    		elems->item(xh,xh,(IDispatch**)&item);
    		if(!item)
    			continue;
    		item->get_tagName(&tag);
    		bool found1=wcscmp(attrName,tag)==0;
    		bool found2=true;
    		if(attrName!=NULL)
    		{
    			item->getAttribute(attrName,1,&value);
    			found2=wcscmp(value.bstrVal,attrValue)==0;
    		}
    		found=found1&&found2;
    		::SysFreeString(tag);
    		if(found)
    		{
    			item->QueryInterface(IID_IHTMLElement,(LPVOID*)&result);
    			break;
    		}
    	}
    	return result;
    }
    问题在哪里呢?我应该如何解决呢?谢谢。


    妆台秋思




    2014年10月22日 16:19

答案

  • 你好:

         从你上面的代码来看,应该是get_children()用错了,试一下下面这些代码:

    e->get_children((IDispatch**)&elems);
    pDispatch->QueryInterface(IID_IHTMLElementCollection, (VOID**)&elems);
    elems->get_length(&len);
    

    Shu Hu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月23日 2:23
  • 谢谢,在您的帮助下我解决问题了。新的代码如下:

    IHTMLElement* WeatherToolForMsn::FindElementForMshtml(IHTMLElement *e, BSTR tagName,BSTR attrName, BSTR attrValue)
    {
    	ASSERT(tagName!=NULL);
    	ASSERT(wcslen(tagName)>0);
    	ASSERT(e!=NULL);
    	ASSERT((attrName==NULL&&attrValue==NULL)||(wcslen(attrName)>0&&wcslen(attrValue)>0));
    	locale zhcn;
    	VARIANT xh;
    	xh.vt=VT_I4;
    	long len=0;
    	IHTMLElementCollection* elems=NULL;
    	IDispatch *disp=NULL;
    	e->get_children(&disp);
    	disp->QueryInterface(IID_IHTMLElementCollection,(LPVOID*)&elems);
    	elems->get_length(&len);
    	IHTMLElement *result=NULL;
    	VARIANT value;
    	value.vt=VT_BSTR;
    	for (long i=0;i<len;i++)
    	{
    		IHTMLElement* item=NULL;
    		BSTR tag=NULL;
    		bool found=false;
    		xh.lVal=i;
    		elems->item(xh,xh,&disp);
    		disp->QueryInterface(IID_IHTMLElement,(LPVOID*)&item);
    		if(!item)
    			continue;
    		item->get_tagName(&tag);
    		TRACE(tag);
    		TRACE(L"    ");
    		CString strTag=tag;
    		strTag.MakeLower();
    		bool found1=wcscmp(tagName,strTag)==0;
    		bool found2=true;
    		if(attrName!=NULL)
    		{
    			item->getAttribute(attrName,1,&value);
    			if(value.bstrVal==NULL)
    				found2=false;
    			else
    			{
    				TRACE(value.bstrVal);
    				CString strValue=value.bstrVal;
    				strValue.MakeLower();
    				found2=wcscmp(attrValue,strValue)==0;
    			}
    		}
    		found=found1&&found2;
    		::SysFreeString(tag);
    		if(found)
    		{
    			result=item;
    			return result;
    		}
    		item->Release();
    		TRACE(L"\r\n");
    	}
    	elems->Release();
    	return result;
    }


    妆台秋思



    2014年10月23日 14:59

全部回复

  • 你好:

         从你上面的代码来看,应该是get_children()用错了,试一下下面这些代码:

    e->get_children((IDispatch**)&elems);
    pDispatch->QueryInterface(IID_IHTMLElementCollection, (VOID**)&elems);
    elems->get_length(&len);
    

    Shu Hu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月23日 2:23
  • 谢谢,在您的帮助下我解决问题了。新的代码如下:

    IHTMLElement* WeatherToolForMsn::FindElementForMshtml(IHTMLElement *e, BSTR tagName,BSTR attrName, BSTR attrValue)
    {
    	ASSERT(tagName!=NULL);
    	ASSERT(wcslen(tagName)>0);
    	ASSERT(e!=NULL);
    	ASSERT((attrName==NULL&&attrValue==NULL)||(wcslen(attrName)>0&&wcslen(attrValue)>0));
    	locale zhcn;
    	VARIANT xh;
    	xh.vt=VT_I4;
    	long len=0;
    	IHTMLElementCollection* elems=NULL;
    	IDispatch *disp=NULL;
    	e->get_children(&disp);
    	disp->QueryInterface(IID_IHTMLElementCollection,(LPVOID*)&elems);
    	elems->get_length(&len);
    	IHTMLElement *result=NULL;
    	VARIANT value;
    	value.vt=VT_BSTR;
    	for (long i=0;i<len;i++)
    	{
    		IHTMLElement* item=NULL;
    		BSTR tag=NULL;
    		bool found=false;
    		xh.lVal=i;
    		elems->item(xh,xh,&disp);
    		disp->QueryInterface(IID_IHTMLElement,(LPVOID*)&item);
    		if(!item)
    			continue;
    		item->get_tagName(&tag);
    		TRACE(tag);
    		TRACE(L"    ");
    		CString strTag=tag;
    		strTag.MakeLower();
    		bool found1=wcscmp(tagName,strTag)==0;
    		bool found2=true;
    		if(attrName!=NULL)
    		{
    			item->getAttribute(attrName,1,&value);
    			if(value.bstrVal==NULL)
    				found2=false;
    			else
    			{
    				TRACE(value.bstrVal);
    				CString strValue=value.bstrVal;
    				strValue.MakeLower();
    				found2=wcscmp(attrValue,strValue)==0;
    			}
    		}
    		found=found1&&found2;
    		::SysFreeString(tag);
    		if(found)
    		{
    			result=item;
    			return result;
    		}
    		item->Release();
    		TRACE(L"\r\n");
    	}
    	elems->Release();
    	return result;
    }


    妆台秋思



    2014年10月23日 14:59