Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

c# - What does "Invalid managed/unmanaged type combination." mean?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...