gpt4 book ai didi

c# - Dll 使用数组将 C++ 导入到 C#

转载 作者:太空狗 更新时间:2023-10-29 20:15:10 26 4
gpt4 key购买 nike

我接到了将一些库例程移植到 C# 以便我们的其他应用程序开发人员可以访问它的任务,但我不知道如何声明变量以便它们正确进入例程。

问题是,当我使用 C++ 代码逐步读取输入时,我得到了所有偏斜的值

尝试使用:

double[] Par = { 8, 16, 8, 0.61, 0.00635, ... }; // 29 variables
double[] Inlet = { 22.18, 43.31, 1.13, 2.81, 0.43 }; // 5 variables
double[] Outlet = { 0, 0, 0, 0, 0, 0 }; // placeholder for 6 variables
SteadyFor(ref Par, ref Inlet, ref Outlet, FileIn, FileOut);

DLL 导入

[DllImport(MODELAPP, EntryPoint = "SteadyFor", ExactSpelling = false)]
public static extern int SteadyFor(
ref double[] par, ref double[] inlet, ref double[] outlet,
[MarshalAs(UnmanagedType.LPStr)] string input,
[MarshalAs(UnmanagedType.LPStr)] string output);

C++ 文件:

extern "C" int SteadyFor(double Par[], double Inlet[], double Outlet[], char* FileIn, char* FileOut)
{
int n = (int)Par[0]; // Actual Reading: [0]
int nt = (int)Par[1]; // Actual Reading: [0]
int pass = (int)Par[2]; // Actual Reading: [0]
double l = Par[3]; // Actual Reading: [2.9581223236733198e+174]
double rTube = Par[4]; // Actual Reading: [2.121995790965e-314#DEN]
double tTube = Par[5]; // Actual Reading: [5.432309224896e-312#DEN]
double pl = Par[6]; // Actual Reading: [1.0253217546256438e-267]
double pt = Par[7]; // Actual Reading: [4.60629e-308]
// ...
}

显然,我得到的值都是错误的——几乎就像未初始化的内存。

有人可以告诉我我做错了什么以及如何解决吗?

问候,

~乔

最佳答案

删除声明中的“ref”关键字,它们是不正确的。 C++ 代码不会与 AV 崩溃有点神秘。

字符串上的 [MarshalAs] 属性是不必要的。

关于c# - Dll 使用数组将 C++ 导入到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3087016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com