Accessing ATL COM DLL object from VB.NET using delegate (asynchronous)
Hello.
I have developed a COM object using ATL. It seems to work fine when
accessing it from VB.NET most of the time.
However, I want to use a delegate in VB to asynchronously run a method in
one of the interfaces in my COM module (using Delegate.BeginInvoke()). The
interface seems to be "junk" when accessing it from the delegated VB method.
When called synchronously from the same thread using the Delegate.Invoke(),
it seems to work fine.
Anyone want to take a crack at helping me on this? I'd be very greatful.
Thanks!
- Roger
=============================
The object is defined as follows:
=============================
IDL:
=============================
[
uuid(0C3D816E-4413-456E-BEE1-A13DC7E60BE7),
version(1.0),
helpstring("TEST COM 1.0 Type Library")
]
library TestComModuleLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(7635E876-D811-4895-8D0D-B7700CCE1D9E),
helpstring("TestCOM Class")
]
coclass TestCOM
{
interface I_TestInterface1;
interface I_TestInterface2;
};
};
C++:
=============================
class ATL_NO_VTABLE CTestCOM :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CTestCOM, &CLSID_TestCOM>,
public I_TestInterface1,
public I_TestInterface2,
public ISupportErrorInfo
{
public:
CTestCOM();
DECLARE_REGISTRY_RESOURCEID(IDR_TESTCOM)
BEGIN_COM_MAP(CTestCOM)
COM_INTERFACE_ENTRY(I_TestInterface1)
COM_INTERFACE_ENTRY(I_TestInterface2)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct();
void FinalRelease();
public:
...
};
=============================
The VB.NET code is as follows:
=============================
Public Class TestClass
Inherits System.Windows.Forms.Form
Windows Forms Designer generated code
Private m_TestComModule As TestComModuleLib.TestCOM = New
TestComModuleLib.TestCOM
Private m_TestInterface1 As TestComModuleLib.I_TestInterface2 =
m_TestComModule
Friend Delegate Sub Test_Delegate()
Private Sub ButtonTest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles m_ctlButtonTest.Click
Dim tdTest As New Test_Delegate(AddressOf Test_Method)
tdTest.BeginInvoke(Nothing, Nothing) ' Does not work!! Reference to
m_TestInterface2 crashes app.
'tdTest.Invoke() ' Works just fine, but this is synchronous which I
don't want!!
End Sub
Private Sub Test_Method()
'
' m_TestInterface2 is garbage when this method is invoked by
BeginInvoke( ).
' m_TestInterface2 is fine and works correctly when this method is
invoked by Invoke( ).
'
If (m_TestInterface2 .TestMethod()) Then
...
End If
End Sub
End Class