468,505 Members | 1,592 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

pass structure address to un-managed code

hello

I'm calling Win32 API methods like "SendMessage" in this method there
are the lParam & wParam parameters, sometimes I need to pass a structure
address in the lParam or wParam.

the only way I found that workes is to define SendMessage like that:

[DllImport("User32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
ref RECT r);

this approach is very limited and enforces me to define a new prototype for
every datatype I need to pass.

I'm sure there is a better way. but I did not find it.

Thanks in advance.

Yoramo


Nov 15 '05 #1
7 1778
Your assessment is correct. However, that IS the correct way to handle it.
While C gives you the ability to treat random memory as arbitrary data, that
is strictly against the type-safe nature of C#. You should in fact, overload
your definition of SendMessage to include different argument types. This is
particularly true of the few cases where lParam is passed by Value instead
of by Ref.
If you really don't care about the type safety, you can define the argument
as IntPtr (and not Ref), and use pass the address of pinned variables to it.
To be honest, I don't recommend it, especially since this could potentially
circumvent important marshalling procedures.

-Rob Teixeira [MVP]

"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessage" in this method there
are the lParam & wParam parameters, sometimes I need to pass a structure
address in the lParam or wParam.

the only way I found that workes is to define SendMessage like that:

[DllImport("User32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
ref RECT r);

this approach is very limited and enforces me to define a new prototype for every datatype I need to pass.

I'm sure there is a better way. but I did not find it.

Thanks in advance.

Yoramo

Nov 15 '05 #2
Hello Rob

I have tried using the IntPtr but my code refuses to compile. I'm getting a
message saying: "Can not convert Type ... to IntPtr" and cast does not work.

I have tried to create a win32 class that will enable me to call all the
methods I'm use to in unmanaged code. Creating a prototype for every
combination of SendMessage is an endless job. Do you have a better
suggestion? Does Microsoft supply such a class in C# ?

Thanks in advance

Yoramo

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:u0**************@tk2msftngp13.phx.gbl...
Your assessment is correct. However, that IS the correct way to handle it.
While C gives you the ability to treat random memory as arbitrary data, that is strictly against the type-safe nature of C#. You should in fact, overload your definition of SendMessage to include different argument types. This is particularly true of the few cases where lParam is passed by Value instead
of by Ref.
If you really don't care about the type safety, you can define the argument as IntPtr (and not Ref), and use pass the address of pinned variables to it. To be honest, I don't recommend it, especially since this could potentially circumvent important marshalling procedures.

-Rob Teixeira [MVP]

"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessage" in this method there are the lParam & wParam parameters, sometimes I need to pass a structure
address in the lParam or wParam.

the only way I found that workes is to define SendMessage like that:

[DllImport("User32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT r);

this approach is very limited and enforces me to define a new prototype

for
every datatype I need to pass.

I'm sure there is a better way. but I did not find it.

Thanks in advance.

Yoramo


Nov 15 '05 #3

"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello Rob

I have tried using the IntPtr but my code refuses to compile. I'm getting a message saying: "Can not convert Type ... to IntPtr" and cast does not work.

You can never convert a structure to an IntPtr. Try using the Marshal class
in System.Runtime.Interop to help you out.
Again, realize that by doing this you are potentially creating unstable code
that is prone to (nasty) bugs, particularly when it comes to circumventing
marshalling operations.
I have tried to create a win32 class that will enable me to call all the
methods I'm use to in unmanaged code. Creating a prototype for every
combination of SendMessage is an endless job.
Not endless. The possibilities aren't THAT many :-)
This is still the safest way to go.
Do you have a better
suggestion? Does Microsoft supply such a class in C# ?


Nope. MS wraps what it needed internally.

-Rob Teixeira [MVP]
Nov 15 '05 #4
Thanks for your kind help
Yoramo
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...

"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello Rob

I have tried using the IntPtr but my code refuses to compile. I'm
getting a
message saying: "Can not convert Type ... to IntPtr" and cast does not work.

You can never convert a structure to an IntPtr. Try using the Marshal

class in System.Runtime.Interop to help you out.
Again, realize that by doing this you are potentially creating unstable code that is prone to (nasty) bugs, particularly when it comes to circumventing
marshalling operations.
I have tried to create a win32 class that will enable me to call all the
methods I'm use to in unmanaged code. Creating a prototype for every
combination of SendMessage is an endless job.


Not endless. The possibilities aren't THAT many :-)
This is still the safest way to go.
Do you have a better
suggestion? Does Microsoft supply such a class in C# ?


Nope. MS wraps what it needed internally.

-Rob Teixeira [MVP]

Nov 15 '05 #5
I think something like this might work (and yes, it's naughty and
error-prone)

[DllImport("User32.dll",CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, short
wParam,int lParam);

unsafe
{
fixed ( void * p = &struct ) // Can be any struct
{
int lParam = (int) p ;
SendMessage(hWnd,msg,wParam,lParam) ;
}
}
"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessage" in this method there
are the lParam & wParam parameters, sometimes I need to pass a structure
address in the lParam or wParam.

the only way I found that workes is to define SendMessage like that:

[DllImport("User32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
ref RECT r);

this approach is very limited and enforces me to define a new prototype for every datatype I need to pass.

I'm sure there is a better way. but I did not find it.

Thanks in advance.

Yoramo

Nov 15 '05 #6
Thanks i will try it later.
Yoramo

"Steve Terepin" <st***@terepin.freeserve.co.uk> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
I think something like this might work (and yes, it's naughty and
error-prone)

[DllImport("User32.dll",CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, short
wParam,int lParam);

unsafe
{
fixed ( void * p = &struct ) // Can be any struct
{
int lParam = (int) p ;
SendMessage(hWnd,msg,wParam,lParam) ;
}
}
"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessage" in this method there are the lParam & wParam parameters, sometimes I need to pass a structure
address in the lParam or wParam.

the only way I found that workes is to define SendMessage like that:

[DllImport("User32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT r);

this approach is very limited and enforces me to define a new prototype

for
every datatype I need to pass.

I'm sure there is a better way. but I did not find it.

Thanks in advance.

Yoramo


Nov 15 '05 #7
Don't forget to look through some of the System.Windows.Forms code, they
will have great guidelines to writing this type of code (since they all us
it internally anyways!)
--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessage" in this method there
are the lParam & wParam parameters, sometimes I need to pass a structure
address in the lParam or wParam.

the only way I found that workes is to define SendMessage like that:

[DllImport("User32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
ref RECT r);

this approach is very limited and enforces me to define a new prototype for every datatype I need to pass.

I'm sure there is a better way. but I did not find it.

Thanks in advance.

Yoramo

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by JSheble | last post: by
2 posts views Thread by prakashgkhaire | last post: by
2 posts views Thread by Scott Collier | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.