使用COPYDATASTRUCT对SendMessage进行C ++到C#的转换

时间:2010-07-21 13:47:15

标签: c# c++ pointers marshalling sendmessage

我正在将一个C ++应用程序转换为C#,它通常很直接,但现在我正在处理指针并遇到问题。

这是原始的 C ++ 代码

  ShockVideoInfo* pVideoInfo = new ShockVideoInfo;
  COPYDATASTRUCT  cd;

  cd.dwData = bSelf ? SHOCK_REQUEST_SELFVIEW_WINDOW : SHOCK_REQUEST_MAINVIEW_WINDOW;
  cd.lpData = pVideoInfo;
  cd.cbData = sizeof(ShockVideoInfo);

  pVideoInfo->dwLeft   = 10;
  pVideoInfo->dwTop    = 10;
  pVideoInfo->dwWidth  = 100;
  pVideoInfo->dwHeight = 100;
  pVideoInfo->dwFlags  = SHVIDSHOW | SHVIDSIZE | SHVIDTOPMOST | SHVIDMOVE | SHVIDSIZABLE;

  if (::SendMessage(m_hShockWnd, WM_COPYDATA, (WPARAM)GetSafeHwnd(), (LPARAM)&cd))
    Log(eResult, _T("Window updated"));
  else
    Log(eError, _T("Window not updated"));

结构定义

typedef struct tagShockVideoInfo
{
  DWORD dwLeft;
  DWORD dwTop;
  DWORD dwWidth;
  DWORD dwHeight;
  DWORD dwFlags;

} ShockVideoInfo;

这是我转换为 C#,使用编组来处理指针:

ShockVideoInfo videoInfo = new ShockVideoInfo();
COPYDATASTRUCT cd = new COPYDATASTRUCT();

cd.dwData = (IntPtr)_shockRequestMainviewWindow;
cd.lpData = Marshal.AllocCoTaskMem(cd.cbData); // Pointer to videoInfo
Marshal.StructureToPtr(videoInfo, cd.lpData, true);
cd.cbData = Marshal.SizeOf(videoInfo);        

// Set-up video window position
videoInfo.dwFlags = _shvidShow | _shvidSize | _shvidTopmost | _shvidMove | _shvidSizable;
videoInfo.dwTop = 10;
videoInfo.dwLeft = 10;
videoInfo.dwHeight = 100;
videoInfo.dwWidth = 100;

IntPtr cdPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(videoInfo));
Marshal.StructureToPtr(videoInfo, cdPointer, true);

if ( (int) SendMessage(_hWnd, WM_COPYDATA, _formHandle, cdPointer) == 1 )
{
    Debug.WriteLine("Window updated");
}
else
{
    Debug.WriteLine("Window not updated");
}

Marshal.FreeCoTaskMem(cdPointer);
Marshal.FreeCoTaskMem(cd.lpData);

另外我在C#代码中使用以下内容:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential)]    
struct ShockVideoInfo
{
    public uint dwLeft;
    public uint dwTop;
    public uint dwWidth;
    public uint dwHeight;
    public uint dwFlags;
}

[StructLayout(LayoutKind.Sequential)]
struct COPYDATASTRUCT
{
    public IntPtr dwData;
    public int cbData;
    public IntPtr lpData;
}

我的代码没有得到我想要的响应(正在移动视频窗口),所以我做错了。

任何有这方面经验的人都可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:2)

我想我找到了自己的问题:

IntPtr cdPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(videoInfo));
Marshal.StructureToPtr(videoInfo, cdPointer, true);

应该是:

IntPtr cdPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(cd));
Marshal.StructureToPtr(cd, cdPointer, false);

换句话说,我没有将指针传递给 videoInfo 结构!这有助于:)