Home
Forums
New posts
Search forums
Articles
Latest reviews
Search resources
Members
Current visitors
Newsgroups
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
How to Copy like C++?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Sharon, post: 13612845"] Hello Gurus, I have a function that copy data from byte[] to class as follow: public static void CopyByteToHeader(Header target, byte[] source) { ntPtr pHeaderTarget = Marshal.AllocHGlobal(HEADER_SIZE); arshal.Copy(source, 0, pHeaderTarget, HEADER_SIZE); arshal.PtrToStructure(pHeaderTarget, target); } public const int HEADER_SIZE = 9; [StructLayout(LayoutKind.Sequential)] public class Header { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public byte[] m_type; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] m_lMsgIndex; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] m_lLength; } The function works fine. BUT: This function is executed many times, so it makes a lot of memory copeing. I want to optimize it doing like in C++: Public CPP_CopyByteToHeader(Header* target, byte* source) { target = (Header*)source } This way I’m avoiding the copy. But is C# I tried: public static void CopyByteToHeader(Header target, byte[] source) { unsafe { fixed (byte* pSource = (byte[])source) { // Now I need the pointer for the target. // But I can’t find how to! } } } As you can see its incomplete. I need to get a pointer to the target. Can anybody show me how can I do the coply like, like it’s done in the C++ above? [/QUOTE]
Verification
Post reply
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
How to Copy like C++?
Top