日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# C++ 互操作:C++向C#输出不定长数组或指针的实现

發布時間:2024/3/13 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# C++ 互操作:C++向C#输出不定长数组或指针的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這種需求算有點難度吧,在這里貼出一個實現以供參考:

C++部分:頭文件

// 下列 ifdef 塊是創建使從 DLL 導出更簡單的 // 宏的標準方法。此 DLL 中的所有文件都是用命令行上定義的 CALCULATE_EXPORTS // 符號編譯的。在使用此 DLL 的 // 任何項目上不應定義此符號。這樣,源文件中包含此文件的任何其他項目都會將 // CALCULATE_API 函數視為是從 DLL 導入的,而此 DLL 則將用此宏定義的 // 符號視為是被導出的。#ifndef _Calculate_ #define _Calculate_#include <stdlib.h>#ifdef CALCULATE_EXPORTS #define CALCULATE_API extern "C" __declspec(dllexport) #else #define CALCULATE_API __declspec(dllimport) #endiftypedef intptr_t IntPtr;CALCULATE_API int MultiGray(double st_high,double st_low,IntPtr* P_inflect_handle,int** P_inflect,int* P_inflect_Len,double set_max,double set_min,double response_setTime,IntPtr* P_mid_handle,int** P_mid,int* P_mid_Len,IntPtr* flag_handle,int** flag,int* flag_Len,int jump_edge,int tcon_OD_up,int tcon_OD_down,IntPtr* P_left_handle,int** P_left,int* P_left_Len,IntPtr* P_right_handle,int** P_right,int* P_right_Len,int freq,double response ); CALCULATE_API bool ReleaseObject(IntPtr handle);#endif

C++部分:函數實現

// Calculate.cpp : 定義 DLL 的導出函數。 //#include "pch.h" #include "framework.h" #include "Calculate.h" #include <vector> using namespace std;CALCULATE_API int MultiGray(double st_high,double st_low,IntPtr* P_inflect_handle,int** P_inflect,int* P_inflect_Len,double set_max,double set_min,double response_setTime,IntPtr* P_mid_handle,int** P_mid,int* P_mid_Len,IntPtr* flag_handle,int** flag,int* flag_Len,int jump_edge,int tcon_OD_up,int tcon_OD_down,IntPtr* P_left_handle,int** P_left,int* P_left_Len,IntPtr* P_right_handle,int** P_right,int* P_right_Len,int freq,double response ) {int count = 100000;auto array1 = new std::vector<int>();for (int i = 0; i < count; i++){array1->push_back(i);}*P_inflect_handle = reinterpret_cast<IntPtr>(array1);*P_inflect = array1->data();*P_inflect_Len = count;auto array2 = new std::vector<int>();for (int i = 0; i < count; i++){array2->push_back(i);}*P_mid_handle = reinterpret_cast<IntPtr>(array2);*P_mid = array2->data();*P_mid_Len = count;auto array3 = new std::vector<int>();for (int i = 0; i < count; i++){array3->push_back(i);}*flag_handle = reinterpret_cast<IntPtr>(array3);*flag = array3->data();*flag_Len = count;auto array4 = new std::vector<int>();for (int i = 0; i < count; i++){array4->push_back(i);}*P_left_handle = reinterpret_cast<IntPtr>(array4);*P_left = array4->data();*P_left_Len = count;auto array5 = new std::vector<int>();for (int i = 0; i < count; i++){array5->push_back(i);}*P_right_handle = reinterpret_cast<IntPtr>(array5);*P_right = array5->data();*P_right_Len = count;return 0; }CALCULATE_API bool ReleaseObject(IntPtr handle) {std::vector<int>* object = reinterpret_cast<std::vector<int>*>(handle);delete object;return true; }

C# 部分:

public class SafeHandle : SafeHandleZeroOrMinusOneIsInvalid{public SafeHandle(): base(true){}protected override bool ReleaseHandle(){return ReleaseObject(handle);}[DllImport("Calculate.dll", CallingConvention = CallingConvention.Cdecl)]static unsafe extern bool ReleaseObject(IntPtr handle);}public class Performer{[DllImport("Calculate.dll", EntryPoint = "MultiGray", CallingConvention = CallingConvention.Cdecl)]unsafe extern static int MultiGray(double st_high,double st_low,out SafeHandle handle1,out int* P_inflect,out int P_inflect_Len,double set_max,double set_min,double response_setTime,out SafeHandle handle2,out int* P_mid,out int P_mid_Len,out SafeHandle handle3,out int* flag,out int flag_Len,int jump_edge,int tcon_OD_up,int tcon_OD_down,out SafeHandle handle4,out int* P_left,out int P_left_Len,out SafeHandle handle5,out int* P_right,out int P_right_Len,int freq,double response);public unsafe ReturnData Excute(double st_high,double st_low,out SafeHandle handle1,out int* P_inflect,out int P_inflect_Len,double set_max,double set_min,double response_setTime,out SafeHandle handle2,out int* P_mid,out int P_mid_Len,out SafeHandle handle3,out int* flag,out int flag_Len,int jump_edge,int tcon_OD_up,int tcon_OD_down,out SafeHandle handle4,out int* P_left,out int P_left_Len,out SafeHandle handle5,out int* P_right,out int P_right_Len,int freq,double response){int result = MultiGray(st_high,st_low,out handle1,out P_inflect,out P_inflect_Len,set_max,set_min,response_setTime,out handle2,out P_mid,out P_mid_Len,out handle3,out flag,out flag_Len,jump_edge,tcon_OD_up,tcon_OD_down,out handle4,out P_left,out P_left_Len,out handle5,out P_right,out P_right_Len,freq,response);ReturnData data = new ReturnData();data.flag_Len = flag_Len;data.flag = new int[flag_Len];for(int i=0;i<flag_Len;i++){data.flag[i] = flag[i];}data.freq = freq;data.jump_edge = jump_edge;data.P_inflect_Len = P_inflect_Len;data.P_inflect = new int[P_inflect_Len];for(int i=0;i<P_inflect_Len;i++){data.P_inflect[i] = P_inflect[i];}data.P_left_Len = P_left_Len;data.P_left = new int[P_left_Len];for(int i=0;i<P_left_Len;i++){data.P_left[i] = P_left[i];}data.P_mid_Len = P_mid_Len;data.P_mid = new int[P_mid_Len];for(int i=0;i<P_mid_Len;i++){data.P_mid[i] = P_mid[i];}data.P_right_Len = P_right_Len;data.P_right = new int[P_right_Len];for(int i=0;i<P_right_Len;i++){data.P_right[i] = P_right[i];}data.response = response;data.response_setTime = response_setTime;data.set_max = set_max;data.set_min = set_min;data.st_high = st_high;data.st_low = st_low;data.tcon_OD_down = tcon_OD_down;data.tcon_OD_up = tcon_OD_up;handle1.Dispose();handle2.Dispose();handle3.Dispose();handle4.Dispose();handle5.Dispose();return data;}}public class ReturnData{public double st_high;public double st_low;public int[] P_inflect;public int P_inflect_Len;public double set_max;public double set_min;public double response_setTime;public int[] P_mid;public int P_mid_Len;public int[] flag;public int flag_Len;public int jump_edge;public int tcon_OD_up;public int tcon_OD_down;public int[] P_left;public int P_left_Len;public int[] P_right;public int P_right_Len;public int freq;public double response;}

總結

以上是生活随笔為你收集整理的C# C++ 互操作:C++向C#输出不定长数组或指针的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。