【发布时间】:2016-05-26 10:35:20
【问题描述】:
使用 C++:
extern "C" MY_API int connect(const char* pcHost,const int nPort, ConnectionId_T *pConId);
存在:
-
pcHost一个IP -
nPort一个端口 -
pConId一个常数 (-1)
我的尝试是:
[DllImport("my.dll", CharSet = CharSet.Ansi)]
public static extern int connect(StringBuilder pcHost, int nPort, ConnectionId_T pConId);
但是当我尝试连接时:
StringBuilder ip = new System.Text.StringBuilder();
ip.Append("1.1.1.1");
int nPort = 1111;
ConnectionId_T nConId = MY_NCONID // This is defined previously as -1 (public const int MY_NCONID = -1)
connect(ip, nPort, nConId))
我在 connect 行收到 AccesViolationException。我的元帅错了吗?
PS:ConectionId_T 定义为using ConnectionId_T = System.Int32;
提前致谢。
【问题讨论】:
-
@codroipo 我也试过
[MarshalAs(UnmanagedType.LPStr)],我得到了同样的错误。 -
您还必须将属性添加到您的方法定义
[DllImport("yourdll.dll", CharSet = CharSet.Ansi)] -
@MujahidDaudKhan 这已包含在原始帖子中。
标签: c# c++ marshalling