C# SendMessage to C++ WinProc

会有一股神秘感。 提交于 2019-12-07 11:08:53

问题


I need to send a string from C# to a C++ WindowProc. There are a number of related questions on SO related to this, but none of the answers have 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've tried a number of different things, Marshalling the string as LPStr, LPWStr, also tried creating an IntPtr from unmanaged memory, and writing to it with Marshal.WriteByte.

The pointer is the correct memory location on the C++ side, but the data isn't there. What am I missing?


回答1:


For C++ LPWSTR or LPSTR parameters you need to use the C# StringBuilder in your DllImport.

For C++ LPCWSTR or LPCSTR parameters you need to use the C# string in your DllImport.




回答2:


Make sure that your SendMessage call is occurring in the expected, synchronous manner and that your NativeMethods class maps the proper Win32 call (Send vs. PostMessage.) If this isn't correct it's possible that by the time your message is processed on the C++ end, you've left the scope of your C# method and any local variables created on the stack are gone resulting in your bad pointer.

Stack and heap considerations for cross-thread calls: Threads have their own stacks but share the heap. Stack-allocated variables in one thread will not be visible in another. The string type is an odd duck in .NET. It is an Object-derived, reference type but made to look and feel like a value type in code. So perhaps passing a pointer to heap-allocated data ought to work. That's where StringBuilder, as a heap-allocated reference type, comes in.



来源:https://stackoverflow.com/questions/2653658/c-sharp-sendmessage-to-c-winproc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!