为什么发生此错误?引用计数增加,线程模型是单个公寓。 Coll-object和EmptyColl-function都位于一个dll中。 ATL项目的默认调用转换是__stdcall。与此dll内的其他对象发生同样的错误。当打开观察窗口将COM对象传递给VB 6时VariantClear异常
与空物体清除VARIANT VariantClear时抛出异常:
异常在VB6.EXE在0x75C14974(的oleaut32.dll)抛出:0000005:
访问冲突读取位置0x00000008。
frmMain.frm(错误,见下面为什么):
Private Sub Form_Load()
Dim c As Coll
Set c = EmptyColl
'error when ends here with variable "c" in the watch window.
End Sub
frmMain.frm(没有错误):
Private Sub Form_Load()
Dim c2 As Coll 'instead of Coll can be any object of same library
Set c2 = New Coll 'creation
Set c2 = Nothing 'destroying (optionaly)
Dim c As Coll
Set c = EmptyColl
'no error
End Sub
filyus.idl:
[
object,
uuid(6FA7FAEB-5CE3-4A80-9288-2667EE5E7596),
dual,
nonextensible,
pointer_default(unique)
]
interface IColl : IDispatch{
//some methods
};
[
uuid(157F3D2F-A427-4D5A-B908-87868297EA43),
version(1.0),
]
library Filyus
{
importlib("stdole2.tlb");
[
dllname("Filyus")
]
module Filyus{
[entry("EmptyColl")]
HRESULT EmptyColl([out, retval] IColl** Coll);
}
};
filyus.def:
LIBRARY
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
DllInstall PRIVATE
EmptyColl
ole.h:
extern HRESULT EmptyColl(IColl** Coll);
ole.cpp:
HRESULT EmptyColl(IColl** Coll) {
HRESULT hr; CComObject<CColl>* Object;
if (Coll != nullptr) {
hr = CComObject<CColl>::CreateInstance(&Object);
if (hr == S_OK) {
Object->AddRef();
*Coll = Object; //same error with using QueryInterface
}
}
else hr = E_POINTER;
return hr;
}
“*同样的错误与此DLL *内其他物体发生” - 那么显然,你正在做的事情你的DLL里面根本错误的。请提供[最小,完整和可验证的示例](http://stackoverflow.com/help/mcve),其中显示了更多的DLL代码。特别是,它是如何声明和设置'CColl'类以及其他您遇到问题的类。 –