struct to IntPtr?
Hi all,
Is there a way to create an IntPtr from a struct rather then this way?
-----------------------------------------------
namespace csConsole
{
using System;
using System.Runtime.InteropServices;
struct Test
{
public int Value;
public Test(int e)
{Value = e;}
public override string ToString()
{
return string.Format("Test.Value = {0}",Value);
}
}
class MessageTest
{
public unsafe static IntPtr Get(Test* t)
{
IntPtr ptr = new IntPtr(t);
Marshal.StructureToPtr(new Test(12345) ,ptr ,true);
return ptr;
}
public static unsafe void Main()
{
Test* tPtr = stackalloc Test[1];
Test d = (Test)Marshal.PtrToStructure(Get(tPtr) ,typeof(Test));
Console.WriteLine(d);
Console.Read();
}
}
}
-----------------------------------------------
This way I need unsafe context and allocate space on the stack which is only usable in the methods scope.
Is there another way of creating and maintaining IntPtrs?
Thanks in advance
Replies
Know the answer? Post it — somebody with the same question will find it here.