views:

1025

answers:

3

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?

A: 

I'm not a .net expert, but I've needed to do something similar recently.

It is not just a matter of serialization, you also have to stop the garbage collector from cleaning up your byte array while it is being used in C++ land...

The below snippet of C# should help.

// pin the byte[] (byteArray)  
GCHandle handle = GCHandle.Alloc(byteArray, GCHandleType.Pinned);   
IntPtr address = handle.AddrOfPinnedObject();  
// Do your C++ stuff, using the address pointer.

// Cleanup 
handle.Free();
+1  A: 

If you need to pin a managed structure in order to pass it as a parameter you can use the following code.

    // (c) 2007 Marc Clifton
    /// <summary>
    /// A helper class for pinning a managed structure so that it is suitable for
    /// unmanaged calls. A pinned object will not be collected and will not be moved
    /// by the GC until explicitly freed.
    /// </summary>

    internal class PinnedObject<T> : IDisposable where T : struct
    {
        protected T managedObject;
        protected GCHandle handle;
        protected IntPtr ptr;
        protected bool disposed;

        public T ManangedObject
        {
            get
            {
                return (T)handle.Target;
            }
            set
            {
                Marshal.StructureToPtr(value, ptr, false);
            }
        }

        public IntPtr Pointer
        {
            get { return ptr; }
        }

        public int Size
        {
            get { return Marshal.SizeOf(managedObject); }
        }

        public PinnedObject()
        {
            managedObject = new T();
            handle = GCHandle.Alloc(managedObject, GCHandleType.Pinned);
            ptr = handle.AddrOfPinnedObject();
        }

        ~PinnedObject()
        {
            Dispose();
        }

        public void Dispose()
        {
            if (!disposed)
            {
                if (handle.IsAllocated)
                    handle.Free();
                ptr = IntPtr.Zero;
                disposed = true;
            }
        }
    }
}

You can then call the unmanaged code using PinnedObject.Pointer. In your extern declaration, use IntPtr as the Type for that parameter.

PinnedObject<BatteryQueryInformation> pinBatteryQueryInfo = new PinnedObject<BatteryQueryInformation>();
pinBatteryQueryInfo.ManangedObject = _structBatteryQueryInfo;
Unmanaged.Method(pinBatteryQueryInfo.Pointer);
Chris Thompson
A: 

In your PInvoke definition just declare the char* parameter as a byte[] and the standard marshaller will handle work.

But this may or may not be the best idea. Is the C++ function expecting a string or is it expecting a buffer of data (C/C++ code often uses char* for a buffer, relying on the fact that a char is one byte)?

If it is a buffer then a byte[] is certainly correct, but if it expects a string then it may be clearer if you declare the parameter as a string (to be explicit) and use Encoding.ASCII.GetString() to convert the byte[] to a string.

Also if it C++ function expects a string and you decide to declare the parameter as a byte[], be sure the byte array ends with a zero, since that is how C/C++ determines the end of the string.

shf301