473,756 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshaling Struct for native SendMessage

Hi,

I need to call a lot of different native SendMessage to retreive
informations from non managed application.

Some win32 messages use struct pointer for lparam....how to create and
marshaling the struct to be able to use it in sendmessage...

Here is an example LM_GETITEM:
http://msdn.microsoft.com/en-us/libr...20(VS.85).aspx
---------------------------------------------------------

[DllImport("user 32.dll")]
public static extern IntPtr SendMessage(Int Ptr hwnd, int msg, IntPtr
wparam, IntPtr lparam);
//...
[StructLayout(La youtKind.Sequen tial)]
public struct LITEM
{
uint mask;
int iLink;
uint state;
uint stateMask;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
string szID; //??? is it equivalent to WCHAR ?//
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
string szUrl; //??? is it equivalent to WCHAR ?//
}

LITEM MyLItem = new LITEM();
int MsgResult = SendMessage(Han dle,LM_GETITEM, IntPtr.Zero,ref
MyLItem); //<----- ???????

I know the information in the struct must be set before calling
sendmessage...t he code is just an incomplete example, but how to
create and marshaling a valid struct to call sendmessage in this
case? Is it possible when the Handle is in a different process??

Thanks for any help and example...
Jul 13 '08 #1
5 5235
On Jul 13, 1:10*pm, michelqa <miche...@yahoo .cawrote:
Hi,

* *I need to call a lot of different native SendMessage to retreive
informations from non managed application.

Some win32 messages use struct pointer for lparam....how to create and
marshaling the struct to be able to use it in sendmessage...

Here is an example LM_GETITEM:http://msdn.microsoft.com/en-us/libr...20(VS.85).aspx
---------------------------------------------------------

[DllImport("user 32.dll")]
public static extern IntPtr SendMessage(Int Ptr hwnd, int msg, IntPtr
wparam, IntPtr lparam);
//...
[StructLayout(La youtKind.Sequen tial)]
public struct LITEM
{
* * * * uint mask;
* * * * int iLink;
* * * * uint state;
* * * * uint stateMask;
* * * * [MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
* * * * string szID; * *//??? is it equivalent to WCHAR *?//
* * * * [MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
* * * * string szUrl; * //??? is it equivalent to WCHAR *?//

}

LITEM MyLItem = new LITEM();
int MsgResult = SendMessage(Han dle,LM_GETITEM, IntPtr.Zero,ref
MyLItem); * //<----- ???????

I know the information in the struct must be set before calling
sendmessage...t he code is just an incomplete example, but how to
create and marshaling a valid struct to call sendmessage in this
case?
Consider using Marshal.Structu reToPtr() method. You'll need to
allocate a memory block of proper size first, then call StructureToPtr
to marshal your structure into that block in the appropriate format,
and then you can pass the address of that block as IntPtr to
SendMessage.
Is it possible when the Handle is in a different process??
It is, though it has no relevance to P/Invoke marshalling - it's the
SendMessage function itself that checks whether a handle belongs to a
different process, and does any further necessary marshalling to
deliver it properly. However, this SendMessage feature only works for
Win32 predefined message types (with codes less than WM_USER), not for
any custom messages.
Jul 13 '08 #2
Thanks for your help... I really need help on this :(

Ok now my code look like this... but sendMessage return 0
- Is anybody can confirm that the code look correct (for passing a
structure pointer to sendmessage??)
- After calling sendmessage how can I access to the structure
information returned by MyLItemPtr???
- Since LM_GETITEM is WM_USER +771.... I suspect I need other
marshaling to get the structure back with the information??

//----------- BEGIN CODE -------------//
[DllImport("user 32.dll")]
public static extern IntPtr SendMessage(Int Ptr hwnd, int msg, IntPtr
wparam, IntPtr lparam);
//...
int WM_USER =1024,
int LM_GETITEM = (WM_USER + 0x303)
int LIF_ITEMINDEX = 1;
int LIF_URL = 8;

//..
[StructLayout(La youtKind.Sequen tial)]
public struct LITEM
{
uint mask;
int iLink;
uint state;
uint stateMask;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 48)]
string szID; //??? is it equivalent to WCHAR ?//
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 2084)]
string szUrl; //??? is it equivalent to WCHAR ?//
}
LITEM MyLItem = new LITEM();
MyLItem.mask=(u int)LIF_ITEMID | (uint)LIF_URL; //must use these
masks to get the url
MyLItem.iLink=0 ;
IntPtr MyLItemPtr = Marshal.AllocHG lobal(Marshal.S izeOf(MyLItem)) ;
Marshal.Structu reToPtr(MyLItem , MyLItemPtr, false);
IntPtr MsgResult =
Win32.SendMessa ge(Handle,LM_GE TITEM,IntPtr.Ze ro,MyLItemPtr);
MessageBox.Show (MsgResult.ToSt ring()); //this return 0 :(
Marshal.FreeHGl obal(MyLItemPtr );
//----------- END CODE -------------//

