Passing a vector/array from unmanaged C++ to C#

As long as the managed code does not resize the vector, you can access the buffer and pass it as a pointer with (for C++0x) or &vector[0]. This results in a zero-copy system. Example C++ API: #define EXPORT extern “C” __declspec(dllexport) typedef intptr_t ItemListHandle; EXPORT bool GenerateItems(ItemListHandle* hItems, double** itemsFound, int* itemCount) { auto … Read more

Marshal “char *” in C#

OregonGhost’s answer is only correct if the char* returned from GetDir is either allocated in HGlobal or LocalAlloc. I can’t remember which one but the CLR will assume that any string return type from a PInvoke function was allocated with one or the other. A more robust way is to type the return of GetDir … Read more

Why are date/time values interpreted incorrectly when patching/saving?

Date/time values are being casted/parsed in a locale aware fashion Update: this is the default behavior with the CakePHP application template versions prior to 3.2.5. As of 3.2.5 locale parsing is not enabled by default anymore, which will make the date/time marshalling logic expect a default format of Y-m-d H:i:s instead. In the marshalling process, … Read more

JAXB marshalling XMPP stanzas

How about the following?: Create a custom XMLStreamWriter that will treat all namespace declarations as default namespaces, and then marshal to that: ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory xof = XMLOutputFactory.newFactory(); XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out); xsw = new MyXMLStreamWriter(xsw); m.marshal(iq, xsw); xsw.close(); MyXMLStreamWriter import java.util.Iterator; import javax.xml.namespace.NamespaceContext; import; import; public class MyXMLStreamWriter implements … 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

How do you specify the date format used when JAXB marshals xsd:dateTime?

You can use an XmlAdapter to customize how a date type is written to XML. package com.example; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateAdapter extends XmlAdapter<String, Date> { private final SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); @Override public String marshal(Date v) throws Exception { synchronized (dateFormat) { return dateFormat.format(v); } } @Override public … Read more

C++ .NET convert System::String to std::string

There is cleaner syntax if you’re using a recent version of .net #include “stdafx.h” #include <string> #include <msclr\marshal_cppstd.h> using namespace System; int main(array<System::String ^> ^args) { System::String^ managedString = “test”; msclr::interop::marshal_context context; std::string standardString = context.marshal_as<std::string>(managedString); return 0; } This also gives you better clean-up in the face of exceptions. There is an msdn article … Read more