Click here to Skip to main content
15,902,939 members
Home / Discussions / C#
   

C#

 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
David Stone23-Aug-05 12:33
sitebuilderDavid Stone23-Aug-05 12:33 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
Guffa23-Aug-05 12:53
Guffa23-Aug-05 12:53 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
Daniel132423-Aug-05 13:14
Daniel132423-Aug-05 13:14 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
Daniel132423-Aug-05 13:29
Daniel132423-Aug-05 13:29 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
David Stone23-Aug-05 12:34
sitebuilderDavid Stone23-Aug-05 12:34 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
senorbadger10-Mar-09 7:12
senorbadger10-Mar-09 7:12 
QuestionHow to copy struct to byte [] ? Pin
queisser23-Aug-05 11:04
queisser23-Aug-05 11:04 
AnswerRe: How to copy struct to byte [] ? Pin
Bojan Rajkovic23-Aug-05 19:44
Bojan Rajkovic23-Aug-05 19:44 
Well, to copy a struct to a byte[], you need some marshaling code..It uses the Marshal class (as if that wasn't obvious) to allocate some memory..Like this:

(i usually declare methods like this as an instance method and a static method with a parameter of whatever type it's defined in)

C#
<pre><br />
public byte[] ToBytes()<br />
{<br />
   byte[] buff = new byte[Marshal.SizeOf(typeof(MyType))];<br />
   GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);<br />
   Marshal.StructureToPtr(this, handle.AddrOfPinnedObject(), false);<br />
   handle.Free();<br />
   return buff;<br />
}<br />
</pre><br />
<br />
That code will marshal the struct to a byte[], and from that code, the code for marshaling back to a struct comes easy:<br />
<br />
<code lang=cs><pre><br />
public static MyType FromBytes(byte[] buff)<br />
{<br />
   GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);<br />
   MyType mt = (MyType)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(MyType)); // because ptrtostructure returns an object :-\<br />
   handle.Free();<br />
   return mt;<br />
}<br />
</pre><br />
<br />
<br />
Now as for chaining structures together, you can do that yourself using Array.Copy() and copying into a byte[]<br />
<br />
Passing back the data as an IntPtr requires only slight modification to the original functions:<br />
<br />
<code lang=cs><pre><br />
public IntPtr ToIntPtr()<br />
{<br />
   byte[] buff = new byte[Marshal.SizeOf(typeof(MyType))];<br />
   IntPtr handle = Marshal.AllocHGlobal(buff.Length);<br />
   Marshal.StructureToPtr(this, handle, true); // passing false for that last param can cause memory leaks<br />
   return handle;<br />
}<br />
<br />
public static MyType FromIntPtr(IntPtr ptr)<br />
{<br />
   MyType mt = (MyType)Marshal.PtrToStructure(ptr, typeof(MyType)); // because ptrtostructure returns an object :-\<br />
   //Marshal.FreeHGlobal(ptr); - only do this if you know you won't be using that IntPtr again<br />
   return mt;<br />
}<br />


Note: That code is not 100% guaranteed to compile or work, I didn't test it very much, but it should work, and the basic concept is more or less correct.

If you have any more questions, feel free to ask.
AnswerRe: How to copy struct to byte [] ? Pin
leppie23-Aug-05 21:47
leppie23-Aug-05 21:47 
GeneralSelection Frames Pin
Shawn Dwyer23-Aug-05 10:06
Shawn Dwyer23-Aug-05 10:06 
GeneralDatagrid help! Pin
dgap23-Aug-05 8:27
dgap23-Aug-05 8:27 
GeneralRe: Datagrid help! Pin
Guffa23-Aug-05 9:21
Guffa23-Aug-05 9:21 
QuestionHow can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 7:40
professionalTesic Goran23-Aug-05 7:40 
AnswerRe: How can I prevent my control to be serialized in design-mode? Pin
Daniel132423-Aug-05 8:05
Daniel132423-Aug-05 8:05 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 8:12
professionalTesic Goran23-Aug-05 8:12 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Daniel132423-Aug-05 8:39
Daniel132423-Aug-05 8:39 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 9:12
professionalTesic Goran23-Aug-05 9:12 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Daniel132423-Aug-05 9:17
Daniel132423-Aug-05 9:17 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 9:25
professionalTesic Goran23-Aug-05 9:25 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
lmoelleb23-Aug-05 21:35
lmoelleb23-Aug-05 21:35 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Dave Kreskowiak23-Aug-05 8:42
mveDave Kreskowiak23-Aug-05 8:42 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 9:46
professionalTesic Goran23-Aug-05 9:46 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Dave Kreskowiak23-Aug-05 10:00
mveDave Kreskowiak23-Aug-05 10:00 
QuestionHow can I check inside the control where I am - in design mode or run-time mode? Pin
Anonymous23-Aug-05 7:34
Anonymous23-Aug-05 7:34 
AnswerRe: How can I check inside the control where I am - in design mode or run-time mode? Pin
gnjunge23-Aug-05 7:42
gnjunge23-Aug-05 7:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.