c# - TypeLoadException using DllImport -
i'm using third party library developped in c++ win32 c# x86 application.
i make differents calls library succeed except one.
the call contains complicated signature. here c++ signature:
er_code kmapi clgetmeasdata(device_handle hdevice, cl_colorspace type, cl_measdata* pcolor);
here defines understand signature
typedef int int32_km; //!< 32bit(signed) typedef int32_km er_code; #define kmapi __stdcall //!< stdcall (for windows os) typedef void* device_handle; /*! * 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 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 //-------------------- /*! * 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; /*! * 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; /*! * 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; /*! * structure of color rendering index */ typedef struct tcl_renderingdata{ float data[cl_rendering_len]; //!< color rendering index } cl_renderingdata; /*! * structure of peak wavelength */ typedef struct tcl_pwdata{ float peakwave; //!< peak wavelength } cl_pwdata; /*! * structure of illuminance spectral data */ typedef struct tcl_spcdata { real data[irradiance_len]; //!< illuminance spectral data } cl_spcdata; /*! * structure of xyz data */ typedef struct tcl_xyzdata{ float x; //!< x float y; //!< y float z; //!< z } cl_xyzdata; /*! * 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; /*! * union of 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;
i wrote following code fit call
[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);
with following types declaration
public class clcolorconditions { //!< start wavelength illuminance spectral data public const int irradiance_begin = 360; //!< end wavelength illuminance spectral data public const int irradiance_end = 780; //!< wavelength pitch illuminance spectral data public const int irradiance_pitch = 1; //!< number of illuminance spectral data public const int irradiance_len = (irradiance_end - irradiance_begin) / irradiance_pitch + 1; /*! * 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 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 } }
and
public class clconditions { //-------------------- // measurement data //-------------------- /*! * structure of evxy data */
[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 } /*! * 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 } /*! * 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 } /*! * structure of color rendering index */ [structlayout(layoutkind.sequential, pack = 1)] public struct cl_renderingdata { [marshalas(unmanagedtype.byvalarray, sizeconst = clcolorconditions.irradiance_len)] public float[] data; //!< color rendering index } /*! * structure of peak wavelength */ [structlayout(layoutkind.sequential, pack = 1)] public struct cl_pwdata { public float peakwave; //!< peak wavelength } /*! * 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 } /*! * 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 } /*! * 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 } /*! * union of 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 } }
when run program, got following exception: (sorry in french)
impossible de charger le type 'cl_measdata' à partir de l'assembly 'xxx, version=1.0.0.0, culture=neutral, publickeytoken=null', car il contient un champ objet à l'offset '0' qui n'est pas correctement aligné ou qui est chevauché par un champ non objet.
in english says:
can't load 'cl_measdata' because contains field @ index 0 misaligned or on non object field (approximative translation me;) )
i've found 2 fiels makes problem fields contains struct embed list of float (public float[] data;)
if remove them union seems pass got stack unbalance exception (i think second problem).
but need them !! i've made lot of try playing "unmanagedtype" enum or arraysubtype attribute no result ok.
i'm okay make big post lot of code have remove unnecessary code.
if have idea ?
Comments
Post a Comment