469,946 Members | 1,936 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

Constructing IntPtr from an instance of a class

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
Nov 17 '05 #1
4 2144
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Valerie Hough" <sa***@pcTrans.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 17 '05 #2
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3
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
Nov 17 '05 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Valerie Hough" <sa***@pcTrans.com> wrote in message
news:e8****************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Benjamin Scott | last post: by
2 posts views Thread by Nuno Esculcas | last post: by
13 posts views Thread by Christian Westerlund | last post: by
2 posts views Thread by Alex Sedow | last post: by
5 posts views Thread by Rik Hemsley | last post: by
2 posts views Thread by swartzbill2000 | last post: by
6 posts views Thread by Big George | last post: by
reply views Thread by eck3ko | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.