"SafeArray cannot be marshaled to this array type" 错误
"SafeArray cannot be marshaled to this array type" error
我有一个 C++ COM 本地服务器和 C# 客户端。服务器代码:
// MyStruct as define in the _i.h file
typedef /* [uuid] */ DECLSPEC_UUID("...") struct MyStruct
{
SAFEARRAY * FormatData;
LONG aLong;
BOOL aBool;
} MyStruct;
// Server method being invoked
STDMETHODIMP CMyClass::Foo(MyStruct* StreamInfo, int* result)
{
long Length;
BYTE* Data;
GetData(Length, Data);
PackBytes(Length, Data, &(StreamInfo->FormatData));
}
PackBytes
将 BYTE
数组转换为 SAFEARRAY
。它取自 this Whosebug question。它设置 SAFEARRAY
.
的边界和维度
客户端代码:
MyStruct myStruct;
int rc = obj.Foo(out myStruct);
其中 MyStruct
是从 COM 程序集导入的。它显示为
public struct MyStruct
{
public Array FormatData;
int aLong;
int aBool;
}
在 运行 Foo
之后出现错误 "SafeArray cannot be marshaled to this array type because it has either nonzero lower bounds or more than one dimension" 并附加说明 "Make sure your array has the required number of dimensions".
调试服务器代码时,Data
似乎已正确填充到 FormatData
中:如下面的屏幕截图所示。 cElements
等于Length
,18个数据等于Data
.
硬编码Length = 1
没有帮助。删除 PackByets
调用使错误消失(其他字段通过正常)。如何解决?
您引用的 PackBytes
方法构造了一个下限为 1 的 SAFEARRAY
。用零下限构造它可能会解决问题:
SAFEARRAYBOUND bound{ count, 0 };
我有一个 C++ COM 本地服务器和 C# 客户端。服务器代码:
// MyStruct as define in the _i.h file
typedef /* [uuid] */ DECLSPEC_UUID("...") struct MyStruct
{
SAFEARRAY * FormatData;
LONG aLong;
BOOL aBool;
} MyStruct;
// Server method being invoked
STDMETHODIMP CMyClass::Foo(MyStruct* StreamInfo, int* result)
{
long Length;
BYTE* Data;
GetData(Length, Data);
PackBytes(Length, Data, &(StreamInfo->FormatData));
}
PackBytes
将 BYTE
数组转换为 SAFEARRAY
。它取自 this Whosebug question。它设置 SAFEARRAY
.
客户端代码:
MyStruct myStruct;
int rc = obj.Foo(out myStruct);
其中 MyStruct
是从 COM 程序集导入的。它显示为
public struct MyStruct
{
public Array FormatData;
int aLong;
int aBool;
}
在 运行 Foo
之后出现错误 "SafeArray cannot be marshaled to this array type because it has either nonzero lower bounds or more than one dimension" 并附加说明 "Make sure your array has the required number of dimensions".
调试服务器代码时,Data
似乎已正确填充到 FormatData
中:如下面的屏幕截图所示。 cElements
等于Length
,18个数据等于Data
.
硬编码Length = 1
没有帮助。删除 PackByets
调用使错误消失(其他字段通过正常)。如何解决?
您引用的 PackBytes
方法构造了一个下限为 1 的 SAFEARRAY
。用零下限构造它可能会解决问题:
SAFEARRAYBOUND bound{ count, 0 };