将C#结构传递给非托管代码
问题描述:
大家好,
我怀疑将c#结构传递给非托管代码。我需要相当于C中的C#
例如
Hi All,
I have a doubt in passing the c# structure to unmanaged code.I need the equivalent of C in C#
For example
typedef struct stacks
{
NL_F1DNODE * f1d;
} NL_STACKS;
typedef struct f1dnode {
NL_FLAG *ptr;
struct f1dnode *next;
} NL_F1DNODE;
函数C
NL_VOID N_InitNurbs(NL_STACKS * S)
我需要相当于上面提到的c#中的C结构和上面定义的函数。请任何人指导我。
function in C
NL_VOID N_InitNurbs( NL_STACKS *S )
I need the equivalent of C struct in c# above mentioned and equivalent of the function defined above .Please anyone guide me.
答
您好我已尝试过您的问题,但不知道它可能对您有什么帮助请看看我的代码片段:
我使用了托管指针Inptr来保存每个结构对象指针的内存地址,并且没有类似于C中的类型def的内容#
Hi I have tried something around your problem but don't know how it may help you please have a look at my code snippet:
I have used Managed pointer Inptr to hold memory address of each structure object pointer and there is nothing like type def in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ManagedPointers
{
class Program
{
struct stacks
{
public IntPtr NL_F1DNODE;
};
struct f1dnode
{
public IntPtr NL_FLAG;
public IntPtr next;
};
struct nlflag
{
public string flagname;
public bool flagvalue;
};
static void Main(string[] args)
{
nlflag[] arraynlflag;
arraynlflag = Create_nlflags();
f1dnode[] arrayf1nodes;
arrayf1nodes = Create_f1Node(arraynlflag);
stacks objStack = new stacks();
IntPtr f1nodepointer = Marshal.AllocHGlobal(Marshal.SizeOf(arrayf1nodes[0]));
Marshal.StructureToPtr(arrayf1nodes[0], f1nodepointer, false);
objStack.NL_F1DNODE = f1nodepointer;
IntPtr stackpointer = Marshal.AllocHGlobal(Marshal.SizeOf(objStack));
Marshal.StructureToPtr(objStack, stackpointer , false);
ReadFromStack(stackpointer);
Console.ReadLine();
}
static nlflag[] Create_nlflags()
{
nlflag[] arraynlflag = new nlflag[8];
for (int i = 0; i < 4; i++)
{
arraynlflag[i].flagname = "flag_" + i.ToString();
arraynlflag[i].flagvalue = i % 2 == 0?true:false;
}
return arraynlflag;
}
static f1dnode[] Create_f1Node(nlflag[] arraynlflag)
{
f1dnode[] arrayf1nodes = new f1dnode[4];
IntPtr nflagpointer3 = Marshal.AllocHGlobal(Marshal.SizeOf(arraynlflag[3]));
Marshal.StructureToPtr(arraynlflag[3], nflagpointer3, false);
arrayf1nodes[3].NL_FLAG = nflagpointer3;
arrayf1nodes[3].next = IntPtr.Zero;
////////////////////////////////////////////////////////////////////////////////
IntPtr nextpointer1 = Marshal.AllocHGlobal(Marshal.SizeOf(arrayf1nodes[3]));
Marshal.StructureToPtr(arrayf1nodes[3], nextpointer1, false);
IntPtr nflagpointer2 = Marshal.AllocHGlobal(Marshal.SizeOf(arraynlflag[2]));
Marshal.StructureToPtr(arraynlflag[2], nflagpointer2, false);
arrayf1nodes[2].NL_FLAG = nflagpointer2;
arrayf1nodes[2].next = nextpointer1;
//////////////////////////////////////////////////////////////////////////////////
IntPtr nextpointer2 = Marshal.AllocHGlobal(Marshal.SizeOf(arrayf1nodes[2]));
Marshal.StructureToPtr(arrayf1nodes[2], nextpointer2, false);
IntPtr nflagpointer1 = Marshal.AllocHGlobal(Marshal.SizeOf(arraynlflag[1]));
Marshal.StructureToPtr(arraynlflag[1], nflagpointer1, false);
arrayf1nodes[1].NL_FLAG = nflagpointer1;
arrayf1nodes[1].next = nextpointer2;
///////////////////////////////////////////////////////////////////////////////
IntPtr nextpointer3 = Marshal.AllocHGlobal(Marshal.SizeOf(arrayf1nodes[1]));
Marshal.StructureToPtr(arrayf1nodes[1], nextpointer3, false);
IntPtr nflagpointer0 = Marshal.AllocHGlobal(Marshal.SizeOf(arraynlflag[0]));
Marshal.StructureToPtr(arraynlflag[0], nflagpointer0, false);
arrayf1nodes[0].NL_FLAG = nflagpointer0;
arrayf1nodes[0].next = nextpointer3;
return arrayf1nodes;
}
static void ReadFromStack(IntPtr stackpointer)
{
stacks ObjectStack;
ObjectStack = (stacks)Marshal.PtrToStructure(stackpointer, typeof(stacks));
Marshal.FreeHGlobal(stackpointer);
f1dnode Objf1node0;
Objf1node0 = (f1dnode)Marshal.PtrToStructure(ObjectStack.NL_F1DNODE, typeof(f1dnode));
Marshal.FreeHGlobal(ObjectStack.NL_F1DNODE);
f1dnode Objf1nodeTemp;
while (Objf1node0.next != IntPtr.Zero)
{
nlflag objnlflag1 = (nlflag)Marshal.PtrToStructure(Objf1node0.NL_FLAG, typeof(nlflag));
Console.WriteLine("Falg Name:{0} Falg Value:{1}", objnlflag1.flagname, objnlflag1.flagvalue);
Marshal.FreeHGlobal(Objf1node0.NL_FLAG);
Objf1nodeTemp = (f1dnode)Marshal.PtrToStructure(Objf1node0.next, typeof(f1dnode));
Marshal.FreeHGlobal(Objf1node0.next);
Objf1node0 = Objf1nodeTemp;
}
}
}
}