Marshalling C++ .dll in C# -


i developing c# application uses dll written in c++. able call of functions in .dll c# application, 1 function cause problem. think marshalling types.

this c++ prototype of function:

int getprogressinfo(     int *state,      double *progress,      char* stateinfo,      int infostringmaxlen ); 

this c# delegate:

delegate int get_progress_info(     ref int state,      ref double progress,      [marshalas(unmanagedtype.lpstr)]      string stateinfo,      int infostringmaxlen ); 

can please me? can not find wrong

one important lesson must learn signature of function not enough information specify semantics function. in case, c++ int* address of single int, or array. likewise double*. , char* string being passed function, or pointer buffer in function returns string caller.

i'm going take best guess, need check actual semantics.

my best guess function returns 3 things caller: int state int, double progress value, , string giving info on state. latter information encoded in final 2 parameters. caller allocates buffer , passes pointer beginning, length. c# should this:

delegate int get_progress_info(     out int state,      out double progress,      stringbuilder stateinfo,      int infostringmaxlen ); 

i'm assuming using default charset of charset.ansi, , third parameter not need marshalas attribute.

call function this:

int state; double progress; stringbuilder stateinfosb = new stringbuilder(256); int retval = getprogressinfo(     out state,      out progress,      stateinfosb,      stateinfosb.capacity ); if (retval == 0) // guessing 0 means ok {     string stateinfo = stateinfosb.tostring();     .... } 

another possible cause mismatch calling convention. written, there no calling conventions specified. default c++ code cdecl, default c# stdcall. need check calling convention used c++ , make sure c# code matches.

and once again, need check documentation guesswork above accurate.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -