Pass a C# struct with string to a C++ application -


i have c# struct pass via udp c++ application. checked length of string field , got different results c# , c++ codes. here definitions of structs:

//  c# side //------------- [structlayout(layoutkind.sequential, pack = 1)] public struct stname {  /// data types  [marshalas(unmanagedtype.byvaltstr, sizeconst = 20)]  string stringname; }  // c side //--------- struct stname{ /// data types char stringname[20]; 

when stringname = "192.168.53.2" size of field @ c# side 12 while 20 on c side. doing wrong? if c# uses actual length of string, field sizeconst stand for?

when check length of string in c#, counts number of characters (192.168.53.2 is, correctly, 12 characters long).

however, in c, string array of characters null-terminated. however, size of array in case set to, 20. size of "field" 20.

the sizeconst field refers size of c array, interop can create c array appropriately (with correct total size).


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -