C# 调用C++的dll,提示"尝试读取或写入受保护的内存。这通常指示其他内存已损坏"

chinakgsoft 2009-01-08 03:36:34
C++:

GPSSUPPORT_API int __stdcall ress(unsigned char *compr, unsigned long comprLen, unsigned char **uncompr, unsigned long *uncomprLen);
C#:
public static extern int ZlibUncompress(ref byte[] compr, long comprLen, ref byte[] uncompr, ref long uncomprLen);
调用:
private byte[] Keys = { 0x78, 0xDA, 0x63, 0x60, 0x60, 0x6C, 0x60, 0x6A, 0x63, 0x64, 0x60, 0x16, 0x06, 0x00, 0x06, 0x59, 0x01, 0x21 };
byte[] b=new byte[255];
long blen=a.Length;
int Err = XWAPI.ZlibUncompress(ref a, (long)a.Length, ref b, ref blen);
提示:尝试读取或写入受保护的内存。这通常指示其他内存已损坏"
如何处理,谢谢.




尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
...全文
243 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
diandian0122 2012-06-10
  • 打赏
  • 举报
回复
[DllImport("xx.dll")]
public static extern int ZlibUncompress(byte[] compr,uint comprLen,ref byte[] uncompr,ref uint uncomprLen);
diandian0122 2012-06-10
  • 打赏
  • 举报
回复
[DllImport("xx.dll")]
public static extern int ZlibUncompress(byte[] compr,uint comprLen,ref byte[] uncompr,ref uint uncomprLen);

lunat 2009-01-09
  • 打赏
  • 举报
回复
LZ的c++函数是否写错了?
我在网上查了一下 LZ用的是zlib.dll来压缩解压缩吧 貌似c++的函数原型不是这样的 你再仔细检查检查 压缩 解压缩的别写反了..
chinakgsoft 2009-01-09
  • 打赏
  • 举报
回复
[DllImport("xx.dll")]
public static extern int ZlibUncompress(byte[] compr,uint comprLen,ref byte[] uncompr,ref uint uncomprLen);


private byte[] Keys = { 0x78, 0xDA, 0x63, 0x60, 0x60, 0x6C, 0x60, 0x6A, 0x63, 0x64, 0x60, 0x16, 0x06, 0x00, 0x06, 0x59, 0x01, 0x21 }; uint blen=0;
uint alen = Convert.ToUInt32(Keys.Length);
byte[] buffer = new byte[128];
int Err = XWAPI.ZlibUncompress(Keys, alen, ref buffer, ref blen);

我这样写了.可是blen是对的.但buffer的没值,值不对.请上楼的朋友帮我看看.谢谢.
lunat 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chinakgsoft 的回复:]
C++代码写错了.是这样的:GPSSUPPORT_API int __stdcall ZlibUncompress(unsigned char *compr, unsigned long comprLen, unsigned char **uncompr, unsigned long *uncomprLen);
[/Quote]


[DllImport("xx.dll")]
public static extern int ZlibUncompress(byte[] compr,uint comprLen,ref byte[] uncompr,ref uint uncomprLen);

Dobzhansky 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wodegege10 的回复:]
先用C写个测试程序看看,
[/Quote] .
wenbin 2009-01-08
  • 打赏
  • 举报
回复
先用C写个测试程序看看,
qimini 2009-01-08
  • 打赏
  • 举报
回复
看不出来,不过char*一般对应C#里的string或者StringBuilder
chinakgsoft 2009-01-08
  • 打赏
  • 举报
回复
C++代码写错了.是这样的:GPSSUPPORT_API int __stdcall ZlibUncompress(unsigned char *compr, unsigned long comprLen, unsigned char **uncompr, unsigned long *uncomprLen);
chinakgsoft 2009-01-08
  • 打赏
  • 举报
回复
C++的代码:GPSSUPPORT_API int __stdcall ZlibUncompress(unsigned char *compr, unsigned long comprLen, unsigned char **uncompr, unsigned long *uncomprLen);

111,131

社区成员

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

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

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