Passing Array of Bool to C++ Code from C# -
i'm working on project few other people, sadly no longer available reference material. project uses c# gui, , c++ updating our archaic database. @ moment, have function in c#:
[dllimport("synch.dll", charset = charset.unicode, callingconvention = callingconvention.cdecl)] //synch bool synch(void * voidptr, const tchar * databasedir, const tchar * truckdir) static extern int32 synch(intptr psync, string databasedir, string destdir, ref bool[] wholefiles);
wholefiles array of booleans used determine if files should synced or not, based on user input.
the c++ supposed take in array, , able flip of bools true or false conditions met.
if (numsyncrecords > 0){ log::writeverbose(_t(" %d recs, %d differences found"), (int) numrecsused, (int) numsynrecords); if(wholefiles[sourcefileid]){ cstring temppath = _t(""); temppath += destdir; temppath += filename; deletefile(temppath); } else wholefiles[sourcefileid] = false; } else wholefiles[sourcefileid] = false;
in case above, pass in trues, , if there no changes file, set false avoid processing file doesn't need it.
now, issue is, when c++ code finished, there still true marks when should set false. if send in ref, stack overflow. without ref, array doesn't seem change.
edit: requested.
sync_api int32 synch(void * voidptr, const tchar * databasedir, const tchar * truckdir, bool wholefiles[])
edit2: have, in c++ code, log statement loops on array , outputs true/false once per line per position in wholefiles array, , end appropriate log output, when @ array on c# code after done, it's unchanged.
if comment below dllimport
exact function declaration on c++ side, wholefiles
array never seen on c++ side because it's not expecting 5th argument. make sure fix issue first.
in c# there's 1 size of bool
, , 1 byte. in c++ there 2 types, c-style 1-byte bool
, win32 4-byte bool
. by default p/invoke marshal bools bool
.
so if first bit isn't entire issue , it's not working, using c-style bool
, need tell c# [marshalas] this:
[dllimport("synch.dll", charset = charset.unicode, callingconvention = callingconvention.cdecl)] static extern int32 synch( intptr psync, string databasedir, string destdir, [marshalas(unmanagedtype.lparray, arraysubtype=unmanagedtype.i1)] bool[] wholefiles);
you don't need ref
because array reference type in itself. ref
array useful if function calling wants swap array one.
also note don't have make multiline, attribute work if function in 1 line, did readability here.
Comments
Post a Comment