我已經被賦予將一些庫例程移植到C#的任務,所以我們的其他應用程序開發人員可以訪問它,但我不知道如何聲明變量以便它們進入例程正確。Dll將數組導入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]
// ...
}
顯然,我得到的值都是錯的 - 就像非初始化的內存一樣。
有人能告訴我我做錯了什麼,以及如何解決它?
問候,
〜喬
這很有效,漢斯。現在,爲什麼? C++引入了指針,而C#正在傳遞......什麼?指針? – jp2code 2010-06-21 18:38:18
數組的默認編組傳遞一個指向其第一個元素的指針。就像C++一樣。數組永遠不會被超值,這將是非常昂貴的,並註定要在大型陣列上失敗。當你使用「ref」時,它會傳遞一個指向該指針的指針。 – 2010-06-21 18:41:57