本人需要用c#调用一个C写的dll里的一个函数:
int ReadTrack(unsigned char *track1, unsigned long *len1, unsigned char *track2, unsigned long *len2, unsigned char *track3, unsigned long *len3)
函数名:ReadTrack
功能: 读磁条数据
参数:[out] track1 磁道数据 [out] len1 磁道数据长度
[out] track2 磁道数据 [out] len2 磁道数据长度
[out] track3 磁道数据 [out] len3 磁道数据长度
返回值:1 成功
其余 参见错误码(整理错误码列表)
本人写的调用代码如下:
[DllImport(@"C:\FORTUNE_SP\interface\FORTUNE_IDC.dll")] public extern static int FD_IDC_ReadTrack(ref byte track1, uint len1, ref byte track2, uint len2, ref byte track3, uint len3);
byte tck1 = new byte(); byte tck2 = new byte(); byte tck3 = new byte();
ret = FD_IDC_ReadTrack(ref tck1, l1, ref tck2, l2, ref tck3, l3); if (ret == 1) { Console.WriteLine("读磁条成功!\n"); } else { Console.WriteLine("读磁条失败!\n"); }
出现如下错误:
同样试过:
[DllImport(@"C:\FORTUNE_SP\interface\FORTUNE_IDC.dll")] public extern static int FD_IDC_ReadTrack(byte[] track1, uint len1, byte[] track2, uint len2, byte[] track3, uint len3); static void Main(string[] args) { byte[] tck1 = new byte[512]; byte[] tck2 = new byte[512]; byte[] tck3 = new byte[512]; /* StringBuilder tck1 = new StringBuilder(); StringBuilder tck2 = new StringBuilder(); StringBuilder tck3 = new StringBuilder(); */ uint l1 = new uint(); uint l2 = new uint(); uint l3 = new uint();
也是不行,同样的错误
劳烦各位高手帮本人看看哪里错了
解决方案
40
参数声明为这种形式:
IntPtr track1, out int len1
调用过后
byte[] trackdata1 = new byte[len1];
Marshal.Copy(track1, trackdata1, 0, len1);
IntPtr track1, out int len1
调用过后
byte[] trackdata1 = new byte[len1];
Marshal.Copy(track1, trackdata1, 0, len1);
30
现在人在写程序时都是自顾自吗?都不考虑共享?
如是这样,何苦写成 dll?
如是这样,何苦写成 dll?
30
调用函数应该是这样的,要传入的是数组的第一个元素的地址
ret = FD_IDC_ReadTrack(ref tck1[0], l1, ref tck2[0], l2, ref tck3[0], l3);
ret = FD_IDC_ReadTrack(ref tck1[0], l1, ref tck2[0], l2, ref tck3[0], l3);