求助:C#调用DLL提示尝试读取或写入受保护的内存。这通常指示其他内存已损坏
C调用:// Id Card User Info
typedef struct tagIdCardUserInfo {
char *pszName;// 姓名
char *pszSex;// 性别
char *pszNation;// 民族
char *pszBirth;// 出生日期
char *pszAddress;// 地址
char *pszId;// 身份证号
char *pszIssue;// 发证机关
char *pszExpireFrom;// 有效期起始日期
char *pszExpireTo;// 有效期截止日期
unsigned long nPhotoLen;// 照片长度
unsigned char *pbPhoto;// 照片(BMP文件格式)
}IDCARDUSERINFO,*PIDCARDUSERINFO;
// Read Id Card User Info
unsigned long __stdcall ReadIdCardUserInfo(
PIDCARDUSERINFO pUserInfo);
改成C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace BaseObject
{
public class IdCardUserInfo
{
#region 构造函数
private char _pszName;
/// <summary>
/// 姓名
/// </summary>
public char PszName
{
set { _pszName = value; }
get { return _pszName; }
}
private char _pszSex;
/// <summary>
/// 性别
/// </summary>
public char PszSex
{
set { _pszSex = value; }
get { return _pszSex; }
}
private char _pszNation;
/// <summary>
/// 民族
/// </summary>
public char PszNation
{
set { _pszNation = value; }
get { return _pszNation; }
}
private char _pszBirth;
/// <summary>
/// 出生日期
/// </summary>
public char PszBirth
{
set { _pszBirth = value; }
get { return _pszBirth; }
}
private char _pszAddress;
/// <summary>
/// 地址
/// </summary>
public char PszAddress
{
set { _pszAddress = value; }
get { return _pszAddress; }
}
private char _pszId;
/// <summary>
/// 身份证号
/// </summary>
public char PszId
{
set { _pszId = value; }
get { return _pszId; }
}
private char _pszIssue;
/// <summary>
/// 发证机关
/// </summary>
public char PszIssue
{
set { _pszIssue = value; }
get { return _pszIssue; }
}
private char _pszExpireFrom;
/// <summary>
/// 有效期起始日期
/// </summary>
public char PszExpireFrom
{
set { _pszExpireFrom = value; }
get { return _pszExpireFrom; }
}
private char _pszExpireTo;
/// <summary>
/// 有效期截止日期
/// </summary>
public char PszExpireTo
{
set { _pszExpireTo = value; }
get { return _pszExpireTo; }
}
private long _nPhotoLen;
/// <summary>
/// 照片长度
/// </summary>
public long nPhotoLen
{
set { _nPhotoLen = value; }
get { return _nPhotoLen; }
}
private char _pbPhoto;
/// <summary>
/// 照片(BMP文件格式)
/// </summary>
public char pbPhoto
{
set { _pbPhoto = value; }
get { return _pbPhoto; }
}
#endregion
}
}
[DllImport("McIdCard.dll", CharSet = CharSet.Ansi)]
public static extern uInt32 ReadIdCardUserInfo(IdCardUserInfo pUserInfo);
public string GetIdCardUserInfo(dCardUserInfo pUserInfo)
{
try
{
uInt32 nResult;
nResult = ReadIdCardUserInfo(pUserInfo);
switch (nResult.ToString())
{
case "RESULT_SUCCESS":
FreeIdCardUserInfo(pUserInfo);
return "SUCCESS";
break;
case "RESULT_TIMEOUT":
return "读二代身份证超时";
break;
case "RESULT_OPEN_PORT_FAILED":
return "打开串口失败";
break;
case "RESULT_NO_MEMORY":
return "系统内存不足";
break;
default:
return "读二代身份证失败";
break;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
提示尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
求助 --------------------编程问答-------------------- 好长 ... --------------------编程问答-------------------- 我将CHAR类型用stringbuild类型替换也不行
求助啊 急 --------------------编程问答-------------------- 你这用的是类啊,你改用struct试过吗 --------------------编程问答-------------------- 这不是内存重用问题么?你上网搜下很多类似的,你好好看看自己的代码有没有内存重用的问题。 --------------------编程问答-------------------- 貌似在C#调用时要声明为结构体,网上有挺多这方面的例子! --------------------编程问答-------------------- 在你的class上面加上[StructLayout(LayoutKind.Sequential)]试一下 --------------------编程问答-------------------- 可以建立一个save函数进行保护呢,你可尝试下,呵呵
void save()
{
*FILE;
fp=fopen(“file.txt”,“r”);
}
自己看看往下咋么写了 --------------------编程问答-------------------- 在你的class上面加上[StructLayout(LayoutKind.Sequential)]试一下
不报尝试读取或写入受保护的内存了
但是返回值都是空的 --------------------编程问答-------------------- 一般是访问越界。 --------------------编程问答-------------------- 最新代码是这样的,还是不行,求助 急
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public class IdCardOperate
{
#region 申明DLL函数
[DllImport("McIdCard.dll", CharSet = CharSet.Ansi)]
public static extern UInt32 ReadIdCardUserInfo(ref idinfo pUserInfo);
#endregion
public string GetIdCardUserInfo(ref IdCardUserInfo pUserInfo)
{
string msg = "";
try
{
UInt32 nResult;
idinfo idinfo = new idinfo();
nResult = ReadIdCardUserInfo(ref idinfo);
return msg;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
[StructLayout(LayoutKind.Sequential)]
public class idinfo
{
public string pszName;
public string pszSex;
public string pszNation;
public string pszBirth;
public string pszAddress;
public string pszId;
public string pszIssue;
public string pszExpireFrom;
public string pszExpireTo;
public UInt32 nPhotoLen;
public UInt32 pbPhoto;
}
}
--------------------编程问答-------------------- 改成这样试一下,那个32的值你自己修改下,指字符串的长度
[StructLayout(LayoutKind.Sequential)]
public class idinfo
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszSex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszNation;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszBirth;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszAddress;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszIssue;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszExpireFrom;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string pszExpireTo;
public UInt32 nPhotoLen;
public UInt32 pbPhoto;
} --------------------编程问答-------------------- 还是不行啊
我用VB做了个,到是可以执行的 :
Private Declare Function ReadIdCardUserInfo Lib "McIdCard.DLL" (ByRef carduserinfo As IdcardUserInfo) As Long
Private Type IdcardUserInfo
pszName As String * 255
pszSex As String * 255
pszNation As String * 255
pszBirth As String * 255
pszAddress As String * 255
pszId As String * 255
pszIssue As String * 255
pszExpireFrom As String * 255
pszExpireTo As String * 255
nPhotoLen As Long
pbPhoto As Long
End Type
Private Sub Command1_Click()
Dim myIdcardUserInfo As IdcardUserInfo
Dim lRet As Long
lRet = ReadIdCardUserInfo(myIdcardUserInfo)
MsgBox myIdcardUserInfo.pszName, vbOKOnly, "消息"
End Sub
补充:.NET技术 , C#