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="Peter Duniho, post: 13614306"] That C++ function doesn't do anything. Perhaps you meant "Header **target" and "*target = (Header *)source"? That's true. But if the caller discards the byte array that "source" points to, you're in a world of hurt. If you write the _entire_ thing as unsafe code, always dereferencing the data via a pointer to your structure, it could work. Otherwise, no...you simply can't do that in C#. Managed types and pointers are completely different. For what it's worth, you should understand that the "StructLayout" attribute doesn't actually force a particular layout for the managed type. It's used for marshaling only, to indicate how the structure should look to unmanaged code. Anyway... As with the previous question, IMHO you should not be using the low-level stuff at all. Just copy the bytes and forget the marshaling altogether: void CopyByteToHeader(Header target, byte[] source) { target.m_type = new byte[1]; Array.Copy(source, 0, target.m_type, 0, 1); target.m_lMsgIndex = new byte[4]; Array.Copy(source, 1, target.m_lMsgIndex, 0, 4); target.m_lLength = new byte[4]; Array.Copy(source, 5, target.m_lLength, 0, 4); } Though, frankly...storing that data as byte[] in your Header class doesn't make any sense to me. IMHO, what you really ought to be doing is just going ahead and converting the bytes to their appropriate values, and storing them as that. In other words, all three fields wind up being "int" fields instead of "byte[]" and are the actual values of interest. After all, if you ever expect to use the data as anything other than bytes, you'll have to do that conversion eventually anyway. And if you don't ever expect to use the data as anything other than bytes, there's really no point in chopping up the original byte[] in the first place. Pete [/QUOTE]
Verification
Post reply
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
How to Copy like C++?
Top