Thanks again
Jul 13 '08 #3
Still playing with the code all day without any success :(

I desperately need help
Jul 14 '08 #4
On Sun, 13 Jul 2008 22:32:31 -0700, michelqa <mi******@yahoo .cawrote:
Still playing with the code all day without any success :(

I desperately need help
For what it's worth, there is a newsgroup specifically dedicated to
interop questions like yours:
microsoft.publi c.dotnet.framew ork.interop

You may have better luck getting your question answered there, especially
if you're in a rush.

Pete
Jul 14 '08 #5
Thanks for the suggestion.. I post my problem in this newsgroup also.

For now I will maybe try to recreate this directly in a win32 project
to make sure the problem is not a misunderstandin g of the LM_GETITEM
API....but by looking at the LM_GETITEM in controlSpy my parameters
seems to be ok. (http://www.microsoft.com/downloads/details.aspx?
familyid=19d429 4d-0531-4ec2-8b27-2e463b315f16&di splaylang=en)
Jul 14 '08 #6

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

Similar topics

1
2274
by: BW Glitch | last post by:
Hi! I'm trying to send a message from a Python script to a Scite window via win32gui.SendMessage() I'm trying to pack the commands using the struct module. However, I can't figure out why Scite isn't responding as I expect. The SendMessage is returning 0. I've searched Google and haven't found anything that would help me. There was a discussion about this same thing about 2 years ago and the poster solved the problem using calldll. I...
5
96494
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain what it is. Is converting a C-style struct to a C# class (or struct) marshalling? And what about the function exports? Are those Marshalling too? Thanks for your help and thanks for helping me with the Dll exports (the several messages I posted in...
3
3641
by: Rudy Velthuis | last post by:
Hello, Does anyone know how to create a struct that will marshal to the following C++ struct A, containing an array of the user defined String10 type: struct String10 { char SLen; char S;
1
2479
by: Nadav | last post by:
Hi I am about to write a performance crutial system, I am considering writing this system based on native COM or unmanaged C++ exposed as CLI, Now, I Wonder... does exposing a native code through CLI ( using a mixed mode DLL ) have any performance penalty? consider one Native C++ CLI assembly calling a methos od another CLI Native C++ Assembly, will there be any marshaling penalty, will there be any penelty at-all? is it possible to pass a...
0
1472
by: Jeff | last post by:
Hi guys Mattias, thanx for answering my last question Well, I'm struggling with marshaling a struct that has **ptr to an array of arrays of struct. Why? I'm stuck with it 1. I need to know how to get to the contents of "dumberstruct" contained within "dumbstruct" (i.e. dumbstruct.dstruct) 2. How to get to dumbstruct.tag_field_value and dumbstruct.field_value Code follows
5
5860
by: Adam Clauss | last post by:
I am attempting to set the text on a richedit control in another application using EM_SETTEXTEX: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_settextex.asp I have the following: public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
2
8536
by: Ryan Ross | last post by:
Hello, I need some help with the SendMessage method. I've imported it into C# with the following statement: public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, long wparam, int lparam);
0
2104
by: weixian_shen | last post by:
I'm trying to call my DLL written in C, and got the error: Cannot marshal field 'b' of type 'mystruct': There is no marshaling support for this type. The 2 functions in the DLL are: void unpack(mystruct* str, void* in_buf); void pack(mystruct* str, void* out_buf);
8
1382
by: Just Me | last post by:
I have SendMessage declared with the last two parameters as ByVal IntPtr I need to call it with an Integer value and a Byte array pointer. The Integer is while the Byte array is I could probably figure how to do the pointer using Marshal.AllocHGlobal , Marshal.StructureToPtr, ... If that's the simplest way to do it. But is it? Also, if the integer value was zero I'd use IntPtr.Zero, but it's not zero -
0
9431
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
10014
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
9844
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
9819
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
8688
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
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
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
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.