C#: Marshalling a “pointer to an int array” from a SendMessage() lParam

前端 未结 1 764
长发绾君心
长发绾君心 2021-01-24 01:12

I\'m trying to subclass an unmanaged statusbar window from my managed COM server using a class inherited from NativeWindow, and am running into a wall trying to make sense of ho

1条回答
  •  别那么骄傲
    2021-01-24 01:36

    Following the "dtb"'s comment, you can borrow some code from this SO entry.

    The LPARAM you must supply is a pointer to the first element of the array. Then all you have to do is:

    int[] parts = new int[]{ 1, 2, 3, 4 };
    int nParts = parts.Length;
    IntPtr pointer = Marshal.AllocHGlobal(nParts * Marshal.SizeOf(typeof(int)));
    for (int i = 0; i < nParts; i++) {
        Marshal.WriteInt32(pointer, i * Marshal.SizeOf(typeof(int), parts[i]));
    }
    // Call SendMessage with WPARAM = nParts and LPARAM = Pointer
    Marshal.FreeHGlobal(pointer);
    

    0 讨论(0)
提交回复
热议问题