【发布时间】:2014-02-24 12:59:57
【问题描述】:
我在 C++ 中定义了以下结构:
struct GraphicsAdapterDesc {
// ... Just some constructors / operators / destructor here
DEFINE_DEFAULT_CONSTRUCTOR(GraphicsAdapterDesc);
DEFINE_DEFAULT_DESTRUCTOR(GraphicsAdapterDesc);
ALLOW_COPY_ASSIGN_MOVE(GraphicsAdapterDesc);
std::wstring AdapterName;
int32_t AdapterNum;
std::wstring HardwareHash;
int64_t DedicatedVMEM;
int64_t DedicatedSMEM;
int64_t SharedSMEM;
int32_t NumOutputs;
};
在 C# 中,我有一个这样声明的“镜像”结构:
[StructLayout(LayoutKind.Sequential)]
public struct GraphicsAdapterDesc {
string AdapterName;
int AdapterNum;
string HardwareHash;
long DedicatedVMEM;
long DedicatedSMEM;
long SharedSMEM;
int NumOutputs;
};
我尝试非常小心地匹配变量的宽度(尽管我有点不确定如何准确地处理字符串)。
无论如何,我有以下导出的 C 方法:
extern "C" __declspec(dllexport) bool GetGraphicsAdapter(int32_t adapterIndex, GraphicsAdapterDesc& outAdapterDesc) {
outAdapterDesc = RENDER_COMPONENT.GetGraphicsAdapter(adapterIndex);
return true;
}
并且,我的 C# 应用程序中的以下 extern 方法:
[DllImport(InteropUtils.RUNTIME_DLL, EntryPoint = "GetGraphicsAdapter", CallingConvention = CallingConvention.Cdecl)]
internal static extern bool _GetGraphicsAdapter(int adapterIndex, out GraphicsAdapterDesc adapterDesc);
但是,当我调用它时,它无法正常工作。根据我是在 x64 还是 x86 模式下编译(C++ DLL 和 C# 应用程序都编译为 x86 或 x64),我得到不同的结果:
- 在 x86 模式下,调用返回,但结构中包含“废话”值,并且字符串均为空,
- 在 x64 模式下,调用会引发 NullPointerException。
我的期望是我在编组字符串时做错了,我需要为字符指定“宽模式”,但我不知道如何(或者这是否是正确的选择)。
提前谢谢你。
【问题讨论】: