I'm trying to send a String from C++/CLI to C#/WinForms
Here is my PostMessage
void Browser::NavigateTo(System::String^ address){
GCHandle gch = GCHandle::Alloc(address, GCHandleType::Pinned);
auto GCPin = gcHandle.AddrOfPinnedObject();
::PostMessage(procWndHandle, WM_NAVTO, 0, (LPARAM &GCPin);
}
In the Winform Function, i'm trying to marshal it like this
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust"]
protected override void WndProc(ref Message m)
switch (m.Msg) {
case WM_NAVTO:
string s = Marshal.PtrToStringUni(m.LParam);
break;
}
}
I'm not getting an exception, however I'm not getting the address I sent, instead I'm getting Unicode Garbage.
I'm sure I'm missing something fundamental, but I can't seem to see it. Can anyone help?
There's a couple issues with your current code:
PostMessage
does not wait for the receiving application before returning. Therefore, the pinned object becomes unpinned before the message has been processed, and may be moved to a new location in memory. (This is probably not the major issue here.) To fix #2, you need to copy the memory from one process to the other, somehow.
WM_COPYDATA
message. This message will have Windows copy your data (the string contents, in this case) from one process to the other. See the Using Data Copy example on MSDN.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.