473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr
wparam, IntPtr lparam);
//...
[StructLayout(LayoutKind.Sequential)]
public struct LITEM
{
uint mask;
int iLink;
uint state;
uint stateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
string szID; //??? is it equivalent to WCHAR ?//
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
string szUrl; //??? is it equivalent to WCHAR ?//
}

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

I know the information in the struct must be set before calling
sendmessage...the 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 5202
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("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr
wparam, IntPtr lparam);
//...
[StructLayout(LayoutKind.Sequential)]
public struct LITEM
{
* * * * uint mask;
* * * * int iLink;
* * * * uint state;
* * * * uint stateMask;
* * * * [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
* * * * string szID; * *//??? is it equivalent to WCHAR *?//
* * * * [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
* * * * string szUrl; * //??? is it equivalent to WCHAR *?//

}

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

I know the information in the struct must be set before calling
sendmessage...the code is just an incomplete example, but how to
create and marshaling a valid struct to call sendmessage in this
case?
Consider using Marshal.StructureToPtr() 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("user32.dll")]
public static extern IntPtr SendMessage(IntPtr 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(LayoutKind.Sequential)]
public struct LITEM
{
uint mask;
int iLink;
uint state;
uint stateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
string szID; //??? is it equivalent to WCHAR ?//
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2084)]
string szUrl; //??? is it equivalent to WCHAR ?//
}
LITEM MyLItem = new LITEM();
MyLItem.mask=(uint)LIF_ITEMID | (uint)LIF_URL; //must use these
masks to get the url
MyLItem.iLink=0;
IntPtr MyLItemPtr = Marshal.AllocHGlobal(Marshal.SizeOf(MyLItem));
Marshal.StructureToPtr(MyLItem, MyLItemPtr, false);
IntPtr MsgResult =
Win32.SendMessage(Handle,LM_GETITEM,IntPtr.Zero,My LItemPtr);
MessageBox.Show(MsgResult.ToString()); //this return 0 :(
Marshal.FreeHGlobal(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.public.dotnet.framework.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 misunderstanding 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=19d4294d-0531-4ec2-8b27-2e463b315f16&displaylang=en)
Jul 14 '08 #6

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

Similar topics

1
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...
5
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...
3
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
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...
0
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...
5
by: Adam Clauss | last post by:
I am attempting to set the text on a richedit control in another application using EM_SETTEXTEX:...
2
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...
0
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.