Discussion:
Copydata
(too old to reply)
Arbie
2009-08-24 22:31:48 UTC
Permalink
Hi

I am rewriting a C++ application to C# and am a bit stuck on translating a
Copydata structure. This is used to copy a block of ushort data from another
application.

I have defined

public class Win32
{

[StructLayout(LayoutKind.Sequential)]
public class CopyDataStruct
{
public int dwData;
public int cbData;
public IntPtr data;
}
}

and am receiving the message ok with

unsafe protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COPYDATA)
{

Win32.CopyDataStruct cds = new Win32.CopyDataStruct();

Marshal.PtrToStructure((IntPtr)m.LParam.ToPointer(), cds);
}

base.WndProc(ref m);
}



but how do I use the IntPtr cds.data to fill out a buffer of
matching size

something like
Array.Copy((System.Array)cds.data, adcBuffer, adcBufferSize);

thanks for any help

Rambler
Kevin Westhead
2009-09-18 18:09:47 UTC
Permalink
[...]
but how do I use the IntPtr cds.data to fill out a buffer of matching size
something like
Array.Copy((System.Array)cds.data, adcBuffer, adcBufferSize);
thanks for any help
Rambler
You can use Marshal.Copy to copy from an IntPtr to a managed array and vice
versa. I don't think there are overloads for ushort so you'll have to copy
the data to an array of short and then convert to ushort. Something like
this:

short[] signedBuffer = new short[bufferSize];
Marshal.Copy(cds.data, signedBuffer, 0, bufferSize);
ushort[] unsignedBuffer = Array.ConvertAll(signedBuffer, input =>
(ushort)input);
--
Kevin Westhead
Loading...