473,230 Members | 4,210 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,230 software developers and data experts.

Interop SendMessage : How to pass Structure to be filled bySendMessage

Hi,

I already post a similar question last week without success.

Ok I want to get the current text selection in a RICHEDIT control..
This can be easily done in C++ with EM_EXGETSEL message. I really
need to do the same thing in C#.

How can I put the structure in memory to be able to call SendMessage
and get the expected results in the structure.

Im playing with the following code but the target application always
crash.
For using the following example you need to start WordPad.exe (not
notepad), manually get the handle of the RichEdit control with Spy++
and replace the handle parameter in the sendmessage

initial condition : Type and select some text in WordPad

The following code must return Starting and ending position of
selected text in WordPad.

using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Diagnostics;

//..
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr
wparam, IntPtr lparam);
private const int WM_USER = 1024;
private const int EM_EXGETSEL = WM_USER + 52;

[StructLayout(LayoutKind.Sequential)]
public struct CharRange
{
public int From;
public int To;
public CharRange(int from, int to)
{
this.From = from;
this.To = to;
}
}
//..
CharRange ChrRange= new CharRange();
IntPtr ChrRangePtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ChrRange));
Marshal.StructureToPtr(ChrRange,ChrRangePtr, false);
SendMessage(Handle,EM_EXGETSEL,IntPtr.Zero,ChrRang ePtr);
Marshal.PtrToStructure(ChrRangePtr,ChrRange);
MessageBox.Show("From="+ChrRange.From.ToString()+" ,
to="+ChrRange.To.ToString());

Is anybody know how to correctly code the structure pointer marshaling
part??? How to make this simple example working?

Please help me if you can.. :(
Jul 24 '08 #1
3 6098
On Jul 25, 2:30*am, michelqa <miche...@yahoo.cawrote:
Hi,

I already post a similar question last week without success.

Ok I want to get the current text selection in a RICHEDIT control..
This can be easily done in C++ with EM_EXGETSEL message. *I really
need to do the same thing in C#.

How can I put the structure in memory to be able to call SendMessage
and get the expected results in the structure.

Im playing with the following code but the target application always
crash.
For using the following example you need to start WordPad.exe (not
notepad), manually get the handle of the RichEdit control with Spy++
and replace the handle parameter in the sendmessage

initial condition : Type and select some text in WordPad

The following code must return Starting and ending position of
selected text in WordPad.

using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Diagnostics;

//..
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr
wparam, IntPtr lparam);

private const int WM_USER = 1024;
private const int EM_EXGETSEL * * * * * * *= WM_USER + *52;

[StructLayout(LayoutKind.Sequential)]
public struct CharRange
{
* * * * public int From;
* * * * public int To;
* * * * * * * * public CharRange(int from, int to)
* * * * {
* * * * * * * * this.From = from;
* * * * * * * * this.To = to;
* * * * }}

//..
CharRange ChrRange= new CharRange();
IntPtr ChrRangePtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ChrRange));
Marshal.StructureToPtr(ChrRange,ChrRangePtr, false);
SendMessage(Handle,EM_EXGETSEL,IntPtr.Zero,ChrRang ePtr);
Marshal.PtrToStructure(ChrRangePtr,ChrRange);
MessageBox.Show("From="+ChrRange.From.ToString()+" ,
to="+ChrRange.To.ToString());

Is anybody know how to correctly code the structure pointer marshaling
part??? *How to make this simple example working?
At the first glance your declarations are okay. The problem seems to
be this:

Marshal.PtrToStructure(ChrRangePtr,ChrRange);

It doesn't work like that. The signature of this method is:

public static void PtrToStructure(
IntPtr ptr,
Object structure
);

It is actually quite misleading, but the second argument named
"structure" actually is of type object - so when you pass a struct
(i.e. a value type) to it, it gets boxed first, and the method
operates on that boxed copy. In fact, this method tries to detect
this, as described in MSDN:

"Exceptions: ArgumentException when structure layout is not
sequential or explicit or structure is a boxed value type."

What you need to do is to use the second overload, like this:

ChrRange = (CharRange)PtrToStructure(ChrRangePtr,
typeof(CharRanger));

See if that helps.

Also, don't forget to free the memory block you've allocated - GC
won't do it for you.
Jul 25 '08 #2
CharRange ChrRange= new CharRange();
IntPtr ChrRangePtr =
Marshal.AllocCoTaskMem(Marshal.SizeOf(ChrRange));
Marshal.StructureToPtr(ChrRange,ChrRangePtr, false);
Win32.SendMessage(Handle,
(int)Win32.WindowsMessages.EM_EXGETSEL,IntPtr.Zero ,ChrRangePtr);
ChrRange =
(CharRange)Marshal.PtrToStructure(ChrRangePtr,type of(CharRange));
MessageBox.Show("From="+ChrRange.From.ToString()+" ,
to="+ChrRange.To.ToString());
Marshal.FreeCoTaskMem(ChrRangePtr);

the target application is still crashing when executing SendMessage :
The memory could not be 'written'

What is wrong with my ChrRangePtr???

Note: In my test the target application is WordPad.exe and Handle is
the RichEdit Handle

Jul 25 '08 #3
I Found the answer here : http://www.codeproject.com/KB/thread..._memsteal.aspx

Aug 11 '08 #4

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

Similar topics

1
by: Rob Mayo | last post by:
How can I create a pointer to a string array from VB.Net? I want to take a string array of variable size and pass it as a wParam on a message to a custom subclassed window. I've tried wrapping the...
8
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ...
6
by: Steve | last post by:
I am trying to use the SendMessage API function using EM_POSFROMCHAR to get the x,y location of the current character. I have declared the function and placed the call into my code. How do I...
2
by: SamSpade | last post by:
I'd like to define a wrapped SendMessage. Something like: function zz (TxtHandle as IntPtr, Msg1 as integer, Msg2 as Integer, Struct as Structure, StructSize as integer, StructType as ???) as...
5
by: Scott Gunn | last post by:
Hi, I'm trying to find out what order the columns in a listview have been reordered into. I found some examples on the Internet that use csharp and I tried to convert them into vb but it always...
0
by: kagorami | last post by:
Q: How do you return a LPSTR from unmanaged code? A: Use a StringBuilder. What about this: public struct CSTRUCT { string return_value; } How do you get a LPSTR from unmanaged code when it...
6
by: Nataraj1978 | last post by:
I have a requirement to use a dll written in Visual C++ 6.0 in my C# application. In order to establish the link between my application and the dll, I have written a ATL COM Component in Visual...
5
by: michelqa | last post by:
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...
11
by: michelqa | last post by:
When executing some win32 messages in c# I get unexpected results. The following example is suppose to return the handle of an image in a button control of another application but it return a...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.