作者iamkjs (这就是人生)
看板C_Sharp
标题[问题]透过DllImport引用DLL出现记忆体损毁例外
时间Thu Feb 5 21:43:50 2009
小弟透过C#去使用第三方(C/C++)的DLL时,遇到如下的例外讯息
拦截到 System.AccessViolationException
Message="尝试读取或写入受保护的记忆体。这通常表示其他记忆体已损毁。"
本以为是在包DLL时参数型态给定错误,
但其实该DLL的方法确实有成功执行(透过该DLL方法写入的特定log档得知)
不知道大家可以看一下我写的程式是否有问题~~thx
目前比较怀疑回传的型态,我用String去接,是否有误...
----C#程式片段----------
[DllImport("dllbndclir.dll", SetLastError = true)]
private unsafe static extern String msgCliFixConn(
String senderID,
String targetID,
String basePath,
String host,
int port,
int hbt,
String profile,
int errorCode,
String Encode);
string msgCliHdl = msgCliConn(
"9700001",
"9720001",
"./Log",
"192.168.11.11",
11122,
6,
"setting.ini",
0,
null);
---厂商提供的DLL说明(片段)-------
.........
typedef char *msgCliHdl_t;
.........
/**
* Input
* (略)
* return
* return NULL if failure -- msgCliHdl is not allocated.
* Othereise, return allocated msgCliHdl.
*
*/
msgCliHdl_t CallStack msgCliConn(char *senderID, char *targetID,
char *basePath, char *host, int port, int hbt, char *profile,
int errorCode, Char *Encode);
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.217.32.71
1F:推 toki:我建议回传值改用 IntPtr 接 02/05 23:40
2F:→ toki:然後用 Marshal.PtrToStringAuto 之类的转成 String 02/05 23:40
3F:→ toki:但要先检查回传值是否 == IntPtr.Zero 02/05 23:41