Marshal.Copy for ushort[]?!

D

dast

I would need a function to copy unsigned short values from a IntPtr to
a ushort[]-array, but the Marshal.Copy-function only support the
short[]-array!

At the moment I do it the following way:

...

IntPtr pResult;
int len;

// this function is located in a DLL
CanIpuExecCmd(mid, hid, did, data, data.Length, out pResult, out
len);

short[] tmp = new short[len];
Marshal.Copy(pResult, tmp, 0, len);
Marshal.FreeHGlobal(pResult);

ushort[] result = new ushort[tmp.Length];
tmp.CopyTo(result, 0);

return result;

Is there no nicer way?

Regards,
Daniel.
 
D

dast

Sorry, this is the correct code I am using at the moment:

....

IntPtr pResult;
int len;

// this function is located in a DLL
CanIpuExecCmd(mid, hid, did, data, data.Length, out pResult, out
len);

short[] tmp = new short[len];
Marshal.Copy(pResult, tmp, 0, len);
Marshal.FreeHGlobal(pResult);

ushort[] result = new ushort[tmp.Length];
System.Buffer.BlockCopy(tmp, 0, result, 0, len * 2);

return result;

Is there no nicer way?

Regards,
Daniel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top