I have a COM library written in C++. For asynchronous operations I use
a callback interface to notify the application of completions. Here's
the interface definition:
interface ICallback : IUnknown
{
HRESULT Execute(IUnknown* caller, DWORD data);
}
It's pretty standard from what I can tell. You pass in an object that
implements ICallback and the Execute method of that object is called
when the operation completes. The object that calls Execute passes a
pointer to itself (this) back via the first parameter, and a result
code via the second.
This all works great so long as the application is written in C++. I'm
having a problem with a sample app I wrote in C#, however. The object
I receive as the first parameter (caller) is not valid. The debugger
shows that it has the correct type, but if you try to access it you
wind up with invalid cast exceptions. Here's the shell of the Execute
method in my C# callback:
public void Execute(object caller, uint Data)
{
// caller has the right type, but it is not a valid object
}
Any ideas about what may be going on here? As far as I can tell, this
is the only place I'm having this kind of problem. Everything else in
the library works fine, and I'm creating, returning, and passing
various objects all over the place. It's just this one place.
I also wrote the same app in VB.NET, and got the exact same exceptions.
Aaron,
>This all works great so long as the application is written in C++. I'm
>having a problem with a sample app I wrote in C#, however. The object
>I receive as the first parameter (caller) is not valid. The debugger
>shows that it has the correct type, but if you try to access it you
>wind up with invalid cast exceptions.
Did you import a type library that defines teh ICallback interface, or
did you write yoru own definition of it in managed code?
Does the callback happen on the same thread or a different one?
Do you have the source code for the COM library?
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
aaron.m.johnson@gmail.com - 05 Jan 2007 15:44 GMT
> Did you import a type library that defines teh ICallback interface, or
> did you write yoru own definition of it in managed code?
I added a reference to the COM library in the project.
> Does the callback happen on the same thread or a different one?
The callback happens in a different thread. The application calls the
asynchronous method which spawns a thread to process the request. This
thread is then used for the callback.
This particular application is just a example to show how the library
works. After making the asynch call, the calling thread just blocks on
a ManualResetEvent that will get set by the callback.
> Do you have the source code for the COM library?
Yeah, I wrote the COM library.
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
aaron.m.johnson@gmail.com - 05 Jan 2007 20:40 GMT
Problem solved.
The application was being started with the STAThread attribute. I
changed it to MTAThread and all is well.