473,537 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6134
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
2851
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 array in a structure, but I think that is to no avail because the size of the string array is variable. Dim strValues() As String = ... Dim...
8
6648
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ######################################################################### #! /usr/bin/env python import win32api, win32ui, win32con
6
4164
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 access the x,y coordinates. Some example code in C# would be greatly appreciated.
2
1313
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 integer Dim TmpStruc As IntPtr = Marshal.AllocHGlobal(StructSize) Marshal.StructureToPtr(Struct, TmpStruc, True) zz=SendMessage(TxtHandle, Msg1,...
5
7189
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 returns false and pCol is empty Where am I going wrong?
0
2116
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 is in a STRUCT?
6
2400
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 C++.NET . This COM component is referenced in my C# application. The COM component statically links with the 6.0 dll. I am facing problems in passing...
5
5222
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 marshaling the struct to be able to use it in sendmessage... Here is an example LM_GETITEM: http://msdn.microsoft.com/en-us/library/bb760720(VS.85).aspx
11
2328
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 negative handle...is anybody know why it return an invalid handle like "-989523277" ?? IntPtr ImageHandle =...
0
7362
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...
0
7534
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. ...
0
7687
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...
1
7277
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...
0
5826
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...
1
5219
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4846
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...
1
1759
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
0
582
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...

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.