Call a C++ DLL from a C# application -


i'm trying integrate c++ dll our c# application, i'm not able identify what's correct way call 1 of methods. in 2 different places of documentation method definition not equal:

imageandscanerror getmicrinfo(unsigned char *ptrcodeline,int* ilength)   imageandscanerror winapi getmicrinfo(char* cmicrinfo,int* iinfolength);  /* imageandscanerror getmicrinfo(unsigned char *ptrcodeline,int* ilength)   parameters:   ptrcodeline: pointer output buffer receive code line read micr algorithm. ptrcodeline should allocate room 96 characters.   ilength: number of characters contained in code line   function: read micr line on check. function must called after startscan .   returns: errornone returned upon success. otherwise, enum imageandscanerror value indicates reason failure returned.  */ 

this how i'm including dll method

[dllimport("scandll.dll", callingconvention = callingconvention.winapi)] 

and combinations i've made far

public static extern imageandscanerror getmicrinfo(out intptr cmicrinfo, out int iinfolength); public static extern imageandscanerror getmicrinfo(out byte[] cmicrinfo, out int iinfolength); public static extern imageandscanerror getmicrinfo(out string cmicrinfo, out int iinfolength); public static extern imageandscanerror getmicrinfo(out stringbuilder cmicrinfo, out int iinfolength);  intptr cmicrinfotmp; byte[] cmicrinfotmp= new byte[96]; string cmicrinfotmp; stringbuilder cmicrinfotmp;  getmicrinfo(out cmicrinfotmp, out iinfolengthtmp); 

when use intptr, value debug gives me in vs2010 859256727 size of 4, , when do

string mystring = marshal.ptrtostringansi(cmicrinfotmp); 

i empty string.

when try of others (byte[], string, stringbuilder) get

the runtime has encountered fatal error. address of error @ 0x53e6716a, on thread 0x1084. error code 0xc0000005. error may bug in clr or in unsafe or non-verifiable portions of user code. common sources of bug include user marshaling errors com-interop or pinvoke, may corrupt stack. 

what missing here? thanks

you can allocate buffer, pass native function.

//error handling omitted [dllimport("your.dll", charset = charset.ansi)] imageandscanerror getmicrinfo(intptr ptrcodeline,ref int bytescopied);  intptr ip = marshal.alloccotaskmem(bufferlen); win32api.zeromemory(ip, (uint)(bufferlen)); int bytescopied=0; getmicrinfo(ip, ref bytescopied); string info= marshal.ptrtostringansi(bytescopied); marshal.freecotaskmem(ip); 

if not need reuse buffer during multiple calls of getmicrinfo, can use default marshaler stringbuilder:

[dllimport("your.dll", charset = charset.ansi)] imageandscanerror getmicrinfo(stringbuilder ptrcodeline,ref int bytescopied); stringbuilder ptrcodeline(bufferlen); int bytescopied=0; getmicrinfo(ptrcodeline, ref bytescopied); 

it comes performance hit if call getmicrinfo multiple times, because on each call default clr marshaller creates marshalling buffer pinning , unicode-ansi conversion. hit may negligible if function isn't being called or not return lot of data.

reference:


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -