“无效的托管/非托管类型组合。”是什么意思?意思是?

发布于 2024-11-01 16:08:26 字数 2341 浏览 8 评论 0原文

我有以下结构:

    [StructLayout(LayoutKind.Auto,Pack=0)]
    private unsafe struct BIRDSYSTEMCONFIG
    {
        public byte bySystemStatus;
        public byte byError;
        public byte byNumDevices;
        public byte byNumServers;
        public byte byXmtrNum;
        public ushort wXtalSpeed;
        public double dMeasurementRate;
        public byte byChassisNum;
        public byte byNumChassisDevices;
        public byte byFirstDeviceNum;
        public ushort wSoftwareRev;
        public fixed byte byFlockStatus[127];
    }

基于 C++ 结构:

typedef struct tagBIRDSYSTEMCONFIG
{
    BYTE    bySystemStatus;     // current system status (see bird system status bits, above)
    BYTE    byError;            // error code flagged by server or master bird
    BYTE    byNumDevices;       // number of devices in system
    BYTE    byNumServers;       // number of servers in system
    BYTE    byXmtrNum;          // transmitter number (see transmitter number bits, above)
    WORD    wXtalSpeed;         // crystal speed in MHz
    double  dMeasurementRate;   // measurement rate in frames per second
    BYTE    byChassisNum;       // chassis number
    BYTE    byNumChassisDevices; // number of devices within this chassis
    BYTE    byFirstDeviceNum;   // number of first device in this chassis
    WORD    wSoftwareRev;       // software revision of server application or master bird
    BYTE    byFlockStatus[BIRD_MAX_DEVICE_NUM + 1]; // status of all devices in flock, indexed by bird number (see note in BIRDFRAME definition) - see bird flock status bits, above
} 
BIRDSYSTEMCONFIG;

以及以下函数:

    [DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern bool birdGetSystemConfig(
        int nGroupID,
        ref BIRDSYSTEMCONFIG psyscfg,
        bool bGetDriverCopy
        );

基于 C++ 函数:

BOOL DLLEXPORT birdGetSystemConfig(int nGroupID, BIRDSYSTEMCONFIG *psyscfg, BOOL bGetDriverCopy = FALSE);

我这样调用:

BIRDSYSTEMCONFIG sysconf = new BIRDSYSTEMCONFIG();
birdGetSystemConfig(1, ref sysconf, true);

但出现错误告诉我:

无法封送“参数#2”:无效 托管/非托管类型组合。

这意味着什么?为什么会出现这种情况?我怎样才能克服它?欢迎所有建议!

I have the following struct:

    [StructLayout(LayoutKind.Auto,Pack=0)]
    private unsafe struct BIRDSYSTEMCONFIG
    {
        public byte bySystemStatus;
        public byte byError;
        public byte byNumDevices;
        public byte byNumServers;
        public byte byXmtrNum;
        public ushort wXtalSpeed;
        public double dMeasurementRate;
        public byte byChassisNum;
        public byte byNumChassisDevices;
        public byte byFirstDeviceNum;
        public ushort wSoftwareRev;
        public fixed byte byFlockStatus[127];
    }

Based on the C++ struct:

typedef struct tagBIRDSYSTEMCONFIG
{
    BYTE    bySystemStatus;     // current system status (see bird system status bits, above)
    BYTE    byError;            // error code flagged by server or master bird
    BYTE    byNumDevices;       // number of devices in system
    BYTE    byNumServers;       // number of servers in system
    BYTE    byXmtrNum;          // transmitter number (see transmitter number bits, above)
    WORD    wXtalSpeed;         // crystal speed in MHz
    double  dMeasurementRate;   // measurement rate in frames per second
    BYTE    byChassisNum;       // chassis number
    BYTE    byNumChassisDevices; // number of devices within this chassis
    BYTE    byFirstDeviceNum;   // number of first device in this chassis
    WORD    wSoftwareRev;       // software revision of server application or master bird
    BYTE    byFlockStatus[BIRD_MAX_DEVICE_NUM + 1]; // status of all devices in flock, indexed by bird number (see note in BIRDFRAME definition) - see bird flock status bits, above
} 
BIRDSYSTEMCONFIG;

And the following function:

    [DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern bool birdGetSystemConfig(
        int nGroupID,
        ref BIRDSYSTEMCONFIG psyscfg,
        bool bGetDriverCopy
        );

Based on the C++ function:

BOOL DLLEXPORT birdGetSystemConfig(int nGroupID, BIRDSYSTEMCONFIG *psyscfg, BOOL bGetDriverCopy = FALSE);

Which I call like this:

BIRDSYSTEMCONFIG sysconf = new BIRDSYSTEMCONFIG();
birdGetSystemConfig(1, ref sysconf, true);

But get an error telling me:

Cannot marshal 'parameter #2': Invalid
managed/unmanaged type combination.

What does that mean? Why does it occur? How can I overcome it? All suggestions welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2024-11-08 16:08:26

事实证明,我需要做的就是更改:

[StructLayout(LayoutKind.Auto,Pack=0)]

由于

[StructLayout(LayoutKind.Sequential,Pack=0)]

问题不仅仅是如何解决它,因此我将暂时保留它。如果能更多地了解这个错误就好了。

Turns out all I needed to do was change:

[StructLayout(LayoutKind.Auto,Pack=0)]

To

[StructLayout(LayoutKind.Sequential,Pack=0)]

Since the question was about more than just how to solve it, I'll leave it open for a while. It'd be nice to find out a bit more about this error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文