marshalling

C# formatting external Dll function parameters

I have yet to find a good reference on this topic. For this example we will take some C++ code that I'm trying to port over to C#. In C++ land we have the following prototype for an external library function: extern "C" DWORD EXPORT FILES_GetMemoryMapping( PSTR pPathFile, PWORD Size, PSTR MapName, PWORD PacketSize, PMAPPING ...

How to convert an IntPtr back into an object

All, this is a follow up from a previous question here: http://stackoverflow.com/questions/727942/c-formatting-external-dll-function-parameters Here specifically is the code that I am trying to convert to C#: FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pMapping, &PagePerSector); // Allocate the m...

How to marshall array of structs in C#?

I've the following structure in C#: [StructLayoutAttribute(LayoutKind.Sequential)] public struct RECORD { public uint m1; public uint m2; public uint m3; } I need too pass an array (fixed length) of these structs over to native Code, which writes some data to these structures. The array is allocated in C# and passed over t...

AccessViolationException during P/Invoke Help

I keep getting an AccessViolationException when calling the following from an external DLL: FILES_GetMemoryMapping(MapFile, out size, MapName, out PacketSize, pMapping, out PagePerSector); Which has a prototype that I've setup as such: [DllImport("Files.DLL", SetLastError = true)] public static extern uint FILES_GetMemoryMapp...

Can Someone explain the Marshal.StructureToPtr

I'm having some trouble with this code: //Creating a new ImageElement Struct ImageElement oElement = new UM0516.ImageElement(); //Create a pointer and allocate enough room for the struct type IntPtr pElement = Marshal.AllocHGlobal(Marshal.SizeOf(new UM0516.ImageElement())); //Copy the contents of the struct into the allocated memory spa...

Converting a C++ .exe project to a dll

Microsoft provides the source code of vshadow to manipulate VSS (Volume Shadow Service [shadow copy]), and I've modified it a bit but I want to make it into a dll so I can use it in my C# projects. I don't know exactly how to go about doing that, the source code is fairly simple, and it shouldn't be too hard, but I don't really know wher...

What is the difference between Serialization and Marshalling?

I know that in terms of several distributed techniques like RPC the term Marshalling is used, but I don't get the difference with Serialization. It both is transforming objects to series of bits no? Related: What is Serialization? What is Object Marshalling? ...

Marshalling reference-types from C++ to C#

I want to invoke the following C++ function (exported by a DLL) from C#: void createVm( const jace::VmLoader& loader, const jace::OptionList& options, bool ignoreUnrecognized = true ); I've found documentation for marshaling primitives from C++ to C# but I'm not sure how to handle reference-types or non-pritmive types such as...

Returning a std::string from a C++ DLL to a c# program -> Invalid Address specified to RtlFreeHeap

Hi, In a function in my C++ DLL, I'm returning a std::string to my c# application. It pretty much looks like this: std::string g_DllName = "MyDLL"; extern "C" THUNDER_API const char* __stdcall GetDLLName() { return g_DllName.c_str(); } But when my C# code calls this function, I get this message in my output window: Invalid Add...

Marshalling C dll code into C#

I have the following C-code signature in a dll: extern __declspec(dllexport) unsigned char * funct_name (int *w, int *h, char **enc, int len, unsigned char *text, int *lp, int *mp, int *ep) The C function can modify w, h, enc, lp, mp, and ep (though the latter three can be null and it won't do anything. I'm using the following in C# ...

Marshalling and casting. Are them related?

Last week a young student ask me if marshalling is the same as casting. My answer was definetly no. Marshalling is seralization, the way to transform a memory representation of an objet into bytes in order to be transmitted to a network whereas casting is related to type convertion / coercion. Later on, rethinking the question I was tho...

What is required to enable marshaling for a COM interface?

I have a 32-bit ATL COM component without a type library. It has a class factory for one given class that implements several interfaces. When I use it as an in-proc server, everything works fine - the client side invokes CoCreateInstance(), the object is instantiated and QueryInterface() retrieves a pointer to a requested interface. But...

WCHAR array not properly marshalled

I have a COM interface with a following method definition (IDL notation): SCODE GetText( [in, out] ULONG* pcwcBuffer, [out, size_is(*pcwcBuffer)] WCHAR* awcBuffer ); Typelib marshaling is used for COM+, the type library is registered, other methods of the interface work allright when called through COM+, but not this met...

JAXB Marshalling with null fields.

Hello, This is a pretty simple request, but I just didn't find a way to do it. I'm basically trying to set up a role in JAXB which says that whenever an null field is encountered, instead of ignoring it in the output, set it to an empty value. So for the class : @XMLRootElement Class Foo { Integer num; Date date; …. } When thi...

What's the most efficient way to marshal C++ structs to C#?

I am about to begin reading tons of binary files, each with 1000 or more records. New files are added constantly so I'm writing a Windows service to monitor the directories and process new files as they are received. The files were created with a c++ program. I've recreated the struct definitions in c# and can read the data fine, but I'm...

IJW: managed proxy struct?

I am working on porting some C++ code to managed .NET. I will need to retain some C++ code in native form, and trying to use an IJW approach to it. I know it's possible to declare an unmanaged struct so that it will get correctly marshaled to .NET code, but C++ compiler doesn't seem to do it. For example I have this code (managed): str...

Marshaling arrays from VB.NET to COM object

Hi, I have a VB6 program which calls a COM method, passing 2 arrays as parameters and expecting 2 arrays to be populated in response. The code is this, where ItemIDs and ItemClientHandles are the input array parameters and MyItemServerHandles and Errors are populated by the COM object. Dim ItemIDs(2) As String Dim ItemClientHandles(2)...

Does .net interop do cross-thread marshalling for COM or do I need to use CoMarshalInterThreadInterfaceInStream or similar?

Can anyone give me a definitive answer as to whether I need to use something like CoMarshalInterThreadInterfaceInStream, CoGetInterfaceAndReleaseStream or GlobalInterfaceTable to marshall a COM interface between threads? In this thread some say interop does it for you, some say you need to use these calls. Which is it? Specifically I'm ...

How best to share logical types between .NET server code and JavaScript client code?

I have an ASP.NET MVC server app and client code which uses jquery. I'd like to use ajax to transfer objects from client to server and back. On the server, I'd basically just be validating and serializing them. On the client, I have some UI code to create a visual representation and some other code to manage some object properties. M...

From C++ wchar_t to C# char via socket

I am currently building a C++ application that communicate via socket to a C# application. My C++ app sends wchar_t* via socket. Here is an overview of what is send : <!-- Normal xml file-- Here is what I receive on the other side (I do a stream.read to a byte array and use UTF8Encoding.GetString() to convert the byte array to a read...