码迷,mamicode.com
首页 > 编程语言 > 详细

C#调用C++ 平台调用P/Invoke 结构体--输入输出参数、返回值、返出值、结构体数组作为参数【五】

时间:2014-09-20 14:09:57      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:pinvoke   c++   c#   结构体   

【1】结构体作为输入输出参数


C++代码:

typedef struct _testStru1
{
	int		iVal;
	char	cVal;
	__int64 llVal;
}testStru1;
EXPORTDLL_API void Struct_Change( testStru1 *pStru )
{
	if (NULL == pStru)
	{
		return;
	}

	pStru->iVal = 1;
	pStru->cVal = 'a';
	pStru->llVal = 2;

	wprintf(L"Struct_Change \n");
}

C#代码:指定为ref即可

/*   1.以StructLayout来标记结构体,指定结构体内存布局
 *   2.字段定义的顺序 
 *   3.字段类型 
 *   4.字段在内存中的大小 
 *   5.非托管与托管结构名称可以不同 
 */ 
//4.1 结构体作为输入输出参数
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct testStru1
{
   public int		iVal;
   public sbyte	    cVal;
   public long      llVal;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_Change(ref testStru1 pStru);

测试:

CExportDll.testStru1 stru1 = new CExportDll.testStru1();
CExportDll.Struct_Change(ref stru1);


【2】结构体作为返回值


C++代码:

typedef struct _testStru5
{
	int		iVal;
}testStru5;
testStru5	g_stru5;
EXPORTDLL_API testStru5* Struct_Return()
{	
	g_stru5.iVal = 5;
	wprintf(L"Struct_Return \n");
	return(&g_stru5);
}

C#代码,定义返回值为IntPtr,再进行解析:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct testStru5
{
    public int	iVal;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr Struct_Return();

测试:

IntPtr struIntPtr = CExportDll.Struct_Return();
CExportDll.testStru5 stru5 = (CExportDll.testStru5)(Marshal.PtrToStructure(struIntPtr, typeof(CExportDll.testStru5)));

【3】结构体数组作为参数


C++代码:

typedef struct _testStru6
{
	int		iVal;
}testStru6;
EXPORTDLL_API void Struct_StruArr( testStru6 *pStru, int len )
{
	if (NULL == pStru)
	{
		return;
	}

	for ( int ix=0; ix<len; ix++)
	{
		pStru[ix].iVal = ix;
	}

	wprintf(L"Struct_StruArr \n");
}

C#代码:定义成testStru6[]即可,如果需要返回修改后的值,则需要指定[In,Out]参数

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct testStru6
{
    public int		iVal;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_StruArr([In, Out]testStru6[] pStru, int len);

测试:

CExportDll.testStru6 []stru6 = new CExportDll.testStru6[5];
CExportDll.Struct_StruArr(stru6, 5);

【4】结构体作为返出参数,释放非托管的内存


C++代码:

typedef struct _testStru8
{
	int		m;
}testStru8;
EXPORTDLL_API void Struct_ParameterOut( testStru8 **ppStru )
{
	if (NULL == ppStru)
	{
		return;
	}

	*ppStru = (testStru8*)CoTaskMemAlloc(sizeof(testStru8));

	(*ppStru)->m	= 8;
	wprintf(L"Struct_ParameterOut \n");
}

C#代码:定义成ref IntPtr即可,需要解析

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct testStru8
{
    public int		m;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_ParameterOut(ref IntPtr ppStru);

测试:

<strong>IntPtr outPtr = IntPtr.Zero;
CExportDll.Struct_ParameterOut(ref outPtr);
CExportDll.testStru8 stru8 = (CExportDll.testStru8)(Marshal.PtrToStructure(outPtr, typeof(CExportDll.testStru8)));
Marshal.FreeCoTaskMem(outPtr);
</strong>


C#调用C++ 平台调用P/Invoke 结构体--输入输出参数、返回值、返出值、结构体数组作为参数【五】

标签:pinvoke   c++   c#   结构体   

原文地址:http://blog.csdn.net/aoshilang2249/article/details/39429243

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!