C#尝试读取或写入受保护的内存。这通常指示其他内存已损坏。调用C++dll的方法出的错
史_老师 2014-04-18 04:49:47 在网上查的是说我的数据类型错误,但是我不知道是哪里错了?求大神解救!!
------------------------------------------------------------
错误代码行:emReturnType = eshong_ipcsdk_invoke.IPCClient_Logon(logoninfo,ref hClient);
----------------------
IPCClient_Logon方法:
[DllImport("IPCSDK.dll", EntryPoint = "IPCClient_Logon", CallingConvention = CallingConvention.Cdecl)]
public static extern emReturnType IPCClient_Logon(LOGON_INFO pLogonInfo, ref IntPtr hClient);
--------------------
LOGON_INFO 结构体:
public const int name_len = 32;
public const int pw_len = 16;
public const int emLen_IP = 10;
public const int emLen_Name = 10;
public const int emLen_Password = 10;
public struct LOGON_INFO
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = emLen_IP)]
// szDeviceIP:设备IP
public string szDeviceIP;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = emLen_Name)]
//szDeviceName:设备的名称
public string szDeviceName;
//nDevicePort:设备的端口
public Int16 nDevicePort;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = emLen_Name)]
//szUserName:登录设备的用户名
public string szUserName;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = emLen_Password)]
//szPassword:登录设备的密码
public string szPassword;
// pContext:上下文
public IntPtr pContext;
//bRegHeartbeat: 是否注册心跳,如果是公网的的设备,就不要注册心跳,默认为TRUE
public Boolean bRegHeartbeat;
//dwTimeout:登录超时时间(毫秒)
public int dwTimeout;
}这是引用的C的结构体
------------------
c原来的结构体
typedef struct tagLOGON_INFO
{
char szDeviceIP[emLen_IP];
char szDeviceName[emLen_Name];
int nDevicePort;
char szUserName[emLen_Name];
char szPassword[emLen_Password];
void *pContext;
BOOL bRegHeartbeat;
DWORD dwTimeout;
tagLOGON_INFO()
{
ZeroMemory(this, sizeof(tagLOGON_INFO));
bRegHeartbeat = TRUE;
}
}LOGON_INFO, *LPLOGON_INFO;
---------------------------