int指向char数组的指针

时间:2014-03-06 02:58:38

标签: c# winapi

我需要在C#的Windows API中使用Send_Message()函数。我的一切都很好,除了最后一个参数WPARAM,我在研究中发现它是一个unsigned int。我正在发送0x402消息(SB_GETTEXT),而WPARAM应该表示指向文本将被放置的字符数组的指针。

这是我的代码

    unsafe
    {
        char[] result = new char[40];
        int* ptr = (*int)&result;
    }

但是我收到以下错误:

    Cannot take the address of, get the size of, or declare a pointer to a managed type ('char[]')

是否与'result = new char [40]'部分有关?

感谢。

1 个答案:

答案 0 :(得分:1)

我认为你想要的是:

[DllImport("user32.dll")]
static extern IntPtr SendMessage(
    IntPtr hWnd, 
    uint Msg, 
    long wParam, 
    [MarshalAs   (UnmanagedType.LPStr)] StringBuilder lParam);

请注意,根据the documentationlParam是指向字符串的指针,而不是wParam,正如您的问题所示。

您可能也对pinvoke.net上的说明感兴趣:http://www.pinvoke.net/default.aspx/user32/SB_GETTEXT.html

相关问题