I've been given the assignment to port some library routines to C# so our other application developers can access it, but I don't know how to declare the variables so they come into the routine correctly.
我已经获得了将一些库例程移植到C#的任务,因此我们的其他应用程序开发人员可以访问它,但我不知道如何声明变量以便它们正确地进入例程。
The problem is, when I step through reading the input in with the C++ code, I get all skewed values
问题是,当我逐步阅读C ++代码中的输入时,我得到了所有偏斜的值
Attempt to use:
尝试使用:
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);
The DLL Import
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);
The C++ file:
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]
// ...
}
Obviously, the values I am getting are all wrong - almost like non-initialized memory.
显然,我得到的值都是错误的 - 几乎就像非初始化的内存。
Could someone tell me what I'm doing wrong and how to fix it?
有人能告诉我我做错了什么以及如何解决它?
Regards,
~Joe
2 个解决方案
#1
7
Drop the "ref" keywords in the declaration, they are not correct. That the C++ code doesn't crash with an AV is a bit mysterious.
删除声明中的“ref”关键字,它们不正确。 C ++代码没有与AV崩溃有点神秘。
The [MarshalAs] attribute on the strings is unnecessary.
字符串上的[MarshalAs]属性是不必要的。
#2
1
Basically you need to declare a marshalling attribute. In your case, it would be a MarshalAs(UnmanagedType.LPArray) attribute.
基本上你需要声明一个编组属性。在您的情况下,它将是MarshalAs(UnmanagedType.LPArray)属性。
看这里:http://msdn.microsoft.com/en-us/library/z6cfh6e6%28VS.80%29.aspx#cpcondefaultmarshalingforarraysanchor3
#1
7
Drop the "ref" keywords in the declaration, they are not correct. That the C++ code doesn't crash with an AV is a bit mysterious.
删除声明中的“ref”关键字,它们不正确。 C ++代码没有与AV崩溃有点神秘。
The [MarshalAs] attribute on the strings is unnecessary.
字符串上的[MarshalAs]属性是不必要的。
#2
1
Basically you need to declare a marshalling attribute. In your case, it would be a MarshalAs(UnmanagedType.LPArray) attribute.
基本上你需要声明一个编组属性。在您的情况下,它将是MarshalAs(UnmanagedType.LPArray)属性。
看这里:http://msdn.microsoft.com/en-us/library/z6cfh6e6%28VS.80%29.aspx#cpcondefaultmarshalingforarraysanchor3