   [C#] Using struct with unmanaged string
  Hello!I need to use a struct in unsafe context. To make sure it has a constant size i marked strings and StringBuilders with [MarshalAs(UnmanagedType.LPStr)]. Now the problem is that i have a byte* to that struct and a normal string which should be placed at the "unmanaged"-string. Something like that:byte* myStruct = (byte*)GCHandle.ToIntPtr(handle).ToPointer();// i would like to do something like:*(string**)(myStruct + SOMEVAL) = &thisString;So basically the question is:how can i get my managed string into that field that is marked as Unmanaged LPStr with a pointer to the struct?GreetingsPlerion   
  I am not sure how to coerce marshaling based on the MarshalAs attribute (I think that is used primarily for pinvoke), but you should be able to convert from string to pointer and pointer to string using the variations of the Marshal.PtrToStringXXX and Marshal.StringToPtrXXX methods.Also the way you get your pointer looks odd.  In C# you need to pin objects if you want to get a pointer to them (most of the time anyway).  Typically this is done with the fixed statement or GCHandle's Alloc method.  Use Alloc with GCHandleType of Pinned and then AddrOfPinnedObject to get the pointer to the actual object.Hope this helps   ;  I do a decent amount of C# to C++ interop at work. It is fairly simple if you are only dealing with primitives and strings, but passing structs and pointers quickly becomes a nightmare. What we did is basically made an interop rule: "all functions only pass primitives and strings" and life became much simpler. Not sure if the nature of your interop can support that though. If I were in your place I would do the entire operation on either one side or the other (C++ or C# side). Either make a C++ function that takes a byte to be appended or make a C# function that appends the byte and returns the new string.   ;public unsafe struct my_struct{  int my_field;  fixed byte my_string[32];  fixed int my_unused[4];}Could maybe help?Not that using fixed inside structures forces you to make the structure unsafe for some reason.   