Marshal.PtrToStructure (and back again) and generic solution for endianness swapping

Reflection does seem like the only real way to accomplish what you’re after. I’ve put together some code below. It creates an attribute called EndianAttribute that can be applied at the field level on a struct. I’ve included the definition for this attribute and it’s associated enum, as well as the modifications to your code … Read more

Can JAXB marshal by containment at first then marshal by @XmlIDREF for subsequent references?

You can leverage the concept of JAXB’s XmlAdapter to do something like the following: input.xml The following is the XML document I will use for this example. The 3rd phone-number entry is a reference to the 1st phone-number entry, and the 5th phone-number entry is a reference to the 4th.: <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?> <customer> … Read more

Can I force JAXB not to convert ” into ", for example, when marshalling to XML?

Solution my teammate found: PrintWriter printWriter = new PrintWriter(new FileWriter(xmlFile)); DataWriter dataWriter = new DataWriter(printWriter, “UTF-8”, DumbEscapeHandler.theInstance); marshaller.marshal(request, dataWriter); Instead of passing the xmlFile to marshal(), pass the DataWriter which knows both the encoding and an appropriate escape handler, if any. Note: Since DataWriter and DumbEscapeHandler are both within the com.sun.xml.internal.bind.marshaller package, you must bootstrap … Read more

Marshal C++ struct array into C#

I would try adding some attributes to your struct decloration [StructLayout(LayoutKind.Sequential, Size=TotalBytesInStruct),Serializable] public struct LPRData { /// char[15] [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)] public string data; /// int[15] [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 15)] public int[] prob; } *Note TotalBytesInStruct is not intended to represent a variable JaredPar is also correct that using the IntPtr class could be … Read more

equivalent char* in C#

The C# way of doing this is by letting the marshaler handle the char* stuff while you work with a string: [DllImport(“pjsipDlld”)] static extern int dll_registerAccount( [MarshalAs(UnmanagedType.LPStr)]string username, [MarshalAs(UnmanagedType.LPStr)]string password); Replace LPStr with LPWStr if you’re working with wide chars.