By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Marshal.Copy for ushort[]?!

P: n/a
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.

Sep 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

Sep 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.