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;
---------------------------
...全文
134 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