Using a C++ dll from C# - not getting expected results -


int jpeg_codec_api jpeg_decompress(unsigned char *jpeg_buf,                                 unsigned long jpeg_len,                                 eimage_s** * img);   * @param jpeg_buf [in]     jpeg buffer * @param jpeg_len [in]     jpeg buffer length * @param img [out]     decompress data      internal struct eimage_s     {         public byte[] imagedata;         public int image_size;         public int width;         public int height;         public int widthstep;     }; 

i declared this;

[dllimport("jpeglib.dll", callingconvention = callingconvention.cdecl)] internal static extern int jpeg_decompress(byte[] jpeg_buf, uint jpeg_len, ref intptr img);  eimage_s obj = (eimage_s)marshal.ptrtostructure(ptr, typeof(eimage_s)); 

when call ptrtostructure, give error "managed debugging assistant 'fatalexecutionengineerror' has detected problem".

i'm not sure wrong!

you can, , should, remove unsafe keyword. not necessary here.

the first parameter declared incorrectly in p/invoke. should be:

byte[] jpeg_buf 

using ref managed type never correct in p/invoke. there's no way native function able create .net managed type.

the final parameter incorrect too. is, believe, impossible sure how parameter should declared , managed, information present. need show image_s is, , explain semantics of parameter. allocates memory? how allocated? array? if not, it. example c++ code calls function help.


your update removes code on above answer based. struct show cannot marshalled because of array. that's runtime error about.

i cannot tell how marshal struct because you've not described native side of interface.

to solve problem need understand native side. cannot expect make progress without doing so. said above, example of native code calling function make clear.

finally, why using library rather built in managed classes?


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -