3,055
社区成员
发帖
与我相关
我的任务
分享
void MyFunc(LPVOID pDlg)
{
CTestDlg* pDlg2=(CTestDlg*)pDlg;
IHTMLDocument2*pHTMLDocument2=(IHTMLDocument2*)(pDlg2->m_brow.GetDocument());
HRESULT hr;
USES_CONVERSION;
CComQIPtr< IHTMLElementCollection > spElementCollection;
hr = pHTMLDocument2->get_forms( &spElementCollection ); //取得表单集合
if ( FAILED( hr ) )
{
return;
}
long nFormCount=0; //取得表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
return;
}
CComQIPtr< IHTMLFormElement > spFormElement;
for(long i=0; i<nFormCount; i++)
{
IDispatch *pDisp = NULL; //取得第 i 项表单
hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
if ( FAILED( hr ) ) continue;
spFormElement = pDisp;
pDisp->Release();
long nElemCount=0; //取得表单中 域 的数目
hr = spFormElement->get_length( &nElemCount );
if ( FAILED( hr ) ) continue;
for(long j=0; j<nElemCount; j++)
{
CComDispatchDriver spInputElement; //取得第 j 项表单域
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
if ( FAILED( hr ) ) continue;
CComVariant vName,vVal,vType; //取得表单域的 名,值,类型
hr = spInputElement.GetPropertyByName( L"name", &vName ); /***执行此举程序内存错误***/
if( FAILED( hr ) ) continue;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void CTestDlg::OnButtonStart()
{
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)MyFunc,this,0,&ThreadID); //内存泄露
MyFunc(this); //没问题
}
error C2664: “ATL::CComGITPtr<T>::CComGITPtr(T *)”: 不能将参数 1 从“IHTMLDocument2 *”转换为“IHTMLDocument2 **”
1> with
1> [
1> T=IHTMLDocument2 *
1> ]
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
//注册并且获得cookie
HRESULT RegisterMyInterface(IMyInterface* pmi, DWORD* pdwCookie) {
// this is usually a global
IGlobalInterfaceTable* g_pGIT = NULL;
HRESULT hr = ::CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void**)&g_pGIT);
ATLASSERT(SUCCEEDED(hr));
hr = g_pGIT->RegisterInterfaceInGlobal(pmi,
__uuidof(pmi), pdwCookie);
return hr;
}
//通过cookie取得接口
HRESULT ReadMyInterface(DWORD dwCookie) {
// ... GIT pointer obtained elsewhere
IMyInterface* pmi = NULL;
hr = g_pGIT->GetInterfaceFromGlobal(dwCookie,
__uuidof(pmi), (void**)&pmi);
// use pmi as usual
return hr;
}
//注销cookie
g_pGIT->RevokeInterfaceFromGlobal(m_dwCookie);
//比如你的主线程:
IHTMLDocument2*pHTMLDocument2=(IHTMLDocument2*)(pDlg2->m_brow.GetDocument());
CComGITPtr<IHTMLDocument2*> pgit(pHTMLDocument2);
DWORD dwCookie=pgit.Detach();
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)MyFunc,dwCookie,0,&ThreadID); //内存泄露
//MyFunc(this); //没问题
//你的子线程:
void MyFunc(DWORD cookie){
OleInitialize(NULL);
CComGITPtr<IHTMLDocument2> pgit(cookie);
CComPtr<IHTMLDocument2> pHTMLDocument2;
if(FAILED(pgit.CopyTo(&pHTMLDocument2))){
AtlTrace(_T("Failed in GIT to PTR"));
CoUninitialize();
return;
}
pgit.Revoke();
//你的代码
...
CoUninitialize();
}