473,769 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshaling a string (and struct) for call to SendMessage

I am attempting to set the text on a richedit control in another application
using EM_SETTEXTEX:
http://msdn.microsoft.com/library/de..._settextex.asp

I have the following:

[DllImport("user 32.dll", CharSet=CharSet .Auto)]
public static extern int SendMessage(Int Ptr hWnd, uint Msg, int wParam, int
lParam);

[StructLayout(La youtKind.Sequen tial)]
public struct SETEXTEX
{
public uint flags;
public uint codepage;
};
private void SetRichEditText (IntPtr hWnd, string text)
{
SETEXTEX setextex = new SETEXTEX();
setextex.codepa ge = 0;
setextex.flags = 0;
IntPtr ptr =
System.Runtime. InteropServices .Marshal.AllocC oTaskMem(System .Runtime.Intero p
Services.Marsha l.SizeOf(setext ex.GetType()));
System.Runtime. InteropServices .Marshal.Struct ureToPtr(setext ex, ptr,
true);
IntPtr sPtr = System.Runtime. InteropServices .Marshal.String ToBSTR(text);
Win32API.SendMe ssage(hWnd, Messages.WM_USE R+97, ptr.ToInt32(),
sPtr.ToInt32()) ;
System.Runtime. InteropServices .Marshal.FreeCo TaskMem(ptr);
System.Runtime. InteropServices .Marshal.FreeBS TR(sPtr);
}

This code, while running without error, gives garbage in the richedit
control. If I change the StringToBSTR to StringToCoTaskM emAnsi or
StringToCoTaskM emAuto, then the other program crashes upon calling
SendMessage. Any ideas on what I'm doing wrong here?

--
Adam Clauss
ca*****@tamu.ed u
Nov 16 '05 #1
5 5860
Adam,

You will have to find another way to marshal the string across the
process boundary. When you allocate memory in the local process, the
address that is returned is valid only in the local process. When you
transmit that value across the process boundary (which is what SendMessage
does, it only sends the pointer values, not the values the pointers point
to), the pointer is invalidated, because it is now in a different virtual
address space.

If you want to share some values, you will have to use another
mechanism, such as remoting, sockets, pipes, shared files, etc, etc.
Basically, you need some way of actually sending the bits that you want, and
not pointers to them.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:eM******** ******@TK2MSFTN GP10.phx.gbl...
I am attempting to set the text on a richedit control in another application using EM_SETTEXTEX:
http://msdn.microsoft.com/library/de..._settextex.asp
I have the following:

[DllImport("user 32.dll", CharSet=CharSet .Auto)]
public static extern int SendMessage(Int Ptr hWnd, uint Msg, int wParam, int lParam);

[StructLayout(La youtKind.Sequen tial)]
public struct SETEXTEX
{
public uint flags;
public uint codepage;
};
private void SetRichEditText (IntPtr hWnd, string text)
{
SETEXTEX setextex = new SETEXTEX();
setextex.codepa ge = 0;
setextex.flags = 0;
IntPtr ptr =
System.Runtime. InteropServices .Marshal.AllocC oTaskMem(System .Runtime.Intero p Services.Marsha l.SizeOf(setext ex.GetType()));
System.Runtime. InteropServices .Marshal.Struct ureToPtr(setext ex, ptr,
true);
IntPtr sPtr = System.Runtime. InteropServices .Marshal.String ToBSTR(text); Win32API.SendMe ssage(hWnd, Messages.WM_USE R+97, ptr.ToInt32(),
sPtr.ToInt32()) ;
System.Runtime. InteropServices .Marshal.FreeCo TaskMem(ptr);
System.Runtime. InteropServices .Marshal.FreeBS TR(sPtr);
}

This code, while running without error, gives garbage in the richedit
control. If I change the StringToBSTR to StringToCoTaskM emAnsi or
StringToCoTaskM emAuto, then the other program crashes upon calling
SendMessage. Any ideas on what I'm doing wrong here?

--
Adam Clauss
ca*****@tamu.ed u

Nov 16 '05 #2
Thats... not good. Is there no way to do this through SendMessage? I do not have control over the other app (its actually an
Windows Installer - I am trying to fill some info in on one of the pages). Since I obviously can't modify the installer itself, I
don't think I can use any of the other methods you mentioned can I?

--
Adam Clauss
ca*****@tamu.ed u
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:OI******** ******@TK2MSFTN GP12.phx.gbl...
Adam,

You will have to find another way to marshal the string across the
process boundary. When you allocate memory in the local process, the
address that is returned is valid only in the local process. When you
transmit that value across the process boundary (which is what SendMessage
does, it only sends the pointer values, not the values the pointers point
to), the pointer is invalidated, because it is now in a different virtual
address space.

If you want to share some values, you will have to use another
mechanism, such as remoting, sockets, pipes, shared files, etc, etc.
Basically, you need some way of actually sending the bits that you want, and
not pointers to them.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:eM******** ******@TK2MSFTN GP10.phx.gbl...
I am attempting to set the text on a richedit control in another

application
using EM_SETTEXTEX:

http://msdn.microsoft.com/library/de..._settextex.asp

I have the following:

[DllImport("user 32.dll", CharSet=CharSet .Auto)]
public static extern int SendMessage(Int Ptr hWnd, uint Msg, int wParam,

int
lParam);

[StructLayout(La youtKind.Sequen tial)]
public struct SETEXTEX
{
public uint flags;
public uint codepage;
};
private void SetRichEditText (IntPtr hWnd, string text)
{
SETEXTEX setextex = new SETEXTEX();
setextex.codepa ge = 0;
setextex.flags = 0;
IntPtr ptr =

System.Runtime. InteropServices .Marshal.AllocC oTaskMem(System .Runtime.Intero p
Services.Marsha l.SizeOf(setext ex.GetType()));
System.Runtime. InteropServices .Marshal.Struct ureToPtr(setext ex, ptr,
true);
IntPtr sPtr =

System.Runtime. InteropServices .Marshal.String ToBSTR(text);
Win32API.SendMe ssage(hWnd, Messages.WM_USE R+97, ptr.ToInt32(),
sPtr.ToInt32()) ;
System.Runtime. InteropServices .Marshal.FreeCo TaskMem(ptr);
System.Runtime. InteropServices .Marshal.FreeBS TR(sPtr);
}

This code, while running without error, gives garbage in the richedit
control. If I change the StringToBSTR to StringToCoTaskM emAnsi or
StringToCoTaskM emAuto, then the other program crashes upon calling
SendMessage. Any ideas on what I'm doing wrong here?

--
Adam Clauss
ca*****@tamu.ed u


Nov 16 '05 #3
Hi Adam,

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:eQ******** ******@TK2MSFTN GP10.phx.gbl...
Thats... not good. Is there no way to do this through SendMessage? I do not have control over the other app (its actually an Windows Installer - I am trying to fill some info in on one of the pages). Since I obviously can't modify the installer itself, I don't think I can use any of the other methods you mentioned can I?


Just an FYI, if the installation package is an ".msi" file then you
*could* change it. Whether it is practical to do so is another matter, of
course. Windows Installer installation packages are actually stand-alone
databases (rather like Access .mdb files) with a specific set of tables.
There is a COM API for modifying the contents of the tables (Microsoft
Windows Installer Object Library: %windir%\system 32\msi.dll).

Regards,
Daniel
Nov 16 '05 #4
Interesting, I'll have to look into that. It could greatly speed up some of what I am doing.

In the meantime, the workaround I just got working (not pretty, nor efficient, but it works) is to just repeatedly send WM_CHAR
messages to the richedit. Seems to have done the job. Fortunately, the strings I am sending are not too long.

--
Adam Clauss
ca*****@tamu.ed u
"Daniel Pratt" <ko************ ******@hotmail. com> wrote in message news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi Adam,

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:eQ******** ******@TK2MSFTN GP10.phx.gbl...
Thats... not good. Is there no way to do this through SendMessage? I do

not have control over the other app (its actually an
Windows Installer - I am trying to fill some info in on one of the pages).

Since I obviously can't modify the installer itself, I
don't think I can use any of the other methods you mentioned can I?


Just an FYI, if the installation package is an ".msi" file then you
*could* change it. Whether it is practical to do so is another matter, of
course. Windows Installer installation packages are actually stand-alone
databases (rather like Access .mdb files) with a specific set of tables.
There is a COM API for modifying the contents of the tables (Microsoft
Windows Installer Object Library: %windir%\system 32\msi.dll).

Regards,
Daniel

Nov 16 '05 #5
"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:OF******** ******@TK2MSFTN GP11.phx.gbl...
Interesting, I'll have to look into that. It could greatly speed up some of what I am doing.
In the meantime, the workaround I just got working (not pretty, nor efficient, but it works) is to just repeatedly send WM_CHAR messages to the richedit. Seems to have done the job. Fortunately, the strings I am sending are not too long.

Hi Adam,

Have a look at the WM_SETTEXT and (EM_SETTEXTEX for RTF) messages. And if
you want to replace part of the existing text, check the EM_SETSEL and
EM_REPLACESEL.

Another way of sending a string to this richedit would be to copy it to the
Clipboard and then send the WM_PASTE message to the window. But I do not
recommend this way, because your string may get corrupted by other apps or
user activities.

Regards,

Stoil

--
Adam Clauss
ca*****@tamu.ed u
"Daniel Pratt" <ko************ ******@hotmail. com> wrote in message

news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi Adam,

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:eQ******** ******@TK2MSFTN GP10.phx.gbl...
Thats... not good. Is there no way to do this through SendMessage? I
do not have control over the other app (its actually an
Windows Installer - I am trying to fill some info in on one of the
pages). Since I obviously can't modify the installer itself, I
don't think I can use any of the other methods you mentioned can I?


Just an FYI, if the installation package is an ".msi" file then you
*could* change it. Whether it is practical to do so is another matter, of course. Windows Installer installation packages are actually stand-alone
databases (rather like Access .mdb files) with a specific set of tables.
There is a COM API for modifying the contents of the tables (Microsoft
Windows Installer Object Library: %windir%\system 32\msi.dll).

Regards,
Daniel



Nov 16 '05 #6

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

Similar topics

3
4738
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got an error message saying "Cannot marshal parameter #2: Invalid managed/unmanaged type combination (this value type must be paired with Struct)" when calling "AceGetPinParams(iHandle,ref sd_pin)" function,here's my test code: private static...
5
96496
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...
2
1874
by: Pas de Spam | last post by:
Hi, I have some problem in marshaling array of structure. I have to exchange a structure of data with a C++ program. This structure data contains a field which is an array of a custom type. I can't find the correct sequence to declare the marshal attributes of my structures, I always got an exception at runtime when marshaling my data. Here an example of code, which gives me the exception "Type TypeData can not be marshaled as an...
2
8537
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
1383
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 -
23
2602
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
6
5900
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example will prove useful. structure MyStruct { // this is a complicated struct declaration in the sense
5
5236
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
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9987
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
9857
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
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
6662
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
5294
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...
1
3952
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
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
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.