P/Invoke

J

Johnny

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.
 
M

Mattias Sjögren

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
 
J

Johnny

I have to "P/Invoke" to a function taking an LPARAM arg which is actually
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

Johnny

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top