473,803 Members | 3,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pass structure address to un-managed code

hello

I'm calling Win32 API methods like "SendMessag e" 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("User 32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(Int Ptr 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 1980
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******** ******@TK2MSFTN GP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessag e" 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("User 32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(Int Ptr 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@@m sn.com> wrote in message
news:u0******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessag e" 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("User 32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(Int Ptr 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******** ********@TK2MSF TNGP10.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@@m sn.com> wrote in message
news:ut******** ******@TK2MSFTN GP10.phx.gbl...

"Yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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("User 32.dll",CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(Int Ptr hWnd, int msg, short
wParam,int lParam);

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

I'm calling Win32 API methods like "SendMessag e" 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("User 32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(Int Ptr 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******** ******@TK2MSFTN GP11.phx.gbl...
I think something like this might work (and yes, it's naughty and
error-prone)

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

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

I'm calling Win32 API methods like "SendMessag e" 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("User 32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(Int Ptr 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******** ******@TK2MSFTN GP09.phx.gbl...
hello

I'm calling Win32 API methods like "SendMessag e" 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("User 32.dll",CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(Int Ptr 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3899
by: Raymond H. | last post by:
Hello, 1- How to see, in a Label, the URL of a link that the mouse pass over? (in the WebBrower control in a vb projet). 2- How to create a menu and a submenu via a button Command1? and after, hot to delete these two menus? Thank in advance. divers_rh@hotmail.com Raymond H.
3
3647
by: Panda2 | last post by:
Say I have a structure such as: struct Barn{ char type; int number; }animal so for example we might have data like animal.type cow animal.number 10
110
9972
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
13
7301
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free( structure->bufferspace ) ). My question is, if I free just the structure, will the (e.g.) bufferspace be freed implicitly, or do I have to (as I currently am) free the members first? Thanks. -cjl
6
1403
by: JSheble | last post by:
Are there any reasons why I shouldn't or couldn't use a structure as a property in a class? Specifically since structures are value types and objects are reference types? I have a Shipping object that has 4 different types of addresses. Initially, I created 4 instances of an Address object, but seem inefficient to me since the Address itslef doesn't really have (or need) and methods... I was thinking a structure would be more...
2
5256
by: prakashgkhaire | last post by:
i have two structure where first structure consists val and structure pointer(linklist), 2nd structure consists, val, a varialbe of first structure, pointer of structure. Now i found to pass the variable of 2nd structure to function as variable of 1st structure struct first { int val; struct first *next;
2
1850
by: Scott Collier | last post by:
I would like to serialize a custom structure eg. Private Structure Dog Dim DogName As String Dim DogAge As Integer Dim Fleas As ArrayList End Structure I am using a BinaryFormatter() to serialize the data. Each Dog is added to a HashTable, and then the final HashTable is serialized... well that is the
1
2717
by: idlyatall | last post by:
Can we assume member variables reside in a class as they would in a structure? It is legitimate to assume the memory layout of the field of a structure. Can we assume the same for a class? e.g. struct { long x; long y; } s; char* p = (char*)&s.x;
8
1331
by: sathishc58 | last post by:
Hi All I read in book that mentioning the structure name gives the whole structure itself and not its base address? What is meant by that? I wrote a small program like this struct a { int a; int b;
1
1211
by: =?Utf-8?B?S0pI?= | last post by:
I've been running IE 7 on my pc for over a year now. Never had any problems and rarely had any pop-ups. I recently un-installed an old version of FireFox and now lots of problems with IE 7. On start-up, the address bar is missing and I have to close out of the app and re-start. It's there on re-start. I am also getting tons of pop ups now - very bad. I un-intalled SP3 and then un-installed IE 7. Re-installed SP3 and re-installed IE...
0
9700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.