使用DllImport的TypeLoadException

时间:2016-07-28 09:28:07

标签: c# c++ pinvoke marshalling dllimport

我使用的是使用C#x86应用程序在C ++ Win32中开发的第三方库。

我对这个库进行不同的调用,除了一个之外都会成功。

该通话包含复杂的签名。 这是C ++签名:

ER_CODE KMAPI CLGetMeasData(DEVICE_HANDLE hDevice, CL_COLORSPACE Type, CL_MEASDATA* pColor);

以下是了解签名

的所有定义
typedef int                 int32_km;           //!< 32bit(signed)
typedef int32_km ER_CODE;

#define KMAPI __stdcall     //!< stdcall (for Windows OS)

typedef void* DEVICE_HANDLE;

/*!
 * The enumeration of color space
 */
typedef enum eCL_COLORSPACE
{
    CL_COLORSPACE_EVXY          = 0,    //!< Ev, x, y
    CL_COLORSPACE_EVUV,                 //!< Ev, uf, vf
    CL_COLORSPACE_EVTCPJISDUV,          //!< Ev, Correlated color temperature Tcp(JIS), delta uv
    CL_COLORSPACE_EVTCPDUV,             //!< Ev, Correlated color temperature Tcp, delta uv
    CL_COLORSPACE_EVDWPE,               //!< Ev, Dominant wavelength, Excitation purity
    CL_COLORSPACE_XYZ,                  //!< X, Y, Z
    CL_COLORSPACE_RENDERING,            //!< Color Rendering Index (Ra, R1 to R15)
    CL_COLORSPACE_PW,                   //!< Peak wavelength
    CL_COLORSPACE_SPC,                  //!< Illuminance spectral data
    CL_COLORSPACE_SCOTOPIC,             //!< Ev, Ev', S/P ratio
    CL_COLORSPACE_NUM,

    CL_COLORSPACE_FIRST = CL_COLORSPACE_EVXY,
    CL_COLORSPACE_LAST = CL_COLORSPACE_SCOTOPIC,
    CL_COLORSPACE_DEFAULT = CL_COLORSPACE_EVXY  
}
CL_COLORSPACE;

//--------------------
// Measurement data
//--------------------
/*!
 * The structure of Evxy data
 */
typedef struct tCL_EvxyDATA{
    float Ev;           //!< Ev
    float x;            //!< x
    float y;            //!< y
}
CL_EvxyDATA;

/*!
 * instrument information structure
 */
typedef struct tCL_EvuvDATA{
    float Ev;           //!< Ev
    float u;            //!< uf
    float v;            //!< vf
}
CL_EvuvDATA;

/*!
 * The structure of Ev/Correlated color temperature/delta uv data
 */
typedef struct tCL_EvTduvDATA{
    float Ev;           //!< Ev data
    float T;            //!< Correlated color temperature
    float duv;          //!< delta uv
}
CL_EvTduvDATA;

/*!
 * The structure of Ev/Dominant wavelength/Excitation purity data
 */
typedef struct tCL_EvDWPeDATA{
    float Ev;           //!< Ev data
    float DW;           //!< Dominant wavelength
    float Pe;           //!< Excitation purity
}
CL_EvDWPeDATA;

/*!
 * The structure of color rendering index
 */
typedef struct tCL_RenderingDATA{
    float Data[CL_RENDERING_LEN];           //!< The color rendering index
}
CL_RenderingDATA;

/*!
 * The structure of Peak wavelength
 */
typedef struct tCL_PWDATA{
    float PeakWave;     //!< Peak wavelength
}
CL_PWDATA;

/*!
 * The structure of illuminance spectral data
 */
typedef struct tCL_SPCDATA
{
    real        Data[IRRADIANCE_LEN];           //!< Illuminance spectral data
}
CL_SPCDATA;

/*!
 * The structure of XYZ data
 */
typedef struct tCL_XYZDATA{
    float X;            //!< X
    float Y;            //!< Y
    float Z;            //!< Z
}
CL_XYZDATA;

/*!
 * The structure of Scotopic lux data
 */
typedef struct tCL_CL_ScotopicDATA{
    float Ev;           //!< Ev  Photopic lux data
    float Es;           //!< Ev' Scotopic lux data
    float SP;           //!< S/P ratio
}
CL_ScotopicDATA;

/*!
 * The union of the measurement data
 */
typedef union tCL_MEASDATA
{
    CL_EvxyDATA     Evxy;                   //!< Ev/x/y data
    CL_EvuvDATA     Evuv;                   //!< Ev/uf/v
    CL_EvTduvDATA   EvTduv;                 //!< Ev/Tcp/delta uv
    CL_EvDWPeDATA   EvDWPe;                 //!< Ev/Dominant wavelength /Excitation purity
    CL_XYZDATA      XYZ;                    //!< X/Y/Z
    CL_RenderingDATA    Rendering;          //!< Color rendering index
    CL_PWDATA       Pw;                     //!< Peak wavelength
    CL_SPCDATA      Spc;                    //!< Illuminance spectral data
    CL_ScotopicDATA Scotopic;               //!< Ev/Ev'/SP
}
CL_MEASDATA;

我编写了以下代码以适应调用

