I need to send a string from C # to C ++ WindowProc. There are a number of related SO questions related to this, but none of the answers worked for me. Here's the situation:
PInvoke: [DllImport("user32", CharSet = CharSet.Auto)] public extern static int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, string lParam); C#: string lparam = "abc"; NativeMethods.User32.SendMessage(handle, ConnectMsg, IntPtr.Zero, lparam); C++: API LRESULT CALLBACK HookProc (int code, WPARAM wParam, LPARAM lParam) { if (code >= 0) { CWPSTRUCT* cwp = (CWPSTRUCT*)lParam; ... (LPWSTR)cwp->lParam <-- BadPtr ... } return ::CallNextHookEx(0, code, wParam, lParam); }
I tried several different things: Marshalling string like LPStr, LPWStr, also tried to create IntPtr from unmanaged memory and write it with Marshal.WriteByte.
A pointer is a valid memory cell on the C ++ side, but there is no data there. What am I missing?
source share