C#调用C++ 平台调用P/Invoke 结构体--内存对齐方式、union封装【七】

时间:2022-08-28 08:00:44

【1】内存对齐方式


C++代码:

#pragma pack(push)
#pragma pack(1)
typedef struct _testStru2
{
	int		iVal;
	char	cVal;
	__int64 llVal;
}testStru2;
#pragma pack(pop)
EXPORTDLL_API void Struct_PackN( testStru2 *pStru )
{
	if (NULL == pStru)
	{
		return;
	}

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

	wprintf(L"Struct_PackN \n");
}

C#代码,指定Pack即可:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public struct testStru2
{
    public int	 iVal;
    public sbyte cVal;
    public long  llVal;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_PackN(ref testStru2 pStru);

测试:

CExportDll.testStru2 stru2 = new CExportDll.testStru2();
CExportDll.Struct_PackN(ref stru2);

【2】Union中含有结构体


C++代码:

typedef union _testStru4
{
	int		iValLower;
	int		iValUpper;
	struct 
	{
		__int64 llLocation;
	};
}testStru4;
EXPORTDLL_API void Struct_Union( testStru4 *pStru )
{
	if (NULL == pStru)
	{
		return;
	}
	
	pStru->llLocation = 1024;
	wprintf(L"Struct_Union \n");
}


C#代码:定义成Explict,并显示通过FieldOffset指定内存偏移

[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]
public struct testStru4
{
    [FieldOffset(0)]
    int		iValLower;
    [FieldOffset(4)]
    int		iValUpper;
    [FieldOffset(0)]       
    long    llLocation;	        
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_Union(ref testStru4 pStru);


测试:

CExportDll.testStru4 stru4 = new CExportDll.testStru4();
CExportDll.Struct_Union(ref stru4);