I need to pass an object of a class from one program to another.
This is the class I need to pass;
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Ansi )]
public class MessageContainer
{
public bool DescFirst { get; set; }
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 200 )]
public string Data;
public decimal PriceMin { get; set; }
public int Hidden { get; set; }
public MessageContainer()
{
DescFirst = true;
}
}
CopyDataStruct Class;
public struct CopyDataStruct : IDisposable
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
public void Dispose()
{
if (this.lpData != IntPtr.Zero)
{
LocalFree(this.lpData);
this.lpData = IntPtr.Zero;
}
}
}
How I send object;
{
int hwnd = -1;
IntPtr handle = IntPtr.Zero;
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.StartsWith("MyApp"))
{
handle = proc.MainWindowHandle;
}
}
if (handle != IntPtr.Zero)
{
Win32.CopyDataStruct cds = new Win32.CopyDataStruct();
MessageContainer cs = new MessageContainer();
cs.Data = "Test123";
cds.DescFirst = true;
cds.lpData = Marshal.AllocHGlobal(Marshal.SizeOf(cs));
cs.PriceMin = "1.25";
cs.Hidden = 0;
try
{
Marshal.StructureToPtr(cs, cds.lpData, false);
Win32.SendMessage(handle, Win32.WM_COPYDATA, IntPtr.Zero, ref cds);
}
finally
{
Marshal.FreeHGlobal(cds.lpData);
}
}
else
MessageBox.Show("Not send");
}
On the other application;
protected override void WndProc(ref Message m)
{
switch ( m.Msg )
{
case Win32.WM_COPYDATA:
Win32.CopyDataStruct cds = (Win32.CopyDataStruct)m.GetLParam(typeof(Win32.CopyDataStruct));
// If the size matches
if ( cds.cbData == Marshal.SizeOf(typeof(MessageContainer)) )
{
MessageContainer msg = (MessageContainer)Marshal.PtrToStructure(cds.lpData, typeof(MessageContainer));
if ( msg != null )
{
TextBox1.text = msg.Data;
}
}
break;
default:
// let the base class deal with it
base.WndProc(ref m);
break;
}
}
I get all the data other than "string" data type ("Test123") on target application. Get blank for "Data".
No errors, both applications work. Object get transfered, but on string variables no data.
Please help me on this