读卡机的dll中有个函数
格 式:OMR_Serial(LPTSTR serial)
//功 能: 取OMR产品序列号
//参 数: serial:指向序列号字符串的指针。
//返回值: 无
//注 释: OMR产品出厂时,都有一唯一序列号,序列号为六位数字字符串,
//如:"980101";这意思是函数没有返回值,序列号是保存在参数中了。
我的C#程序:
class omrclass
{
[DllImport("omrvfapi.dll", EntryPoint = "OMR_Serial")]
public static extern int OMR_Serial(out String serial);
}
然后在另一个文件中调用此托管函数:
String str;
int ren=omrclass.OMR_Serial(out str);
label1.Text = str;运行时提示:
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
异常:AccessViolationException请大家解答!
格 式:OMR_Serial(LPTSTR serial)
//功 能: 取OMR产品序列号
//参 数: serial:指向序列号字符串的指针。
//返回值: 无
//注 释: OMR产品出厂时,都有一唯一序列号,序列号为六位数字字符串,
//如:"980101";这意思是函数没有返回值,序列号是保存在参数中了。
我的C#程序:
class omrclass
{
[DllImport("omrvfapi.dll", EntryPoint = "OMR_Serial")]
public static extern int OMR_Serial(out String serial);
}
然后在另一个文件中调用此托管函数:
String str;
int ren=omrclass.OMR_Serial(out str);
label1.Text = str;运行时提示:
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
异常:AccessViolationException请大家解答!
String str;
omrclass.OMR_Serial(out str);
label1.Text = str;
public static extern int OMR_Serial(out String serial); //----------------这个修改成 public static extern int OMR_Serial(StringBuilder serial); 或者 public static extern int OMR_Serial(IntPtr serial); 调用时采用byte[] serial = new byte[6];
IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement (serial,0);
{
Byte[] str=new Byte[6];
IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement(str, 0);
omrclass.OMR_Serial(p);
label2.Text = Encoding.Default.GetString(str);
}经2楼指点,已经正确获取机器序列号,结题给分。thanks.