[DllImport("libclapi.dll", EntryPoint = "CLGetMeasData", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern int CLGetMeasData(IntPtr hDevice, ClColorConditions.CL_COLORSPACE type, out ClConditions.CL_MEASDATA color);

以下类型声明

public class ClColorConditions
    {
        //!< The start wavelength for illuminance spectral data
        public const int IRRADIANCE_BEGIN = 360;
        //!< The end wavelength for illuminance spectral data
        public const int IRRADIANCE_END = 780;
        //!< The wavelength pitch for illuminance spectral data
        public const int IRRADIANCE_PITCH = 1;
        //!< The number of illuminance spectral data 
        public const int IRRADIANCE_LEN = (IRRADIANCE_END - IRRADIANCE_BEGIN) / IRRADIANCE_PITCH + 1;

        /*!
         * The enumeration of color space
         */
        public enum CL_COLORSPACE
        {
            CL_COLORSPACE_EVXY = 0, //!< Ev, x, y
            CL_COLORSPACE_EVUV,                 //!< Ev, uf, vf
            CL_COLORSPACE_EVTCPJISDUV,          //!< Ev, Correlated color temperature Tcp(JIS), delta uv
            CL_COLORSPACE_EVTCPDUV,             //!< Ev, Correlated color temperature Tcp, delta uv
            CL_COLORSPACE_EVDWPE,               //!< Ev, Dominant wavelength, Excitation purity
            CL_COLORSPACE_XYZ,                  //!< X, Y, Z
            CL_COLORSPACE_RENDERING,            //!< Color Rendering Index (Ra, R1 to R15)
            CL_COLORSPACE_PW,                   //!< Peak wavelength
            CL_COLORSPACE_SPC,                  //!< Illuminance spectral data
            CL_COLORSPACE_SCOTOPIC,             //!< Ev, Ev', S/P ratio
            CL_COLORSPACE_NUM,

            CL_COLORSPACE_FIRST = CL_COLORSPACE_EVXY,
            CL_COLORSPACE_LAST = CL_COLORSPACE_SCOTOPIC,
            CL_COLORSPACE_DEFAULT = CL_COLORSPACE_EVXY
        }
    }

公共类ClConditions     {         // --------------------         //测量数据         // --------------------         / *!          * Evxy数据的结构          * /

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_EvxyDATA
    {
        public float Ev; //!< Ev
        public float x; //!< x
        public float y; //!< y
    }

    /*!
     * instrument information structure
     */

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_EvuvDATA
    {
        public float Ev; //!< Ev
        public float u; //!< uf
        public float v; //!< vf
    }

    /*!
     * The structure of Ev/Correlated color temperature/delta uv data
     */

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_EvTduvDATA
    {
        public float Ev; //!< Ev data
        public float T; //!< Correlated color temperature
        public float duv; //!< delta uv
    }

    /*!
     * The structure of Ev/Dominant wavelength/Excitation purity data
     */
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_EvDWPeDATA
    {
        public float Ev; //!< Ev data
        public float DW; //!< Dominant wavelength
        public float Pe; //!< Excitation purity
    }

    /*!
     * The structure of color rendering index
     */
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_RenderingDATA
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = ClColorConditions.IRRADIANCE_LEN)] public float[] Data;  //!< The color rendering index  
    }

    /*!
     * The structure of Peak wavelength
     */
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_PWDATA
    {
        public float PeakWave; //!< Peak wavelength
    }

    /*!
     * The structure of illuminance spectral data
     */
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_SPCDATA
    {
        [MarshalAs(UnmanagedType.LPArray, SizeConst = ClColorConditions.IRRADIANCE_LEN)] public float[] Data; //!< Illuminance spectral data 
    }

    /*!
     * The structure of XYZ data
     */
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_XYZDATA
    {
        public float X; //!< X
        public float Y; //!< Y
        public float Z; //!< Z
    }

    /*!
     * The structure of Scotopic lux data
     */
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CL_ScotopicDATA
    {
        public float Ev; //!< Ev  Photopic lux data
        public float Es; //!< Ev' Scotopic lux data
        public float SP; //!< S/P ratio
    }

    /*!
     * The union of the measurement data
     */
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
    public struct CL_MEASDATA
    {
        [FieldOffset(0)]
        public CL_EvxyDATA Evxy;                    //!< Ev/x/y data
        [FieldOffset(0)]
        public CL_EvuvDATA Evuv;                    //!< Ev/uf/v
        [FieldOffset(0)]
        public CL_EvTduvDATA EvTduv;                //!< Ev/Tcp/delta uv
        [FieldOffset(0)]
        public CL_EvDWPeDATA EvDWPe;                //!< Ev/Dominant wavelength /Excitation purity
        [FieldOffset(0)]
        public CL_XYZDATA XYZ;                      //!< X/Y/Z
        [FieldOffset(0)]
        public CL_RenderingDATA Rendering;          //!< Color rendering index
        [FieldOffset(0)]
        public CL_PWDATA Pw;                        //!< Peak wavelength
        [FieldOffset(0)]
        public CL_SPCDATA Spc;                      //!< Illuminance spectral data
        [FieldOffset(0)]
        public CL_ScotopicDATA Scotopic;            //!< Ev/Ev'/SP
    }
}

当我运行程序时,我遇到以下异常: (抱歉是法语)

  

不可能充电器类型'CL_MEASDATA'àpartirde l'assembly   'xxx,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null',car il   contient un champobjetàl'offding'0'quin'est pas correctement   alignéouquiestchevauchéparun champ non objet。

用英语说:

  

无法加载“CL_MEASDATA”,因为它包含索引0处的字段   未对齐或非对象字段(近似翻译)   我;))

我发现产生问题的两个fiels包含一个嵌入了float列表的结构的字段(public float [] Data;)

如果我从联盟中删除它们似乎通过了但是我得到了堆栈不平衡异常(我认为这是第二个问题)。

但我需要他们!我已尝试使用“UnmanagedType”枚举或 ArraySubType属性,但没有结果确定。

我很高兴用大量代码发帖,但我删除了所有不必要的代码。

如果您有任何想法?

0 个答案:

没有答案