C++/CLI 托管C++之结构体封装【8】
2015-01-01 15:48
916 查看
【1】结构体作为输入输出参数
C++导出函数:
CLI封装
c#测试函数:
【2】结构体中含有内置数据类型的数组
C++导出函数:
CLI封装
C#测试函数:
【3】结构体数组作为参数、
C++导出函数
CLI封装
C#测试:
CLI类声明:
源码地址:
https://coding.net/u/aoshilangCode/p/CLI/git
C++导出函数:
typedef struct _testStru1 { int iVal; char cVal; __int64 llVal; }testStru1;
//4.1 结构体作为输入输出参数 EXPORTDLL_CLASS void Struct_Change( testStru1 *pStru ) { if (NULL == pStru) { return; } pStru->iVal = 1; pStru->cVal = 'a'; pStru->llVal = 2; wprintf(L"Struct_Change \n"); }
CLI封装
[Serializable] public ref struct test1 { Int32 iVal; SByte cVal; Int64 llVal; void UnmanagedPtr2ManagedStru(IntPtr ptr) { testStru1 *ptStru = static_cast<testStru1 *>(ptr.ToPointer()); if (NULL == ptStru) return; iVal = ptStru->iVal; cVal = ptStru->cVal; llVal= ptStru->llVal; } };
void ExportCLI::StructCls::StructChange( test1^ %pStru ) { if (pStru == nullptr) { return; } testStru1 strT; strT.iVal = pStru->iVal; strT.cVal = pStru->cVal; strT.llVal = pStru->llVal; Struct_Change(&strT); pStru->UnmanagedPtr2ManagedStru(IntPtr(&strT)); }
c#测试函数:
test1 tStru1 = new test1(); tStru1.iVal = 0; tStru1.cVal = 0; tStru1.llVal = 0; StructCls.StructChange(ref tStru1);
【2】结构体中含有内置数据类型的数组
C++导出函数:
typedef struct _testStru3 { int iValArrp[30]; WCHAR szChArr[30]; }testStru3;
//4.3 结构体中含有内置数据类型的数组 EXPORTDLL_CLASS void Struct_ChangeArr( testStru3 *pStru ) { if (NULL == pStru) { return; } pStru->iValArrp[0] = 8; lstrcpynW(pStru->szChArr, L"as", 30); wprintf(L"Struct_ChangeArr \n"); }
CLI封装
[Serializable] public ref struct test3 { array<Int32>^ iValArrp; array<Char>^ szChArr; test3() { iValArrp = gcnew array<Int32>(30); szChArr = gcnew array<WCHAR>(30); } void UnmanagedPtr2ManagedStru(IntPtr ptr) { testStru3 *ptStru = static_cast<testStru3 *>(ptr.ToPointer()); if (NULL == ptStru) return; for (int ix=0; ix<30; ix++) { iValArrp[ix] = ptStru->iValArrp[ix]; szChArr[ix] = ptStru->szChArr[ix]; } } };
void ExportCLI::StructCls::StructChangeArr( test3^ %pStru ) { if (pStru == nullptr) { return; } testStru3 strT; for (int ix=0; ix<pStru->iValArrp->Length; ix++) { strT.iValArrp[ix] = pStru->iValArrp[ix]; strT.szChArr[ix] = pStru->szChArr[ix]; } Struct_ChangeArr(&strT); pStru->UnmanagedPtr2ManagedStru(IntPtr(&strT)); }
C#测试函数:
test3 tStru3 = new test3(); tStru3.iValArrp[0] = 55; tStru3.szChArr[1] = '1'; StructCls.StructChangeArr(ref tStru3);
【3】结构体数组作为参数、
C++导出函数
typedef struct _testStru5 { int iVal; }testStru5;
//4.5 结构体数组作为参数 EXPORTDLL_CLASS void Struct_StruArr( testStru5 *pStru, int len ) { if (NULL == pStru) { return; } for ( int ix=0; ix<len; ix++) { pStru[ix].iVal = ix; } wprintf(L"Struct_StruArr \n"); }
CLI封装
[Serializable] public ref struct test5 { Int32 iVal; test5() { iVal = 5; } };
void ExportCLI::StructCls::StructStruArr( List<test5^>^ pStru ) { if (pStru == nullptr) { return; } int len = pStru->Count; testStru5 *pStruArr = new testStru5[len]; for (int ix=0; ix<len; ix++) { pStruArr[ix].iVal = pStru[ix]->iVal; } Struct_StruArr(pStruArr, len); //内存释放 if (NULL != pStruArr) { delete []pStruArr; pStruArr = NULL; } }
C#测试:
List<test5> tStru5 = new List<test5>(0); tStru5.Add(new test5()); StructCls.StructStruArr(tStru5);
CLI类声明:
/// <summary> /// 3 结构体测试类 /// </summary> public ref class StructCls { public: /// <summary> /// 3.1 结构体作为输入输出参数 /// </summary> /// <param name="pStru">结构体</param> static void StructChange(test1^ %pStru); /// <summary> /// 3.2 结构体边界对齐 /// </summary> /// <param name="pStru">结构体</param> static void StructPackN(test2^ %pStru); /// <summary> /// 3.3 结构体中含有内置数据类型的数组 /// </summary> /// <param name="pStru">结构体</param> static void StructChangeArr(test3^ %pStru); /// <summary> /// 3.4 union类型中含有结构体 /// </summary> /// <param name="pStru">结构体</param> static void StructUnion(test4^ %pStru); /// <summary> /// 3.5 结构体数组作为参数 /// </summary> /// <param name="pStru">结构体数组</param> static void StructStruArr(List<test5^>^ pStru); };
源码地址:
https://coding.net/u/aoshilangCode/p/CLI/git
相关文章推荐
- C++/CLI 托管C++之enum枚举封装【6】
- C++/CLI 托管C++之结构体封装(内存对齐#pragma pack)【9】
- C++/CLI 托管C++之字符串封装【5】
- C++/CLI 托管C++之类、属性封装【7】
- C++/CLI 托管C++之联合体union封装【10】
- C# 发中调用一个采用C++封装好的dll, 其结构体中的二维数转换
- 托管C++、C++/CLI、CLR
- 利用C++/CLI 封装Native C++ 提升.NET web game性能
- 托管与非托管间的代码级桥梁——C++/CLI
- 《转换指南: 将程序从托管扩展C++迁移到C++/CLI》译后记
- 转换指南: 将程序从托管C++扩展迁移到C++/CLI
- 转换指南: 将程序从托管扩展 C++ 迁移到 C++/CLI
- 转换指南: 将程序从托管扩展 C++ 迁移到 C++/CLI
- C#调用C++ 平台调用P/Invoke 结构体--内存对齐方式、union封装【七】
- Unity3d 载入 C++/CLI (托管模式c++) 的动态库(DLL)
- 托管c++ (CLI) String^ 到 std::string 的相互转化
- MYSQL的简单封装,学习基于托管的C++开发
- C++/CLI中,托管代码与本机代码销毁顺序
- OpenSceneGraph 笔记--C++/CLI写托管Scene类
- [CLI/C++].NET托管代码和非托管代码的相互调用