在c#中通过DllImport调用vc6编写的dll,结果出现“尝试读取或写入受保护的内存,这通常指示其他内存已损坏”的错误,抛出AccessViolationException异常。
c中函数原型为fx(LPTSTR a,card b,bool c,int d,int e,char *f short g)我在c#中写成
fx (string a,card b,bool c,int d,int e,string f,int g)
其中card在c中为结构类型,在c#中已改为类,c#中程序运行结果正确,就是出现以上异常,望高手解救!
c中函数原型为fx(LPTSTR a,card b,bool c,int d,int e,char *f short g)我在c#中写成
fx (string a,card b,bool c,int d,int e,string f,int g)
其中card在c中为结构类型,在c#中已改为类,c#中程序运行结果正确,就是出现以上异常,望高手解救!
解决方案 »
- VS2010IDE上的工具栏显示问题
- FormBorderStyle设置成了None,如何让窗体使用细边框?
- C#如何下载指定的文件
- sql数据库连接不上啊?
- 急!!AJAX 疑难问题!3天没有解决,今天最后期限了!ScriptManager.RegisterStartupScript产生确认取消对话框,确认后,如果继续执行后
- Response.Redirect("a.aspx", false) (为false时参数不传递,为什么?)
- (WinForm) MDI 窗体背景图闪烁的问题:
- 编程经验不足,请高手帮忙将两程序整合
- 高手救命!!如何获得当前激活的pdf文档的文件名
- 大家进来纠正一下错误,谢谢
- 用过.NET 2005的请进。
- 关于ASCII字节数组转换为Unicode字符串
函数声明
public class card
{
public char[] name;
public char[] sex;
public char[] birthday;
public char[] address;
} [DllImport("infodll.dll")]
public static extern int fx(string imagefile_name,card idcard,bool Result,int net,int Content,string HeadImg,int Type);private void button1_Click(object sender, EventArgs e)
{
String imgfile = "img.jpg";
String imghead = "head.jpg";
card idcard = new card();
RecogIdcardExALL(imgfile,idcard,true,0,17,imghead,1);
}调用的函数fx原型为fx(LPTSTR a,card b,bool c,int d,int e,char *f short g)
public struct card
{
[MarshalAs( UnmanagedType.LPTStr )]
public StringBuilder name;
[MarshalAs( UnmanagedType.LPTStr )]
public StringBuilder sex;
[MarshalAs( UnmanagedType.LPTStr )]
public StringBuilder birthday;
[MarshalAs( UnmanagedType.LPTStr )]
public StringBuilder address;
}[DllImport("infodll.dll")]
public static extern int fx(string imagefile_name,
[Out]ref card idcard,bool Result,int net,int Content,string HeadImg,int Type);//Call
String imgfile = "img.jpg";
String imghead = "head.jpg";
card idcard = new card();
idcard.name = new StringBuilder( 256 );//Init buffer size with 256
idcard.sex = new StringBuilder( 256 );//Init buffer size with 256
idcard.birthday = new StringBuilder( 256 );//Init buffer size with 256
idcard.address = new StringBuilder( 256 );//Init buffer size with 256RecogIdcardExALL(imgfile,idcard,true,0,17,imghead,1);
public class card
{
public char[] name;
public char[] sex;
public char[] birthday;
public char[] address;
}
把c的定义贴出来
public class card
{
[MarshalAs(UnmanagedType.LPArray,
ArraySubType=UnmanagedType.ByValTStr, SizeConst=40)]
public string name;
[MarshalAs(UnmanagedType.LPArray,
ArraySubType=UnmanagedType.ByValTStr, SizeConst=10)]
public string sex;
[MarshalAs(UnmanagedType.LPArray,
ArraySubType=UnmanagedType.ByValTStr, SizeConst=50)]
public string birthday;
[MarshalAs(UnmanagedType.LPArray,
ArraySubType=UnmanagedType.ByValTStr, SizeConst=200)]
public string address;
}
[DllImport("infodll.dll")]
public static extern int fx(string imagefile_name,ref //这里加ref
card idcard,bool Result,int net,int Content,string HeadImg,int Type);
....public class card
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)]
public string name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=10)]
public string sex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
public string birthday;
[MarshalAs(UnmanagedType.ByValTStr,, SizeConst=200)]
public string address;
}
[StructLayout(LayoutKind.Sequential)]
public class card
{
[MarshalAs(UnmanagedType.LPArray, SizeConst=40)]
public byte[] name;
[MarshalAs(UnmanagedType.LPArray, SizeConst=10)]
public byte[] sex;
[MarshalAs(UnmanagedType.LPArray, SizeConst=50)]
public byte[] birthday;
[MarshalAs(UnmanagedType.LPArray, SizeConst=200)]
public byte[] address;
} [DllImport("infodll.dll")]
public static extern int fx(string imagefile_name,ref card idcard,
bool Result,int net,int Content,string HeadImg,int Type);//Init card as follows
card idcard = new card();
idcard.name = new byte[40];
idcard.sex = new byte[10];
idcard.birthday = new byte[50];
idcard.address = new byte[200];
Message: 运行库遇到了错误。此错误的地址为 0x79ef067e,在线程 0xd58 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。
”郁闷死!!
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi //这里看具体的c++要求)]
public class card
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)] //如果设置不对,结果肯定不对,且会破坏堆栈,
public string name;}
c的struct最好用c#的struct来port.这样容易一些。因为c#的struct可以显示内存布局。只要你布局得和c的内存布局一模一样就肯定能过。
非常感谢hdt(倦怠)、Knight94(愚翁)、mooniscrazy(月色疯狂),有了你们的大力帮助才得以搞定,希望以后多多指教!