473,382 Members | 1,373 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,382 software developers and data experts.

P/Invoke

Hello,

I have to "P/Invoke" to a function taking an LPARAM arg which is actually an
"IntPtr". In this case "IntPtr" needs to be a pointer to an "unsigned int".
Can someone enlighten me on how to pass this. Thanks.
Jan 8 '08 #1
4 1405
>I have to "P/Invoke" to a function taking an LPARAM arg which is actually an
>"IntPtr". In this case "IntPtr" needs to be a pointer to an "unsigned int".
Can someone enlighten me on how to pass this. Thanks.
The easiest way to define the parameter type as ref uint (as long as
it should point to just a single uint and not an array).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 8 '08 #2
I have to "P/Invoke" to a function taking an LPARAM arg which is actually
an
"IntPtr". In this case "IntPtr" needs to be a pointer to an "unsigned
int".
Can someone enlighten me on how to pass this. Thanks.

The easiest way to define the parameter type as ref uint (as long as
it should point to just a single uint and not an array).
Thanks for the info. I was hoping to avoid that however since it's
"SendMessage()" I'm calling. I don't want to have to overload it everytime I
need it. I'd also like to know how to do this anyway, just for my own
knowledge (my roots are C/C++). Do you know if it can be done? Thanks again.
Jan 8 '08 #3
If you don't want to overload it, then you will have to allocate the
space in memory (which you will need to look at the static Size property on
to get the size of the IntPtr on the platform you are running on) and then
pass the pointer to that memory to SendMessage.

You can do this with the CoTaskMemAlloc and CoTaskMemFree methods on the
Marshal class, and then use the Write* methods on the Marshal class as well
to write the contents to memory (the actual value).

If you want to use unsafe code, you can always just pass the pointer to
the location of the variable on the stack as well.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Johnny" <no_spam@_nospam.comwrote in message
news:eN**************@TK2MSFTNGP05.phx.gbl...
>I have to "P/Invoke" to a function taking an LPARAM arg which is
actually an
"IntPtr". In this case "IntPtr" needs to be a pointer to an "unsigned
int".
Can someone enlighten me on how to pass this. Thanks.

The easiest way to define the parameter type as ref uint (as long as
it should point to just a single uint and not an array).

Thanks for the info. I was hoping to avoid that however since it's
"SendMessage()" I'm calling. I don't want to have to overload it everytime
I need it. I'd also like to know how to do this anyway, just for my own
knowledge (my roots are C/C++). Do you know if it can be done? Thanks
again.

Jan 8 '08 #4
If you don't want to overload it, then you will have to allocate the
space in memory (which you will need to look at the static Size property
on to get the size of the IntPtr on the platform you are running on) and
then pass the pointer to that memory to SendMessage.

You can do this with the CoTaskMemAlloc and CoTaskMemFree methods on
the Marshal class, and then use the Write* methods on the Marshal class as
well to write the contents to memory (the actual value).

If you want to use unsafe code, you can always just pass the pointer to
the location of the variable on the stack as well.
Ok, thanks. I'll run with this for now but the overload is obviously looking
better. I was hoping that the pointer could simply be stored in an "IntPtr"
as an integer. How to get hold of that pointer without explictly allocating
memory or relying on unsafe code is another matter. I tried playing around
with boxing (casting to an "object") but apparently it's not doable. Maybe
it's possible to write a template that relies on the memory allocation
technique (I'd rather avoid unsafe code) and then it will work with any type
(or perhaps "SendMessage()" can even be written as a template). I'll play
around with it anyway. Thanks again.
Jan 8 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: Tom | last post by:
Hi Everybod I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
14
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
23
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.