views:

115

answers:

1

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?

A: 

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

Peter Bridger