pass structure address to un-managed code

  • Thread starter Thread starter Yoramo
  • Start date Start date
Y

Yoramo

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
 
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]
 
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 said:
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 said:
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
 
Yoramo said:
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]
 
Thanks for your kind help
Yoramo


Rob Teixeira said:
getting
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.


Not endless. The possibilities aren't THAT many :-)
This is still the safest way to go.


Nope. MS wraps what it needed internally.

-Rob Teixeira [MVP]
 
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) ;
}
}
 
Thanks i will try it later.
Yoramo

Steve Terepin said:
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 said:
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
 
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!)
 
Back
Top