delphi - C# error: cannot use fixed size buffers contained in unfixed expressions -
i'm struggling c# program read binary records database. records created borland delphi. here's example:
// delphi record definition tbowler_rec = record public gender : tgender; bowler_num : byte; name : tstring32; initials : string[3]; ...
// corresponding c# definition (unmanaged code) [structlayout(layoutkind.sequential, pack=4)] public unsafe struct tbowler_rec { public tgender gender; public byte bowler_num; public fixed byte name[32]; public fixed byte initials[3]; ...
i'm able read binary struct out of sql server database , see data in visual studio debugger. yay! i'm able access fields "gender" , "bowler_num" no problem. yay!
q: how turn "name" c# string?
an example name "ashton". looks in memory:
\0x6ashton\0x0\0x0...
here's how i'm trying access it:
[structlayout(layoutkind.sequential, pack=4)] public unsafe struct tbowler_rec { public tgender gender; public byte bowler_num; public fixed byte name[32]; ... public string name { { stringbuilder sb = new stringbuilder(); int ilen = name[0]; (int = 1; <= ilen; i++) sb.append(name[i]); return sb.tostring(); } }
i'm getting error:
error: cannot use fixed size buffers contained in unfixed expressions. try using fixed statement.
help?????
thank in advance!
as i'm not familiar delphi can't give straight answer on tstring32
field. seems unmanagedtype.ansibstr.
if case, go this:
[structlayout(layoutkind.sequential, pack=4)] public struct tbowler_rec { public tgender gender; public byte bowler_num; [marshalas(unmanagedtype.ansibstr)] public string name; [marshalas(unmanagedtype.byvalarray, sizeconst = 3)] public char[] initials;
look how i'm doing initials
marshaling. if tstring not ansibstr, better way marshal characters name.
i show i've removed fixed , unsafe instructions struct declaration not necessary trying do.
Comments
Post a Comment