C# 封送具有 wchar_t* 成员的 C++ 结构有时会使堆损坏
C# Marshalling a C++ struct with wchar_t* member occasionally leaves the heap corrupted
我声明了一个struct
,如下所示:
// C++
struct TestStruct
{
wchar_t* TestString;
};
和相应的托管表示形式
// C#
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TestStruct
{
[MarshalAs(UnmanagedType.LPWStr)]
public string TestString;
}
以及此功能:
// C++
__declspec(dllexport) void __stdcall FillMultipleStructs(TestStruct* testStructures, const short arrayLength)
{
for(int i = 0; i < arrayLength; i++)
{
const wchar_t stringToAllocate[] = L"foo";
const unsigned long size = wcslen(stringToAllocate) * sizeof(wchar_t) + sizeof(wchar_t);
wchar_t* allocatedString = static_cast<wchar_t*>(::CoTaskMemAlloc(size));
wcscpy_s(allocatedString, size, stringToAllocate);
(&testStructures[i])->testString = allocatedString;
}
}
它由 FillMultipleStructs
方法调用,该方法采用多个TestStructs
并在C++代码中初始化它们。
// C#
[DllImport("the path", CallingConvention = CallingConvention.StdCall, EntryPoint = "FillMultipleStructs", ExactSpelling = true, CharSet = CharSet.Unicode)]
[SuppressUnmanagedCodeSecurity]
private static extern void _FillMultipleStructs([In, Out] TestStruct[] structures, [MarshalAs(UnmanagedType.I2)] short arrayLength);
public static IEnumerable<TestStruct> FillMultipleStructs(IEnumerable<TestStruct> structures)
{
TestStruct[] structuresArray = structures.ToArray();
_FillMultipleStructs(structuresArray, (short) structuresArray.Length);
return structuresArray;
}
调用代码的工作方式如下:
FillMultipleStructs(
new List<TestStruct>()
{
new TestStruct(),
new TestStruct(),
new TestStruct()
});
现在,问题是:有时,代码有效,但是,有时我会收到a heap has been corrupted
错误。我不明白这是从哪里来的,也不明白为什么它偶尔会起作用,有时也不会。
我想这与struct
弦成员的编组有关,所以,如果有人能告诉我我的错误在哪里,或者如果有人能指出我正确的方向或告诉我正确的方法,我将不胜感激。
对于遇到这个问题的人,请总结一下 pstrjds 在评论中已经说过的话:
元帅作为
BSTR
,然后代替CoTaskMemAlloc
电话, 创建BSTR
对象
这实际上意味着将C#
struct
的定义从
[MarshalAs(UnmanagedType.LPWStr)]
public string TestString;
自
[MarshalAs(UnmanagedType.BStr)]
public string TestString;
而不是使用 ::CoTaskMemAlloc
在 C++
中分配字符串,需要使用 ::SysAllocString
。
我不必更改C++
struct
的签名,因为BSTR
(就我而言)最终是wchar_t*
的typedef
。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- C++概念:如何使用'concept'检查模板化结构的属性?