关于c++向c#传送字符串的问题
我在C程序里代码如下
HWND hWnd = FindWindow(NULL,_T("WawaApp"));
char *str = new char[3];
str = "123";
SendMessage(hWnd,WM_WAWADEFMSG,(WPARAM)str,strlen(str));
c#代码如下
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int RegisterWindowMessage(string lpString);
private readonly int WM_WAWADEFMSG = RegisterWindowMessage("WawaDefMsg");
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
if(m.Msg == WM_WAWADEFMSG)
{
int len = m.LParam.ToInt32();
string str = Marshal.PtrToStringUni(m.WParam, len );
MessageBox.Show(str);
return;
}
base.WndProc(ref m);
}
可怎么也得不到c里面传过来的值,C里面我用PostMessage和SendMessage都试过了,c#里用Marshal.PtrToStringUni,Marshal.PtrToStringAnsi,Marshal.PtrToStringAuto等也都试过了,都不行。
结贴了,因为是两个进程,所以用消息把指针发送过去,c#是不会认的,所以要用原子,内存文件映射等才行。