473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 2267
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Benjamin Scott | last post by:
Hello. I attempted to build a compound dictionary: len(Lst)=1000 len(nuerLst)=250 len(nuestLst)=500 Dict={}
2
by: Nuno Esculcas | last post by:
Hello, I come from C++ and i now have to work with C#, and someone tell me that bye bye pointers but i think this is not true, i must convert a DIB image in something that i can use in C# (like...
13
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
5
by: Rik Hemsley | last post by:
How does one go about getting a pointer to method as an IntPtr? Example: public class A { public void X() { Y(Z); }
2
by: swartzbill2000 | last post by:
I want to PostMessage from a class instance to its containing form. I want the WPARAM to point to the class instance. I am using the declaration of PostMessage from www.pinvoke.net. WPARAM is an...
6
by: Big George | last post by:
Hello, I develop in ASP.NET with VB.NET code. I need some help constructing a class: Worker. I'm designing the properties of this class. The class is filled reading tables in database. ...
6
by: K Viltersten | last post by:
I have a class that reads an XML and according to its contents, it creates a List<Letter>, where Letter is an abstract class that is implemented in Alpha and Beta classes. I use a foreach...
0
by: eck3ko | last post by:
Hi everybody! i have a program in c# that use a method inside a c# dll that acts as a proxy to a c++ dll xD this works perfect in a x86 pc but when using in a x64 pc this thing just crash with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.