以前用VC写程序,调用这个dll里的函数很简单。
现在要用C#来调用这个dll里的函数搞了好久还没搞定,求各位大大帮帮手
a.dll中的函数声明
void func(unsigned char num,unsigned char *x,unsigned short int *y,unsigned short int *t)num,*x,*y是输出参数,*t是输出参数,x,y,t是一维数组我写的C#声明函数:
[DllImport(("a.dll"), EntryPoint = "find_coef", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]extern static void func(byte num, ref byte x, ref ushort y, out ushort t);调用的代码:
        private void button1_Click(object sender, EventArgs e)
        {
            byte num = 3;
            byte[] x = new byte[3];
            x[0] = 50;
            x[1] = 100;
            x[2] = 150;
            ushort[] y= new ushort[5];
            y[0] = 5555;
            y[0] = 7777;
            y[0] = 9999;
            ushort[] t = new ushort[176];
            func(num, ref x[0], ref y[0], out t[0]);
        }编译可以通过,但运行时一直出错:无法加载“a.dll”:找不到指定的模块,指定dll路径也出相同的错误。

解决方案 »

  1.   

    你干嘛不在工程里直接引用啊还有 a.dll 有没有注册呢
      

  2.   

    把动态库拷到运行目录下了吧?那应该可以加载a.dll的呀。再一个:extern static void func(byte num, ref byte x, ref ushort y, out ushort t);这个东西不一定行。char类型的指针在c#中用字符串即可。如:
    extern static void func(String num, ref String x, ref int y, out int t);
    还有这个东西没个准,多试几次参数的类型应该可以的。 
      

  3.   

    添加应用和注册dll都会出错:
    未能添加对"a.dll"的引用,请确保此文件可访问并且是一个有效的程序集或是COM组件。LoadLibrary("a.dll")失败-找不到指定的模块。
      

  4.   

    dll文件已经拷到运行目录下了,sysytem32下面也拷贝了一份,在程序中指定dll的绝对路径也不行,不像是路径问题。之前用VC编程时用过,很正常,dll文件应该是没问题的。
      

  5.   

    改成这个试试:
    依然C++里面后三个参数是数组,你在C#里至少也是个数组吧,最差也应该是个IntPtr才行的。
    先试试这个吧。
    extern static void func(byte num, StringBuilder x, StringBuilder y, StringBuilder t);
      

  6.   


    用这个试试
    extern static void func(byte num, StringBuilder x, StringBuilder y,IntPtr t);
      

  7.   

    还是不行,算了,不搞了。
    用unsafe搞完,明天回家过年了,呵呵多谢各位了,新年快乐!