Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2007

Tip: Looking for answers? Try searching our database.

Invalid callback object instance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aaron.m.johnson@gmail.com - 03 Jan 2007 19:35 GMT
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.
Mattias Sjögren - 04 Jan 2007 20:20 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.