E
Erik
I am having trouble getting this to work so hopefully someone can
provide some insite.
I have a structure that I store in a CopyDataStruct which is then send
to another application via SendMessage.
The message is arriving in the other application but when I retrieve
my structure from the CopyDataStruct, the CopyDataStruct is correct,
but the fields for the structure contain in CopyDataStruct.data member
are garbage. The do show the correct field sizes but they do not
contain the strings that I added.
The application that I'm sending to is compiled using MBCS.
Thanks in advance!
Erik
Here is my DLLImport statement:
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr
wParam, IntPtr lParam);
This is the CopyDataStruct structure:
[StructLayout(LayoutKind.Sequential)]
public struct CopyDataStruct
{
public int dwData;
public int cbData;
public IntPtr data;
}
This is the structure that is stored in the CopyDataStruct.data
member:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi]
public struct MyInfo
{
[MarshalAs(UnmanagedType.LPStr, SizeConst=100)]
public string mystring1;
[MarshalAs(UnmanagedType.LPStr, SizeConst=30)]
public string mystring2;
[MarshalAs(UnmanagedType.LPStr, SizeConst=30)]
public string mystring3;
[MarshalAs(UnmanagedType.LPStr, SizeConst=64)]
public string mystring4;
public int somenumber;
}
This is the orginal C++ struct for MyInfo:
typedef struct {
TCHAR mystring1[100];
TCHAR mystring2[30];
TCHAR mystring3[30];
TCHAR mystring4[64];
DWORD somenumber;
} MYINFO_STRUCT;
Here is how I'm creating the structures and sending the message:
Win32.MyInfo li;
li.mystring1 = "string1";
li.mystring2 = "string2";
li.mystring3 = "string3";
li.mystring4 = "string4";
li.somenumber = 34;
// Allocate memory for the MYINFO struct and
// copy the contents of our struct to this memory
IntPtr liMemory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.LogonInfo)));
Marshal.StructureToPtr(li, liMemory, false);
Win32.CopyDataStruct cds;
cds.dwData = 123;
cds.cbData = Marshal.SizeOf(typeof(Win32.MyInfo));
cds.data = liMemory;
// Allocate memory for the COPYDATA struct and
// copy the contents of our struct to this memory
IntPtr cdsMemory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.CopyDataStruct)));
Marshal.StructureToPtr(cds, cdsMemory, false);
// Send the actual Win32 message
Win32.SendMessage(apptosendto.MainWindowHandle, Win32.WM_COPYDATA,
IntPtr.Zero, cdsMemory);
// Free allocated memory
Marshal.FreeHGlobal(liMemory);
Marshal.FreeHGlobal(cdsMemory);
provide some insite.
I have a structure that I store in a CopyDataStruct which is then send
to another application via SendMessage.
The message is arriving in the other application but when I retrieve
my structure from the CopyDataStruct, the CopyDataStruct is correct,
but the fields for the structure contain in CopyDataStruct.data member
are garbage. The do show the correct field sizes but they do not
contain the strings that I added.
The application that I'm sending to is compiled using MBCS.
Thanks in advance!
Erik
Here is my DLLImport statement:
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr
wParam, IntPtr lParam);
This is the CopyDataStruct structure:
[StructLayout(LayoutKind.Sequential)]
public struct CopyDataStruct
{
public int dwData;
public int cbData;
public IntPtr data;
}
This is the structure that is stored in the CopyDataStruct.data
member:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi]
public struct MyInfo
{
[MarshalAs(UnmanagedType.LPStr, SizeConst=100)]
public string mystring1;
[MarshalAs(UnmanagedType.LPStr, SizeConst=30)]
public string mystring2;
[MarshalAs(UnmanagedType.LPStr, SizeConst=30)]
public string mystring3;
[MarshalAs(UnmanagedType.LPStr, SizeConst=64)]
public string mystring4;
public int somenumber;
}
This is the orginal C++ struct for MyInfo:
typedef struct {
TCHAR mystring1[100];
TCHAR mystring2[30];
TCHAR mystring3[30];
TCHAR mystring4[64];
DWORD somenumber;
} MYINFO_STRUCT;
Here is how I'm creating the structures and sending the message:
Win32.MyInfo li;
li.mystring1 = "string1";
li.mystring2 = "string2";
li.mystring3 = "string3";
li.mystring4 = "string4";
li.somenumber = 34;
// Allocate memory for the MYINFO struct and
// copy the contents of our struct to this memory
IntPtr liMemory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.LogonInfo)));
Marshal.StructureToPtr(li, liMemory, false);
Win32.CopyDataStruct cds;
cds.dwData = 123;
cds.cbData = Marshal.SizeOf(typeof(Win32.MyInfo));
cds.data = liMemory;
// Allocate memory for the COPYDATA struct and
// copy the contents of our struct to this memory
IntPtr cdsMemory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.CopyDataStruct)));
Marshal.StructureToPtr(cds, cdsMemory, false);
// Send the actual Win32 message
Win32.SendMessage(apptosendto.MainWindowHandle, Win32.WM_COPYDATA,
IntPtr.Zero, cdsMemory);
// Free allocated memory
Marshal.FreeHGlobal(liMemory);
Marshal.FreeHGlobal(cdsMemory);