Constructing IntPtr from an instance of a class

  • Thread starter Thread starter Valerie Hough
  • Start date Start date
V

Valerie Hough

In C#:
How do I construct an IntPtr object to point to an instance of a class, pass
it somewhere, then reference it once again as the original object.?

My specific implementation is to use PostMessage to pass my class instance
as the "lParam" of the message.

Thanks in advance.
Valerie Hough
 
Valerie,

Are you doing this to call a method in the UI thread of your app, and
pass that as a parameter? If so, then you should really use the Invoke
method on the Control class, as it is much easier, and was written to do
just this.

If you have a different need, then use the GCHandle structure. You can
call the static Alloc method on the GCHandle structure, and then cast the
GCHandle structure to an IntPtr. You can then cast back to the GCAlloc
instance, and get the object referenced by the GCAlloc structure when you
pass your IntPtr around.

Hope this helps.
 
Valerie,
How do I construct an IntPtr object to point to an instance of a class, pass
it somewhere, then reference it once again as the original object.?

Use the GCHandle struct. Once allocated you can cast it to/from
IntPtr.


Mattias
 
Thank you for the response.

I am doing it from an asynchronous socket class. I want to process the
socket message in my UI thread (i.e. not in the thread of my socket's
callback). I am not sure this is the right way to do it, but my thinking was
that I could force processing from the UI thread by putting the message in
its queue. I looked at Control.Invoke, but was not sure whether I could use
it from my class that does all my async socket processing. Please let me
know your thoughts.
Valerie Hough
 
Valerie,

I don't know that you want to process the socket message in your UI
thread. It will just make your UI unresponsive, which is not a good thing.

You should process the message in the callback, and then make calls to
Invoke when needed to update the UI.
 
Back
Top