marshalling

Packaging IDispatch Invoke with Parameters in C# (with DISPPARAMS)

Hi, I'm using Invoke for late binding on a legacy COM objects that supports IDispatch. This seems necessary as .NET's Type.GetMethod Type.InvokeMember do not seem to work on these objects. The following code works fine for getting a property from an object, the caller passes in the property name as a string for getting the property va...

JAXB Work-Flow - How Do You Jump into Using JAXB

The JAXB documentation is like a text-book, and I simply don't have to time to learn everything JAXB before I need to use it. I have an XSD, if I want to use JAXB to marshal and un-marshal what is the workflow? I don't need any specifics just a high level view. What I know already: 1. JAXB can be used to take objects and create XML do...

JAXB: how to tell whether a class is Marshalable without annotations

We have a class, with no control over the source, so no way to annotate it for JAXB. We also have a framework to take care of marshaling. Is there any way for this framework to ask that class whether it is marshallable when no annotations are present? ...

How can you Marshal a byte array in C#?

I'm trying to call the following C++ function that's wrapped up into a DLL: unsigned char * rectifyImage(unsigned char *pimg, int rows, int cols) My import statement looks like the following: [DllImport("mex_rectify_image.dll")] unsafe public static extern IntPtr rectifyImage( byte[] data, int rows, int columns); And my call routi...

Passing NON null-terminated strings to unmanaged code

Consider the following struct to be sent over TCP to an unmanaged dll [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct FooMessage { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 42)] public string foo; //More fields... } Using the following function (credit to Cheeso): public byte[]...

Unmanaged C code in C# Marshalling by ref string array!

Hi All, I am having a really hard time getting this marshalling down. I have umanaged code that looks like this: WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num) Just FYI I did not write this unmanaged code but must use it. Returns: WORD indicating an error. DevNames: Pointer to an array of char arrays. Basic...

ActiveRecord marshal dump/load has a different behavior on boolean [rails]

Step 1: [setup] a = Aritcle.new(:some_boolean => false) a.save write_to_memcache(a.id, Marshal.dump(a)) b = Marshal.load(read_from_memcache(a.id)) If I do: b.some_boolean = nil b.save the some_boolean is not written back to the DB. DB still remains false. But: a.some_boolean = nil a.save works perfectly. DB is set to NULL. A...

C++ to C# array declaration

I want to convert the below code to C#: struct Elf32_Ehdr { uint8 e_ident[16]; // Magic number and other info uint16 e_type; // Object file type uint16 e_machine; // Architecture uint32 e_version; // Object file version uint32 e_entry; // Entry point virtual address uint32 e_phoff; // Prog...

C# Encounter a problem when convert byte[] to struct. The byte order is wrong.

I was trying to using struct to parse socket data when implement a UDP based protocol. And I searched and I can use these 2 functions to convert between byte[] and struct: byte[] StructToBytes(object structObj) { int size = Marshal.SizeOf(structObj); IntPtr buffer = Marshal.AllocHGlobal(size); try { ...

How is marshalling performed when C++ code is called from C++/CLI?

According to this question it's possible to seamlessly combine managed and unmanaged code using C++/CLI. I don't quite get it - shouldn't there be marshalling between managed and unmanaged anyway? For example, I have InnerLibrary that is compiled as a native C++ .dll with a header published and C++/CLI OuterLibrary that calls code from ...

Interop question about StringBuilder.

I am calling a C# method from C code. The C# method: [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void p_func(StringBuilder arg); public static void callback(StringBuilder arg) { Console.WriteLine(arg.ToString()); } The C method: extern "C" void c_method(p_func f) { char msg[4]; ...

Should Marshal.FreeHGlobal be placed in a finally block to ensure resources are disposed?

I have the following block of code: IntPtr unmanagedPointer = Marshal.AllocHGlobal(buffer.Length); Marshal.Copy(buffer, 0, unmanagedPointer, buffer.Length); SomeCommandThatCanThrowAnException(); Marshal.FreeHGlobal(unmanagedPointer); Should the block be wrapped in a try, and the FreeHGlobal command be placed in a finally block. (In ca...

How to import an XML schema into the "no namespace"

I have a schema here where I am trying to include/import another schema that has no namespace (and this cannot be changed because it comes from another vendor and it would no longer validate their XML). Here is the first Schema: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:samp="http://sample/namespace" targetNamespace...

JiBX: How to run codegen on a schema that imports into the no-namespace schema

I have a schema here where I am trying to include/import another schema that has no namespace (and this cannot be changed because it comes from another vendor and it would no longer validate their XML). Here is the first Schema: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:samp="http://sample/namespace" targetNamespace...

XML Marshalling: How to add an attribute from another namespace to an element

I want to generate this XML: <myElement myAttribute="whateverstring" xsi:type="hardPart"/> I have this XSD: <xsd:element name="myElement"> <xsd:complexType> <xsd:attribute name="myAttribute" type="xsd:boolean" /> <!-- need to add the xsi:attribue here --> </xsd:complexType> </xsd:element> How exactly can I a...

Marshalling of C structure to C#

Please bear with me as i am new to marshalling. I have a C structure and function declared as the following: typedef struct { char* name; BT_ADDR address; } DeviceList; extern "C" _declspec(dllexport)DeviceList* PerformQuery(); The BT_ADDR structure is the same structure defined in wsbth2.h in Win CE SDK. PerformQuery return...

Does a serializable object always get serialized when put in a bundle?

We were wondering if when using Bundle with serializable or parcelable objects, when does the marshalling actually happen? As soon as you put it in the bundle? Since bundles are mostly used to simply pass around data between two screens (we're not even talking about IPC here!), there doesn't seem to be much point in marshalling an object...

IntPtr To String conversion in Windows messages

I'm getting in trouble by hooking window messages. I need to detect window text (caption) changes, so I intercept the WM_SETTEXT message for the interesting windows (I do so because at window creation the window caption is not specified). Reading the documentation of the WM_SETTEXT documentation, the lParam parameter specify a pointer t...

Access violation on marshaling

Hi, right function declaration is: [DllImport("user32.dll")] static extern int SetScrollInfo (IntPtr hwnd, int n, ref SCROLLINFO lpcScrollInfo, bool b); I declared it like: [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] static extern int SetScrollInfo (IntPtr hwnd, int n, SCROLLINFO lpcScrollInfo, bool b); c...

Jaxb marshaller ang generics(2)

there are types: class A{} @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) @XmlType(propOrder = {"obj"}) @XmlRootElement(name = "response") public class B<T extends A> extends A{ private T obj; @XmlElement(required = true) public T getObj() { return obj; } } When i'm trying to marshal this i get an error: org.springframew...