- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过 P/Invoke 调用编写一个小型 zlib 包装器。它在 64 位目标(64 位 C# 构建,64 位 DLL)上完美运行,但在 32 位目标(32 位 C# 构建,32 位 DLL)上抛出 AccessViolationException。
这是引发异常的 C# 签名和代码:
[DllImport(Program.UnmanagedDll, CallingConvention = CallingConvention.Cdecl)]
private static extern ZLibResult ZLibDecompress(byte[] inStream, uint inLength, byte[] outStream, ref uint outLength);
internal enum ZLibResult : byte {
Success = 0,
Failure = 1,
InvalidLevel = 2,
InputTooShort = 3
}
internal static ZLibResult Decompress(byte[] compressed, out byte[] data, uint dataLength) {
var len = (uint) compressed.Length;
fixed (byte* c = compressed) {
var buffer = new byte[dataLength];
ZLibResult result;
fixed (byte* b = buffer) {
result = ZLibDecompress(c, len, b, &dataLength);
}
if(result == ZLibResult.Success) {
data = buffer;
return result;
}
data = null;
return result;
}
}
这是 C 代码(使用 MinGW-w64 编译):
#include <stdint.h>
#include "zlib.h"
#define ZLibCompressSuccess 0
#define ZLibCompressFailure 1
__cdecl __declspec(dllexport) uint8_t ZLibDecompress(uint8_t* inStream, uint32_t inLength,
uint8_t* outStream, uint32_t* outLength)
{
uLongf oL = (uLongf)*outLength;
int result = uncompress(outStream, &oL, inStream, inLength);
*outLength = (uint32_t)oL;
if(result == Z_OK)
return ZLibCompressSuccess;
return ZLibCompressFailure;
}
我查看了所有内容,但无法弄清楚为什么访问冲突会发生在 32 位版本上而不是 64 位版本上。 ZLibDecompress 在从 C 应用程序调用时可以很好地解压缩相同的流,但在从我的 C# 应用程序调用时会引发访问冲突。
有谁知道为什么会这样?
编辑:更新了我的代码,在 32 位版本上仍然出现访问冲突,但在 64 位版本上没有。
C#代码:
[DllImport(Program.UnmanagedDll, CallingConvention = CallingConvention.Cdecl)]
private static extern ZLibResult ZLibDecompress(
[MarshalAs(UnmanagedType.LPArray)]byte[] inStream, uint inLength,
[MarshalAs(UnmanagedType.LPArray)]byte[] outStream, ref uint outLength);
internal static ZLibResult Decompress(byte[] compressed, out byte[] data, uint dataLength) {
var buffer = new byte[dataLength];
var result = ZLibDecompress(compressed, (uint)compressed.Length, buffer, ref dataLength);
if(result == ZLibResult.Success) {
data = buffer;
return result;
}
data = null;
return result;
}
C 代码:
__declspec(dllexport) uint8_t __cdecl ZLibDecompress(uint8_t* inStream, uint32_t inLength,
uint8_t* outStream, uint32_t* outLength) {
uLongf oL = (uLongf)*outLength;
int result = uncompress(outStream, &oL, inStream, inLength);
*outLength = (uint32_t)oL;
if(result == Z_OK)
return ZLibCompressSuccess;
return ZLibCompressFailure;
}
最佳答案
fixed (byte* b = buffer) {
result = ZLibDecompress(c, len, b, &dataLength);
}
不,那行不通。 fixed 关键字提供了一种高度优化的方式来确保垃圾收集器移动对象不会造成麻烦。它不是通过固定对象(如文档所述)来实现的,而是通过将 b
变量暴露给垃圾收集器来实现的。然后它看到它引用缓冲区并在移动 buffer
时更新 b
的值。
然而,这在这种情况下不起作用,b
值的副本 已传递给 ZlibDecompress()。垃圾收集器无法更新该副本。如果在 ZLibDecompress() 运行时发生 GC,结果会很差, native 代码会破坏垃圾收集堆的完整性,最终会导致 AV。
您不能使用固定,您必须使用 GCHandle.Alloc() 来固定缓冲区。
但是也不要那样做,你帮的太多了。 pinvoke marshaller 已经非常擅长在必要时固定对象。将 instream
和 outstream
参数声明为 byte[] 而不是 byte*。并直接传递数组而不做任何特殊操作。此外,outlength
参数应声明为 ref int
。
关于c# - P/Invoke 调用中的 AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11272256/
关于strcat函数。 while (*p) p++; 和 while (*++p) ; 两者都有效,但是 while (*p++) ; 不起作用。我认为 first 和 th
" in HTML?(HTML中的““是什么
?)
下面例子中的第一行代码是什么。我看到一个YouTuber在写下面的代码,它显示了一个设计在csswar Challenges中。我也尝试了一下,它很管用。但我以前从未在任何HTML教程上看到过它,我在
vs.
是不间断空格,表示没有换行的空白处。 如果我用 我在两个段落之间有一个空格(更大的间隔)。如果我使用 我在两个段落之间只有一个新行(没有中断)。为什么? 最佳答案 在 HTML 中
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 9
我对编程还很陌生,只是想知道为什么这段代码: for ( ; *p; ++p) *p = tolower(*p); 当 p 指向一个字符串时,可以降低 c 中字符串的大小写吗? 最佳答案 一般来说,这
代码 int n = 25; int *p = &n; printf("%x\n %d\n %x\n", p, p[0], p[1]); 返回: \ 当然我永远不会这样做,但在 K&R 中声明
所以,我想创建一个简单的程序,返回有关连续素数的计算结果。首先,我创建一个包含所有这些素数的列表,然后尝试计算结果,但这给了我一个超出范围的索引。有人可以帮助我吗?我的程序: primes = []
这个问题在这里已经有了答案: With arrays, why is it the case that a[5] == 5[a]? (20 个答案) 关闭 9 年前。 我想知道 C/C++ 中以下四
我仍在努力理解 *p、&p 和 p 之间的区别。根据我的理解,* 可以被认为是“指向的值”,而 & 可以被认为是“地址”。换句话说,* 保存值,而 & 保存地址。如果这是真的,那么 *p 和 p 之间
你是吗? [xxxrecipientFirstNamexxx]
和你是吗? {recipientFirstName}
需要更换 你是吗? [xxxrecipientFirstNamexxx] 和 你是吗? {recipientFirstName} 。我尝试使用边界匹配器。但结果并不符合预期。我尝试使用下面的代码 "A
我想按 IsTop 属性升序排序对象,然后按 JobId 属性降序排序: query = query.OrderBy(p => p.IsTop).ThenOrderByDescending(p =
在我尝试使用 Apache POI 进行转换的 Excel 文件中,我有一个单元格的数值为 -3.97819466831428,自定义格式为“0.0 p.p.;(0.0 p.p.)”。因此,在 Exc
我想创建一个扩展方法,允许我调用 ToSerializableDictionary(p => p.ID)而不是 .ToDictionary(p => p.ID)在以下 LINQ 上下文中。虽然我不确定
在下面的 HTML 代码上运行此 jQuery 代码会返回不同的结果,我认为它们应该返回相同的值。 jQuery 代码: var counter = 0; $("p").each(function()
在下面的代码片段中,符号 *p 等同于 p[0],*(p + 1) 等同于p[1],依此类推。 int* p = new int[3] { 1, 2, 3}; cout << *p << ' ' <<
这个问题在这里已经有了答案: What will happen when I call a member function on a NULL object pointer? [duplicate]
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Undefined Behavior and Sequence Points 按照标准中的定义,E1 +=
" in HTML?(在HTML中“
以下示例中的第一行代码是什么。我看到一个youtube用户写下面的代码,它显示在cssbattle挑战的设计。我也试过,它的作品。但我从来没有见过它在任何HTML教程之前,我在谷歌上搜索它,但它只显示
每当我收到来自 MS outlook 的电子邮件时,我都会收到此标记 & nbsp ; (没有空格)哪个显示为?在 <>. 当我将其更改为 ISO-8859-1 时,浏览器页面字符集编码为 UTF-8
p1
TESTp2
代码: from bs4 import BeautifulSoup soup = BeautifulSoup('p1TESTp2') print soup.div() 结果: [p1, p2] 为什么
我是一名优秀的程序员,十分优秀!