C
clawton
Hi All,
With the simple code below I get an exception on the last line of code...
Just trying to figure out some marshalling stuff...but I'm stuck...
Ultimately, the source pointer is data created in a DLL called by p/Invoke
and I need to get the data out of it into some structs.
Any help pointing out what I'm doing wrong here would be greatly appriciated!
Thanks in advance!
Chris
----------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace CopyTest
{
class Program
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct foo
{
public int x;
public double y;
};
static void Main(string[] args)
{
foo f = new foo();
f.x = 217;
f.y = -999.0;
IntPtr srcptr = Marshal.AllocHGlobal(Marshal.SizeOf(f));
Marshal.StructureToPtr(f, srcptr, true);
IntPtr[] one = new IntPtr[1];
Marshal.Copy(srcptr, one, 0, 1);
foo dest = (foo)Marshal.PtrToStructure(one[0], typeof(foo));
}
}
}
With the simple code below I get an exception on the last line of code...
Just trying to figure out some marshalling stuff...but I'm stuck...
Ultimately, the source pointer is data created in a DLL called by p/Invoke
and I need to get the data out of it into some structs.
Any help pointing out what I'm doing wrong here would be greatly appriciated!
Thanks in advance!
Chris
----------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace CopyTest
{
class Program
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct foo
{
public int x;
public double y;
};
static void Main(string[] args)
{
foo f = new foo();
f.x = 217;
f.y = -999.0;
IntPtr srcptr = Marshal.AllocHGlobal(Marshal.SizeOf(f));
Marshal.StructureToPtr(f, srcptr, true);
IntPtr[] one = new IntPtr[1];
Marshal.Copy(srcptr, one, 0, 1);
foo dest = (foo)Marshal.PtrToStructure(one[0], typeof(foo));
}
}
}