4 Replies - 608 Views - Last Post: 27 September 2013 - 01:48 PM Rate Topic: -----

#1 pbshines  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 13

How to convert an unmanaged structure pointer to byte[] in c#?

Posted 26 September 2013 - 07:03 PM

Please help me to convert an unmanaged structure pointer to byte[] in c#?

public struct struct1
{
    public byte a;
    public int b;
}

struct1 objstruct= new struct1();
struct1* ptrStruct=&objstruct;
ptrStruct->a=1;
ptrStruct->b=4;


how can i convert ptrStruct to byte[]?

please help me

This post has been edited by Atli: 26 September 2013 - 07:06 PM
Reason for edit:: Use [code] tags when posting code.

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert an unmanaged structure pointer to byte[] in c#?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: How to convert an unmanaged structure pointer to byte[] in c#?

Posted 26 September 2013 - 08:06 PM

Why are you trying to do this in C#? You would have much less friction trying to do this in C++.
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,519
  • Joined: 08-April 09

Re: How to convert an unmanaged structure pointer to byte[] in c#?

Posted 26 September 2013 - 11:23 PM

By using Marshaling
Marshals data from an unmanaged block of memory(your structure pointer) to a newly allocated managed object(your byte[]) of the specified type.

If you are more comfortable with pointers than objects in C# (or you need pointers for something), I don't see any reason to not use the pointers.

This post has been edited by Michael26: 26 September 2013 - 11:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

Re: How to convert an unmanaged structure pointer to byte[] in c#?

Posted 27 September 2013 - 07:37 AM

Quote

If you are more comfortable with pointers than objects in C# (or you need pointers for something), I don't see any reason to not use the pointers.


I've been doing this a while, and I've never actually needed unsafe code in C#. Clearly that's not always going to be the case for everyone, but I would think very long and very hard before putting them in an important project. Is there a way I could do this with managed code? Could I make a C++ wrapper and call that through P/Invoke? Are the other developers that read this going to be able to maintain it?

Those kinds of questions. And I think that simple comfort isn't a good enough reason.
Was This Post Helpful? 1
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to convert an unmanaged structure pointer to byte[] in c#?

Posted 27 September 2013 - 01:48 PM

View PostCurtis Rutland, on 27 September 2013 - 07:37 AM, said:

I've been doing this a while, and I've never actually needed unsafe code in C#.


I too have been doing this a while, and while I've never needed unsafe code, I have used it. There are some things that execute faster when unsafe (and I wanted to keep it all C#, so no C/C++ DLLs :)) (unlike Microsoft. They call C/C++ DLLs in the .NET classes quite frequently).

In this case, I see no need for pointers and unsafe code, it isn't going to benefit from them.

This post has been edited by Momerath: 27 September 2013 - 01:50 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1