急C# 调用 c++ dll 报错 尝试读取或写入受保护的内存。这通常指示其他内存已损坏,牛人回答
急:C# 调用 c++ dll 出错 尝试读取或写入受保护的内存。这通常指示其他内存已损坏,牛人回答。
各位大侠:
我在用c# 调用一个c++ 的 dll。
C++的函数类型如下:
void __stdcall GetServerAddressAndPort ( const char * sIP,
const unsigned int nLen,
int & nPort
)
函数说明:
获取服务器IP和端口
Parameters
sIP[Out] 服务器IP
nLen 字符长度
nPort[Out] 服务器端口
C# 调用:
声明:
[DllImport("ImApi.dll", EntryPoint = "GetServerAddressAndPort", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern void GetServerAddressAndPort(StringBuilder sip, int nLen, int nport);
程序中调用:
StringBuilder ip= new StringBuilder(256);
GetServerAddressAndPort(ip,256, port);
结果提示:
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
我捣鼓了好几天了,真是着急,希望高手帮忙解决。
------解决思路----------------------
------解决思路----------------------
换成char数组试试
------解决思路----------------------
const char * sIP 对应 StringBuilder 是没问题的,
但int & nPort 你换成 int nport 肯定就不行了,原来是传出,你给变成传入了
------解决思路----------------------
http://blog.csdn.net/yatusiter/article/details/9221861
各位大侠:
我在用c# 调用一个c++ 的 dll。
C++的函数类型如下:
void __stdcall GetServerAddressAndPort ( const char * sIP,
const unsigned int nLen,
int & nPort
)
函数说明:
获取服务器IP和端口
Parameters
sIP[Out] 服务器IP
nLen 字符长度
nPort[Out] 服务器端口
C# 调用:
声明:
[DllImport("ImApi.dll", EntryPoint = "GetServerAddressAndPort", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern void GetServerAddressAndPort(StringBuilder sip, int nLen, int nport);
程序中调用:
StringBuilder ip= new StringBuilder(256);
GetServerAddressAndPort(ip,256, port);
结果提示:
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
我捣鼓了好几天了,真是着急,希望高手帮忙解决。
------解决思路----------------------
void __stdcall GetServerAddressAndPort(char *sIP, const unsigned int nLen, int &nPort);
public static extern void GetServerAddressAndPort(StringBuilder sip, int nLen, [Out] ref int nport);
------解决思路----------------------
换成char数组试试
------解决思路----------------------
const char * sIP 对应 StringBuilder 是没问题的,
但int & nPort 你换成 int nport 肯定就不行了,原来是传出,你给变成传入了
------解决思路----------------------
public static extern void GetServerAddressAndPort(String sip, uint nLen, ref int nport);
http://blog.csdn.net/yatusiter/article/details/9221861