Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Bad Variable exception, when using an ActiveX on WinForm as a seperate Thread.

Hi,

I have typical problem which i am trying for past 4 days. I am newbie to .NET world.
I have a ocx component which i have to use in C#. So i hosted it on a winform and can use it without problems.

How ever if i make this Winform as a seperate thread and use the same interface which is worked in above case(Winform as not a seperate thread) it is giving BAD VARIABLE TYPE exception.

I have used BeginInvoke and delegate method in MainThread to start execution of the member function in WinForm thread. And in Debug->Threads window i can see thread switch happened without any problems. How ever when we call the interface it throws the exception.

I tried to debug the ActiveX code. It fails in ForwardCalltoInvoke... method. I dont understand why it is ailing in second case, as i pass same variable type in both the cases.

Any help would do great for me.

Kalyan
[927 byte] By [sreenivas.hcu] at [2007-11-11 10:26:30]
# 1 Re: Bad Variable exception, when using an ActiveX on WinForm as a seperate Thread.
Hi Some More info on the problem.

The ComInterface is Method(LPDISPATCH x).

In C# we use
cONTROL.METHOD(X);
The Object x is actually creadted from some other vc++DLL SO IT IS ALSO UNMANAGED TYPE.
sO WHEN IT IS CREATED .net represents it as System.__ComObject and while using the above method it is unable to convert from System.__ComObject to LPDISPATCH.

I tried marshal.getiDistpachPointer for explicit marshalling, but it doesnt work.

But if everything is executed in MainThread then it is working fine.
Any clues?
sreenivas.hcu at 2007-11-11 20:47:51 >