原C中的函数声明为:
extern "C" __declspec(dllexport) bool  _stdcall TestCom(unsigned char* mode,unsigned char* block );
extern "C" __declspec(dllexport) short int _stdcall  WriteDefault(int closecard, unsigned char data[16], unsigned char cardno[4]);我在C#中调用用了如下方法:
[DllImport("card.dll")]
public static extern byte TestCom(ref byte mode, ref byte block);
[DllImport("card.dll")]
public static extern short WriteDefault(byte CloseCard, ref byte data, ref byte cardno);请问这里的指针和数组是不是这样用ref声明就可以了?还有就是如果我在C#中用下面的数组声明,和上面的调用方法有什么不同?
[DllImport("card.dll")]
public static extern short WriteDefault(byte CloseCard, ref byte[] data, ref byte[] cardno);

解决方案 »

  1.   

    ref byte data和ref byte[] data 不一样的,第一个是byte类型,第二个是数组,zhe不一样的
      

  2.   

    [DllImport("card.dll")]
    public static extern byte TestCom(ref byte mode, ref byte block);
    [DllImport("card.dll")]
    public static extern short WriteDefault(byte CloseCard, ref byte data, ref byte cardno);
    应该得不到数据或者会出错。
    [DllImport("card.dll")]
    public static extern short WriteDefault(byte CloseCard, ref byte[] data, ref byte[] cardno);
    这个编译会出错的,从托管到非托管需要封装数据,不能直接传过去的。C++中char*,C#用string或StringBuilder,如下(编译通过,试试能不能得到你的数据吧):
    class test
        {
            [DllImport("card.dll")]
            public static extern bool TestCom(string mode, string block);
            [DllImport("card.dll")]
            public static extern short WriteDefault(int CloseCard, [In, Out, MarshalAs(UnmanagedType.LPArray)]byte[] data, [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] cardno);
        
            public test()
            {
                byte[] data = new byte[200];
                byte[] cardno = new byte[100];
                string mode = string.Empty;
                string block = string.Empty;
                TestCom(mode, block);
                WriteDefault(1, data, cardno);
            }
        }