【发布时间】:2015-06-07 06:41:24
【问题描述】:
我有一个 c++ 结构如下:
struct Vehicle
{
u32 something;
Info *info;
u8 something2[ 0x14 ];
Vector3f location;
Template* data;
};
struct Info
{
u32 speed;
std::string name;
BuffCollection** buffCollection;
void* sectionPtr;
};
struct Template
{
u32 templateID;
};
From this question,我弄清楚了u32、u8等的含义,或者我想我做到了。
然后我尝试用它制作自己的 C# 结构:
[StructLayout(LayoutKind.Sequential)]
public struct Vehicle
{
public uint Something;
public Info Info;
public byte Something2;
public Vector3f Location;
public Template Data;
}
[StructLayout(LayoutKind.Sequential)]
public struct Info
{
public uint Speed;
public string Name;
public byte[] BuffCollection;
public IntPtr SectionPointer;
}
[StructLayout(LayoutKind.Sequential)]
public struct Template
{
public uint TemplateId;
}
public struct Vector3f
{
public float X, Y, Z;
public Vector3f(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
}
但是,当我尝试读取车辆时:
[DllImport("Core.dll")]
static extern Vehicle GetVehicle();
static void Main()
{
var vehicle = GetVehicle();
Console.WriteLine(vehicle.Info.Name);
Console.ReadKey();
}
我收到以下错误:
System.Runtime.InteropServices.MarshalDirectiveException: Method's type signature is not PInvoke compatible
从我对它的搜索来看,它让我相信我的结构转换是错误的。
- 转换后的结构有什么问题?
【问题讨论】:
-
我的问题错了吗?我在没有任何信息的情况下看到投票结束,为什么...是因为在底部我有 3 分吗?如果是这样,我可以删除其他 2 个并让它们成为一个新问题。
-
函数的 C++ 签名是什么?
-
@TheodorosChatzigiannakis
extern "C" __declspec(dllexport) Vehicle* GetVehicle();不确定这是否是您要查找的内容,但除了结构之外我只知道这些,因为我无法访问 DLL 源代码。 -
注意
something2中的Vehicle,因为它是一个数组。 -
@Caramiriel 所以应该是
byte[]?