A
Adam Clauss
I am attempting to set the text on a richedit control in another application
using EM_SETTEXTEX:
http://msdn.microsoft.com/library/d...olreference/richeditmessages/em_settextex.asp
I have the following:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int
lParam);
[StructLayout(LayoutKind.Sequential)]
public struct SETEXTEX
{
public uint flags;
public uint codepage;
};
private void SetRichEditText(IntPtr hWnd, string text)
{
SETEXTEX setextex = new SETEXTEX();
setextex.codepage = 0;
setextex.flags = 0;
IntPtr ptr =
System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.Interop
Services.Marshal.SizeOf(setextex.GetType()));
System.Runtime.InteropServices.Marshal.StructureToPtr(setextex, ptr,
true);
IntPtr sPtr = System.Runtime.InteropServices.Marshal.StringToBSTR(text);
Win32API.SendMessage(hWnd, Messages.WM_USER+97, ptr.ToInt32(),
sPtr.ToInt32());
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);
System.Runtime.InteropServices.Marshal.FreeBSTR(sPtr);
}
This code, while running without error, gives garbage in the richedit
control. If I change the StringToBSTR to StringToCoTaskMemAnsi or
StringToCoTaskMemAuto, then the other program crashes upon calling
SendMessage. Any ideas on what I'm doing wrong here?
using EM_SETTEXTEX:
http://msdn.microsoft.com/library/d...olreference/richeditmessages/em_settextex.asp
I have the following:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int
lParam);
[StructLayout(LayoutKind.Sequential)]
public struct SETEXTEX
{
public uint flags;
public uint codepage;
};
private void SetRichEditText(IntPtr hWnd, string text)
{
SETEXTEX setextex = new SETEXTEX();
setextex.codepage = 0;
setextex.flags = 0;
IntPtr ptr =
System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.Interop
Services.Marshal.SizeOf(setextex.GetType()));
System.Runtime.InteropServices.Marshal.StructureToPtr(setextex, ptr,
true);
IntPtr sPtr = System.Runtime.InteropServices.Marshal.StringToBSTR(text);
Win32API.SendMessage(hWnd, Messages.WM_USER+97, ptr.ToInt32(),
sPtr.ToInt32());
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);
System.Runtime.InteropServices.Marshal.FreeBSTR(sPtr);
}
This code, while running without error, gives garbage in the richedit
control. If I change the StringToBSTR to StringToCoTaskMemAnsi or
StringToCoTaskMemAuto, then the other program crashes upon calling
SendMessage. Any ideas on what I'm doing wrong here?