2014-03-03 49 views
0

有一個外部的dll方法:傳遞正確的參數,以託管的DLL方法在C#

public unsafe static void BV([In] int* key, [In] int* m, [In] int* n, [In] double* a, [In] double* b, [In] double* bl, [In] double* bu, [Out] double* x, [Out] double* w, [In] double* act, [In] double* zz, [Out] double* istate, [Out] int* loopa) 

我如何在C#中傳遞參數是要求* int和*雙和如何應對,出?

在示例代碼我不能傳遞參數的方法需要他們。什麼是錯的?

 int key = 0, n = 2, m = 3; 
     double[] a = { 1.0, 2.0, 3.0, 4.0, 5.0 }; 
     double[] b = { 10.0, 20.0, 30.0 }; 
     double[] bl = { 0.0, 1.0 }; 
     double[] bu = { 1.0, 2.0 }; 
     double[] x = new double[n]; 
     double[] w = new double[n]; 
     double[] act = new double[m * (Math.Min(m, n) + 2)]; 
     double[] zz = new double[m]; 
     double[] istate = new double[n + 1]; 
     int loopA = 0; 
     bvlsFortran.BV(key,m,n,a,b,bl,bu, x,w,act,zz,istate,loopA); //bvlsFortran is dll file 
     Console.WriteLine(loopA); 

enter image description here

我與

bvlsFortran.BV(key,m,n,a,b,bl,bu, out x,out w,act,zz,out istate, out loopA); 

bvlsFortran.BV(ref key,ref m,ref n,ref a,ref b,bl,bu, out x,out w,act,zz,out istate, out loopA); 

嘗試,但他們似乎並不管用 我在做什麼錯

回答

1

這爲我工作(至少它編譯):

int key = 100; 
    int* keyPointer = (int*)&key; 
    bvlsFortran.BV(keyPointer); 

我不傳遞其他參數,你可以自己做這件事

要通過雙[]你應該使用fixed關鍵字:

double[] a = {1.0, 2.0, 3.0, 4.0, 5.0}; 
    fixed (double* pt = a) 
    { 
     bvlsFortran.BV(pt); 
    } 

[In][Out]屬性:它的東西從refout不同。如果你願意,你可以在msdn上閱讀更多關於OutAttributeInAttribute的文章。

而且多了一個選擇,你可以嘗試,是寫C++/CLI包裝你非託管的DLL,並用它從C#,無需使用指針和其他不安全的代碼。

+0

是否有可能利用它在安全的前提下,也許使用Marshal? – cMinor

+0

@cMinor唯一的辦法避免使用不安全的代碼在C#中我知道,使用C++/CLI包裝器 – Uriil

+0

你如何應用此技術來加倍*加倍[]?如雙* AP =(雙*)&a ...;是行不通的 – cMinor

相關問題