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 / December 2004

Tip: Looking for answers? Try searching our database.

GCHandle

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Willie - 10 Dec 2004 15:15 GMT
I am using Windows Forms and C# to access a COM object.  The COM object
(“tablet” in the code examples below) requires a window handle.  I am using
the form handle (this.Handle) to pass to the COM object.  I had assumed that
I would have to use a GCHandle to pass to the COM object but I can’t get it
to work.  See below for examples;

This doesn’t work (COM object chokes the moment it uses the hWnd:

GCHandle gch;
gch = GCHandle.Alloc( this.Handle, GCHandleType.Pinned );
int hWnd = (int)gch.AddrOfPinnedObject();
tablet.set_hWnd( ref hWnd );

The following code works just fine:

int hWnd = (int)this.Handle;
tablet.set_hWnd( ref hWnd );

Can anyone shed any light on whether I should be using GCHandle and why it’s
not working for me?

Bill
Mattias Sj?gren - 10 Dec 2004 23:50 GMT
>Can anyone shed any light on whether I should be using GCHandle

Not in this case, no.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

liggett78 - 13 Dec 2004 13:37 GMT
Try this:
GCHandle gch = GCHandle(this.Handle);
tablet.set_hWnd( (IntPtr) gch);

You should free the handle after finished, e.g. gch.Free(). Generally
GCHandle is used to prevent something from being collected by the GC in the
meanwhile (see example code in SDK). I think in your case it's better to use
either direct handle or wrap it in HandleRef() and pass to the COM component.

Regards, Joerg

> I am using Windows Forms and C# to access a COM object.  The COM object
> (“tablet” in the code examples below) requires a window handle.  I am using
[quoted text clipped - 18 lines]
>
> Bill

Rate this thread:







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.