原始的 VB.net,运行良好:
Declare Function HolderName Lib "myCard.dll" (ByVal buf As String) As Integer
Declare Function Photo Lib "myCard.dll" (ByRef photo As Byte) As Integer
...
buff = Space(200) : res = HolderName(buff)
ShowMsg("HolderName():" & IIf(res = 0, "OK:" & Trim(buff), "FAIL"))
photobuf = New Byte(4096) {}
res = Photo(photobuf(0))
ShowMsg("Photo():" & IIf(res = 0, "OK", "FAIL"))
If res = 0 Then
Dim ms As New MemoryStream(photobuf)
picImage.Image = Image.FromStream(ms)
End If
转换后的代码现在在 C# 中(使用http://converter.telerik.com/)
using System.Runtime.InteropServices;
...
[DllImport("myCard.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int HolderName(String dBuff);
[DllImport("myCard.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int Photo(ref byte photo);
//...
buff = new String(' ', 200);
res = HolderName(buff);
// buff remains UNALTERED!
ShowMsg("HolderName():" + (res == 0 ? "OK:" + Strings.Trim(buff) : "FAIL"));
photobuf = new byte[4096];
res = Photo(ref photobuf[0]);
ShowMsg("Photo():" + (res == 0 ? "OK" : "FAIL"));
// photobuf successfully receives the data bytes from Photo function
if (res == 0)
{
MemoryStream ms = new MemoryStream(photobuf);
picImage.Image = Image.FromStream(ms);
}
buff
即使该HolderName
函数实际上返回了一些值(使用 USB 监视器观察),问题仍然没有改变。为什么会发生这种情况,我该如何解决?