繁体   English   中英

将正确的参数传递给c#中的托管dll方法

[英]pass correct parameters to managed dll method in c#

有一个外部DLL方法:

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和* double以及如何处理in,out?

在示例代码中,我无法传递参数,因为方法需要它们。 这有什么不对?

        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);

在此输入图像描述

我正在努力

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 ); 

但他们似乎没有工作我做错了什么

我认为你应该使用IntPtr结构将整数和双指针传递给DLL方法。

http://msdn.microsoft.com/en-us/library/system.intptr%28v=vs.110%29.aspx

还看看这张桌子。 它特定于从C#调用COM / DCOM组件的情况,但它也会给你一个想法。

http://msdn.microsoft.com/en-us/library/sak564ww%28v=vs.110%29.aspx

这对我有用(至少它编译):

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

我没有传递其他参数,你可以自己做

要传递double [],您应该使用fixed keyword:

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

[In][Out]属性:它与refout有所不同。 如果您愿意,可以在msdn上阅读有关OutAttributeInAttribute的更多信息。

您可以尝试的另一个选项是为您的非托管dll编写C ++ / CLI包装器,并从c#中使用它,而无需使用指针和其他不安全的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM