开发者

C++ to C# conversion of SendMessage using COPYDATASTRUCT

开发者 https://www.devze.com 2023-01-08 01:15 出处:网络
I\'m converting a C++ application into C# which has generally been fairly straight forward, but now I\'m dealing with pointers and running into problems.

I'm converting a C++ application into C# which has generally been fairly straight forward, but now I'm dealing with pointers and running into problems.

This is the original C++ code

  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"));

with the structure definition

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

} ShockVideoInfo;

Here's my conversion to C#, using Marshalling to handle the pointers:

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);

In addition I'm using the following in my C# code:

[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;
}

My code isn't getting the response I want (which is moving a video window), so I've done something wrong.

Can anyone with experience in this area please show me what I'm doing wrong?


I think I've found my issue:

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

Should have been:

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

In other words, I wasn't passing the pointer to the videoInfo structure! Which helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消