marshalling

Reading a C/C++ data structure in C# from a byte array

What would be the best way to fill a C# struct from a byte[] array where the data was from a C/C++ struct? The C struct would look something like this (my C is very rusty): typedef OldStuff { CHAR Name[8]; UInt32 User; CHAR Location[8]; UInt32 TimeStamp; UInt32 Sequence; CHAR Tracking[16]; CHAR Filler[12];} And wo...

How to marshal an array of structs - (.Net/C# => C++)

Disclaimer: Near zero with marshalling concepts.. I have a struct B that contains a string + an array of structs C. I need to send this across the giant interop chasm to a COM - C++ consumer. What are the right set of attributes I need to decorate my struct definition ? [ComVisible (true)] [StructLayout(LayoutKind.Sequential)] public...

Exposing nested arrays to COM from .NET

I have a method in .NET (C#) which returns string[][]. When using RegAsm or TlbExp (from the .NET 2.0 SDK) to create a COM type library for the containing assembly, I get the following warning: WARNING: There is no marshaling support for nested arrays. This warning results in the method in question not being exported into the gener...

Converting std::vector<>::iterator to .NET interface in C++/CLI

I am wrapping a native C++ class, which has the following methods: class Native { public: class Local { std::string m_Str; int m_Int; }; typedef std::vector<Local> LocalVec; typedef LocalVec::iterator LocalIter; LocalIter BeginLocals(); LocalIter EndLocals(); private: LocalV...

What is the most efficient way to handle the lifecycle of an object with COM interop?

I have a Windows Workflow application that uses classes I've written for COM automation. I'm opening Word and Excel from my classes using COM. I'm currently implementing IDisposable in my COM helper and using Marshal.ReleaseComObject(). However, if my Workflow fails, the Dispose() method isn't being called and the Word or Excel handles ...

What is object marshalling?

I have heard this concept used frequently, but I don't have a really good grasp of what it is. ...

Marshal C++ "string" class in C# P/Invoke

I have a function in a native DLL defined as follows: #include <string> void SetPath(string path); I tried to put this in Microsoft's P/Invoke Interop Assistant, but it chokes on the "string" class (which I think is from MFC?). I have tried marshaling it as a variety of different types (C# String, char[], byte[]) but every time I eit...

Runtime callable wrapper (RCW) scope - process or application domain?

What is the scope of Runtime Callable Wrapper (RCW), when referencing unmanaged COM objects? According to the docs: The runtime creates exactly one RCW for each COM object, regardless of the number of references that exist on that object. If I had to "guess" - this explanation should mean "one per process", but is it really? ...

Marshal "char *" in C#

Given the following C function in a DLL: char * GetDir(char* path ); How would you P/Invoke this function into C# and marshal the char * properly. .NET seems to know how to do LPCTSTR but when I can't figure out any marshaling that doesn't cause a NotSupportedException to fire when calling this function. ...

Marshal C++ struct array into C#

I have the following struct in C++: #define MAXCHARS 15 typedef struct { char data[MAXCHARS]; int prob[MAXCHARS]; } LPRData; And a function that I'm p/invoking into to get an array of 3 of these structures: void GetData(LPRData *data); In C++ I would just do something like this: LPRData *Results; Results = (LPRData *)mal...

Unmanaged C++ encrypted string into C# byte[]

I have an unmanaged C dll I call from a C# class library that encrypts a string value into an encrypted string that contains non-ascii characters. I need to take the data and write its binary values to a file but C# treats text as strings rather than a byte[]. The encrypted value commonly contains special characters (\r, \O, etc). Whe...

MarshalAsAttribute Sizeconst .NET

Hi Using C#, does anyone know how to get the MarshalAsAttribute's Sizeconst value in runtime ? Eg. I would like to retrieve the value of 10. [StructLayout[LayoutKind.Sequential, Pack=1] Class StructureToMarshalFrom { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] public byte[] _value1; } Thanks in advance ...

Can I create a unit test for a method which marshalls a value from an IntPtr?

I am using a native library which returns an IntPtr to a memory location, which contains the value of an attribute. I know what the type of the attribute is and I have a method for marshalling a value (taking the IntPtr and the type of the attribute) from the memory pointed at by the pointer. This method either calls Marshal.ReadInt32,...

getting an Int/short/byte structure byte representation with C#

Given a FieldInfo object and an object, I need to get the actual bytes representation of the field. I know that the field is either int,Int32,uint,short etc. How can I get the actual byte representation? BinaryFormatter.Serialize won't help, since it'll give me more information than I need (it also records type name etc.). The Marshal c...

vb.net byte[] to C++ char*

I am calling an unmanaged C++ dll that expects a char* as one of its parameters and I want to push a byte[] into it. The project is written in VB.NET. What type of marshalling will work for this? ...

Namespaces unmarshaling problem with Jaxb on Spring WS

Hi all, I am using JAXB1 (v1.0.6) for marshaling/unmarshaling on Spring WS 1.0.4 and JDK 1.4.2. Everything was working well until I have tried to add namespaces to the original XSD files from which I have generated java classes by JAXB, so that the system could accept requests with namespaces. Now, when I send a requst to my system, I...

Why is marshal so much faster than pickle?

From this question and my own benchmarks it seems that the marshal module is about 20-30x faster than cPickle. Why is this so? What functionality does cPickle offer over marshal that justifies this? (Another way of putting it - why not always use marshal? Why do both of these modules exist?) ...

C++ CLI structure to byte array

I have a structure that represents a wire format packet. In this structure is an array of other structures. I have generic code that handles this very nicely for most cases but this array of structures case is throwing the marshaller for a loop. Unsafe code is a no go since I can't get a pointer to a struct with an array (argh!). I c...

Interop'ing between C# and an unmanaged C library

I have a small C library in a DLL and I need to call a handful of its methods. It uses pointers and a few structs but is otherwise quite simple. Problem is I'm not terribly knowledgable on .NET's interop with the unmanaged world and my attempts so far keep hitting memory access violation exceptions (presumably due to me not getting the ...

Helper functions for marshalling arrays of structures (with pointers)

This appears to be the most commonly asked C# interop question and yet seems to be difficult to find a working solution for. I am in need of allocating an array of matrix datastructure in C# passing it to a C DLL which fills up the data and returns it to the caller to deal with. Based on various pages on the web, I seem to have managed...