C#调用C++ 平台调用P/Invoke 结构体--含有内置数据类型的一维、二维数组、字符串指针【六】

【1】结构体中含有内置数据类型的一维数组

C++代码:

typedef struct _testStru3
{
	int		iValArrp[30];
	WCHAR	szChArr[30];
}testStru3;
EXPORTDLL_API void Struct_ChangeArr( testStru3 *pStru )
{
	if (NULL == pStru)
	{
		return;
	}

	pStru->iValArrp[0] = 8;
	lstrcpynW(pStru->szChArr, L"as", 30);

	wprintf(L"Struct_ChangeArr \n");
}

C#代码:定义成数组并通过SizeConst指定其长度(对于字符数组,可以直接指定为string)

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct testStru3
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=30)]
    public int		[]iValArrp;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
    public string szChArr;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_ChangeArr(ref testStru3 pStru);

测试:

CExportDll.testStru3 stru3 = new CExportDll.testStru3();
CExportDll.Struct_ChangeArr(ref stru3);

【2】结构体中含有内置数据类型的二维数组

C++代码:

typedef struct _testStru7
{
	int		m[5][5];
}testStru7;
EXPORTDLL_API void Struct_Change2DArr( testStru7 *pStru )
{
	if (NULL == pStru)
	{
		return;
	}

	pStru->m[3][3] = 1;
	wprintf(L"Struct_Change2DArr \n");
}

C#代码:把二维数组拆分成两个一维数组

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct testStru7Pre
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=5)]
    public int		[]m;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct testStru7
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
    public testStru7Pre []m;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_Change2DArr(ref testStru7 pStru);

测试:

CExportDll.testStru7 stru7 = new CExportDll.testStru7();
CExportDll.Struct_Change2DArr(ref stru7);

【3】结构体中含有字符串指针

C++代码:

typedef struct _testStru9
{
	WCHAR	*pWChArr;
	CHAR	*pChArr;
	bool	IsCbool;
	BOOL	IsBOOL;
}testStru9;
EXPORTDLL_API void Struct_ChangePtr( testStru9 *pStru )
{
	if (NULL == pStru)
	{
		return;
	}

	pStru->IsBOOL = true;
	pStru->IsBOOL = TRUE;
	pStru->pWChArr = (WCHAR*)CoTaskMemAlloc(8*sizeof(WCHAR));
	pStru->pChArr = (CHAR*)CoTaskMemAlloc(8*sizeof(CHAR));

	lstrcpynW(pStru->pWChArr, L"ghj", 8);
	lstrcpynA(pStru->pChArr, "ghj", 8);

	wprintf(L"Struct_ChangePtr \n");
}

C#代码,定义成string即可,注意BOOL与bool的不同:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct testStru9
{
    [MarshalAs(UnmanagedType.LPWStr)]
    public string pWChArr;
    [MarshalAs(UnmanagedType.LPStr)]
    public string pChArr;
    [MarshalAs(UnmanagedType.U1)]
    public bool IsCbool;
    [MarshalAs(UnmanagedType.Bool)]
    public bool IsBOOL;
};
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode)]
public static extern void Struct_ChangePtr(ref testStru9 pStru);

测试:

<strong>CExportDll.testStru9 stru9 = new CExportDll.testStru9();
CExportDll.Struct_ChangePtr(ref stru9);
</strong>
时间: 2024-08-10 02:10:32

C#调用C++ 平台调用P/Invoke 结构体--含有内置数据类型的一维、二维数组、字符串指针【六】的相关文章

C#调用C++ 平台调用P/Invoke 结构体--结构体嵌套【八】

普通的结构体嵌套很简单,C#中直接定义成对应的结构体即可,这里介绍的是嵌套的结构体以指针的方式表达 [1]嵌套结构体指针 C++代码: typedef struct _testStru10Pre { int iVal; }testStru10Pre; typedef struct _testStru10 { testStru10Pre *pPre; long lVal; _testStru10() { pPre = NULL; } }testStru10; EXPORTDLL_API void

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

[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(

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

[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; pS

2015.12.13 二维数组 函数指针 结构体

先说一下指针的用途:1.访问函数,在函数内部需要改变外部传入内部的值:2.函数中需得到一个连续存储空间的首地址:3.动态分配内存,需要记录分配内存的首地址.说穿了,指针的用途就是和地址相关的. 二维数组 定义方法 ①int temp1 [2][3] = {}; 第一个中括号是“行”,第二个中括号是“列”. ②int temp2 [][3] = {1,2,3,4,5,6}; “列数”不需要明确指出. ③int temp3 [2][3] = {1,2,3,4}; 后两个元素为0. char *nam

C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com

原文:C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 本文由 arthinking 发表于315 天前 ⁄ itzhai.com原创文章 ⁄ C语言 ⁄ 评论数 3 ⁄ 被围观 1,775 views+ 指针数组: 在一个数组中,如果它的元素全部都是指针类

一维数组,二维数组,三维数组,数组与指针,结构体数组,通过改变指针类型改变访问数组的方式

 打印数组中的每个元素,打印每个元素的地址: #include <stdio.h> #include <stdlib.h> void main(void) { int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int *p = a; p < a + 10;p++)  //指针类型决定4个字节 { printf("\n%p,%d", p, *p); } getchar(); } 指针数组 #inclu

C语言一维数组、二维数组、结构体的初始化

C语言数组的初始化表示方法 一.C语言一维数组初始化: (1)在定义数组时对数组元素赋以初值.如: static int a[10]={0,1,2,3,4,5,6,7,8,9}; 经过上面的定义和初始化后,a[0]=0,a[1]=1,… ,a[9]=9. (2)初始化时可以只对一部分元素赋初值.例如: static int a[10]={0,1,2,3,4}; 定义的数组有10个元素,但只对其中前5个元素赋了初值,后5个元素初值为0. (3)将数组的元素值全部为0,可以用下面的方法:(方法一)

将传入结构体 pMtInfo 中包含的数据内容转换成 JSON 字符串返回

upu_struct.h封装了有关  pMtInfo结构体的内容,用到的部分如下图所示: 利用jansson库实现将传入结构体 pMtInfo 中包含的数据内容转换成 JSON 字符串返回 代码如下: #include <stdio.h> #include <string.h> #include "jansson.h" #include "upu_struct.h" #include "upu_proto_parse.h"

结构体指针内存——指针数组——字符串指针内存申请

前几天用的结构体,结构体内还包含有结构体指针和数组以及指向字符串的指针,发现自己对这方面的东西还很容易犯错,故现在讲其中容易出错的地方写出来,分享给大家也方便自己日后查看. typedef struct { char name[50]; char job[50]; int age; int people_id; } peopleInfo; typedef struct { bool typeAdd; bool typeDel; int length; peopleInfo *info; char