我正在尝试在 Internet Explorer 的附加组件中异步运行函数(我正在用 VC++ 编写 BHO)。按照建议here https://stackoverflow.com/questions/4285346/threading-in-bho-atl-com-winapi-or-something-else/4285377#4285377我正在尝试使用 CWorkerThread。
我花了几个小时试图弄清楚,但仍然不知道该怎么做。我在ATL方面没有太多经验。互联网上缺乏好的文档或教程让我很苦恼。
我通过“添加”->“类”创建类并选择“ATL 简单对象”(这就是将类添加到 ATL 项目的方式,对吗?)。但是这个IWorkerThreadClient怎么实现呢?我认为在类视图中选择“添加”->“实现接口”会很好,但列表中没有 IWorkerThreadClient。
我想我不太了解 ATL 或 COM,但找不到学习这个的好资源(特别是newest ATL7).
我什至尝试过winapiCreateThread
方法但它不起作用。我路过this
运行静态方法的类指针,但稍后内存会被破坏。尽管如此,如果它有效,我仍然宁愿使用其他东西而不是CreateThread
.
现在我有类似的东西this http://msdn.microsoft.com/en-us/library/bb250489%28VS.85%29.aspx. In OnDocumentComplete
有RemoveImages(sptmlDoc)
我只想异步运行它。
EDIT:我做了什么CreateThread
:
我尝试运行RemoveImages函数(来自here https://stackoverflow.com/questions/4285346/threading-in-bho-atl-com-winapi-or-something-else/4285377#4285377) 异步。我在类中创建了静态函数,其签名如下here http://msdn.microsoft.com/en-us/library/ms682516%28v=VS.85%29.aspx。 RemoveImages 有参数,所以我将其复制到类的成员中:
if (htmlDoc2 != NULL)
{
m_tmpHtmlDocument2 = htmlDoc2;
m_hThread = CreateThread( NULL, 0, MyThreadFunction, this, 0, &m_threadId);
}
and MyThreadFunction
:
static DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
CHelloWorldBHO* myClass = (CHelloWorldBHO*)lpParam;
myClass->RemoveImages(myClass->m_tmpHtmlDocument2);
return 0;
}
我收到“iexplore.exe 中 0x60c0da05 处未处理的异常:0xC0000005:读取位置 0x000001b8 时发生访问冲突”。这里用粗体行表示:
void CHelloWorldBHO::DontDisplayElement(CComPtr htmlElement)
{
CComPtr style;
HRESULT hr = htmlElement->get_style(&style;);
if (hr == S_OK && style != NULL)
{
static const CComBSTR strNone(L"none");
style->put_display(strNone);
}
}