Marshal在C#中的应用(void *指针到IntPtr的转化)

  C#调用C语言的API时一般把void *指针转换成IntPtr,但这经常远远不够的。在C语言中void *是个万金油,尤其是一些老的c语言程序,所有的参数就一个void*指针,里面包罗万象,然后在程序中来一个switch,甚至多个switch来处理不同的参数。最近笔者就碰到了这个问题,不得不来研究一下怎么把void *指针转换成IntPtr。

1.void *指针到IntPtr的简单转化。

c语言函数原型:

int SetConfig(int type, void *p);

这里假设p的所传递的参数式是结构体A:

struct A			
{
	wchar_t osdbuffer[100];				
	unsigned short ix;						
	unsigned short iy;						
};

 那么在C#中原型可以定义如下:

int SetConfig(int type, IntPtr p);

结构体A 

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
		public struct A {
			[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
			public string osdbuffer;		
			public ushort ix;						//显示坐标x
			public ushort iy;						//显示坐标y
		}

  注意这里的CharSet,它由c中wchar_t决定的,如果c程序编译时使用Unicode,这里就用CharSet.Unicode,否则使用CharSet.Ansi。关于字符串的编码问题如果不懂可以去网上查一下。至于怎么知道C语言是用Unicode还是Ansi编译,我是经过调用它的API测试出来的,调用成功了就说明他的编码和我的调用代码一致。

  这里还有一个很重要的问题,那就是内存在编译时的分配问题。一般默认情况下,内存的分配是4byte的整数倍,在这里我省略了,但为了便于理解,补充一下。结构体A完整一点的定义:(注意Pack的值)

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode,Pack = 4)]
		public struct A {
			[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
			public string osdbuffer;		
			public ushort ix;						//显示坐标x
			public ushort iy;						//显示坐标y
		}

c语言是用的是非托管代码,c#使用的是托管代码,c#的调用代码如下:

    A s_a = new A();
  int lenght = Marshal.SizeOf(s_a); IntPtr pA= Marshal.AllocHGlobal(lenght); Marshal.StructureToPtr(s_a, pA, true); int type = 1;
int ret = SetConfig( type, pA); Marshal.FreeHGlobal(pA);

2.void *指针到IntPtr的复杂转化。

在这里结构体A变得复杂一点,如果它内部包含一个指向另一个结构体B的指针

struct A            
{
    wchar_t osdbuffer[100];            
    unsigned short ix;            
    unsigned short iy;、
    B *pB;            
};
struct B            
{
    wchar_t title[20];     
};

在C#中你要做的也就稍微复杂一点,也就是说你不但要为A分配内存,也要为B分配内存

  B s_b = new B();
  //赋值省略
  int
lenght1 = Marshal.SizeOf(s_b); IntPtr pB= Marshal.AllocHGlobal(lenght1); Marshal.StructureToPtr(s_b, pB, true);   A s_a = new A();
  s_a.pB = pB;
  //其他赋值
  //
  int
lenght2 = Marshal.SizeOf(s_a);   IntPtr pA= Marshal.AllocHGlobal(lenght2); Marshal.StructureToPtr(s_a, pA, true); int type = 1;
int ret = SetConfig( type, pA); Marshal.FreeHGlobal(pB); Marshal.FreeHGlobal(pA);

万变不离其宗,只要掌握了原理,不管void *指针传递的参数有多么复杂,都可以搞定。

 

 

 

 

posted on 2013-03-18 01:17  钟湘光  阅读(16692)  评论(1编辑  收藏  举报

导航