tags:

views:

41

answers:

2

I have a c# .net 2.0 CF application that interfaces with a native DLL implementing a function like this:

struct NATIVE_METHOD_REPLY
{
    int other_irrelevant_data;
    int data_size;
    void* data;
}

// reply_buffer will contain an array of NATIVE_METHOD_REPLY structures
// and their data.
//
// returns an error code
int Foo(NATIVE_METHOD_REPLY* reply_buffer, int reply_size);

I've implemented it in C# like this:

[StructLayout(LayoutKind.Sequential)]
internal struct NATIVE_METHOD_REPLY
{
    public Int32 OtherIrrelevantData;
    public Int16 DataSize;
    public IntPtr DataPtr;
}

[DllImport("my_lib.dll", SetLastError = true)]
internal static extern Int32 Foo(byte[] replyBuffer, Int32 replySize);

public byte[] void Bar()
{
    // data returned to the user. May be an arbitrary size.
    byte[] result_buffer = new byte[256];

    // data sent to Foo() 
    byte[] reply_buffer = 
        new byte[Marshal.SizeOf(typeof(NativeMethods.NATIVE_METHOD_REPLY)) + 
            result_buffer.Length];

    NativeMethods.Foo(reply_buffer, reply_buffer.Length);

    // is there a better way of doing this?

    NativeMethods.NATIVE_METHOD_REPLY reply;
    GCHandle pinned_reply = GCHandle.Alloc(reply_buffer, 
        GCHandleType.Pinned);
    try
    {
        reply = (NativeMethods.NATIVE_METHOD_REPLY)Marshal.PtrToStructure(
            pinned_reply.AddrOfPinnedObject(), 
            typeof(NativeMethods.NATIVE_METHOD_REPLY));

        Marshal.Copy(reply.DataPtr, result_buffer, 0, reply.DataSize);
    }
    finally
    {
        pinned_reply.Free();
    }

    // bonus point*: is this okay to do after the Free() call?
    int test = reply.OtherIrrelevantData;

    return result_buffer;
}

While this works correctly, I would like to know if this is the most efficient / most correct way of implementing this function.

Is there some method converting a managed byte array to a managed structure that doesn't involve an intermediate native handle and a copy? For instance, in C++, I would just do this:

NATIVE_METHOD_REPLY* reply = reinterpret_cast< NATIVE_METHOD_REPLY* >( reply.DataPtr );

*For a bonus point, is it okay to use data in the structure after the native handle has been freed?

Thanks, PaulH


Edit: Updated solution

[DllImport("my_lib.dll", SetLastError = true)]
internal static extern Int32 Foo(IntPtr replyBuffer, Int32 replySize);

public byte[] void Bar()
{
    byte[] result_buffer = new byte[256];

    int reply_buffer_len = Marshal.SizeOf(typeof(NativeMethods.NATIVE_METHOD_REPLY)) + result_buffer.Length;
    IntPtr reply_buffer = Marshal.AllocCoTaskMem(reply_buffer_len);
    NativeMethods.NATIVE_METHOD_REPLY reply;

    try        
    {
        NativeMethods.Foo(reply_buffer, reply_buffer_len);

        reply = (NativeMethods.NATIVE_METHOD_REPLY)Marshal.PtrToStructure(
            reply_buffer, 
            typeof(NativeMethods.NATIVE_METHOD_REPLY));

        Marshal.Copy(reply.DataPtr, result_buffer, 0, reply.DataSize);
    }
    finally
    {
        Marshal.FreeCoTaskMem(reply_buffer);
    }

    return result_buffer;
}
A: 

In C# under the full framework, you can marshal the array directly. See Default Marshaling for Arrays. I don't know what the limitations are on the Compact Framework.

Jim Mischel
Doesn't that require me to know the size of the reply buffer at compile time? e.g. `[MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]`
PaulH
No, you don't have to know the size at compile time. See http://stackoverflow.com/questions/729514/how-to-marshall-array-of-structs-in-c. Just remember that the `reply_size` you pass to the method will be the size, in bytes, of your array, rather than the number of items in it.
Jim Mischel
@Jim Mischel - Okay... But how does this help me to convert that byte array to a structure without needing an intermediate native handle? Also, my array is a byte array so the number of elements in it and the size of the array should be the same thing, right?
PaulH
Are you trying to get one NATIVE_METHOD_REPLY structure from the call, or an array of NATIVE_METHOD_REPLY structures? If the former, then change your comment that says "reply_buffer will contain an array of NATIVE_METHOD_REPLY structures"
Jim Mischel
A: 

The structure has a fixed size. There's no point in passing an array, just pass the structure:

[DllImport("my_lib.dll", SetLastError = true)]
internal static extern Int32 Foo(out NATIVE_METHOD_REPLY replyBuffer, Int32 replySize);

You do have a memory management problem. Who owns the pointer?


Okay, the structure is actually variable sized and the pointer points into the array. You need nother approach. Simply allocate a chunk of unmanaged memory up front instead of letting the P/Invoke marshaller copy the data into a managed array. Which is in fact a hard requirement since the garbage collector can move the array, invalidating the pointer. Call Marshal.CoTaskMemAlloc() to reserve the memory, you'll have to free it later. And change the first argument of the function to IntPtr (not out).

You'll find that marshaling the structure a lot easier too, no need to pin the memory. Don't forget to Marshal.FreeCoTaskMem() when you're done.

Hans Passant
If I switched passing the structure, how would I allocate space for that structure? Also, can you explain the pointer ownership issue more?
PaulH
The P/Invoke marshaller allocates the space, then copies the structure into the argument you pass. There's a pointer in that structure, presumably the unmanaged code sets it to a chunk of memory that it allocated from the heap. Who deallocates it?
Hans Passant
Oh, I see. The structure sits at the top of a large byte array. The pointer in the structure points to some area after the end of the structure, but before the end of the byte array. That's why the byte array is declared to be the length of the structure + some additional data.
PaulH
Then my advice is not accurate and what you've got is as pretty as it is going to get.
Hans Passant
@Hans Passant - Okay. Thanks for the help. Any comment on the bonus point question?
PaulH
@Paul - actually there's a critical bug in the code, the garbage collector is going to ruin your day sooner or later. Post updated with a solution.
Hans Passant
@Hans Passant - "critical bug" sounds scary. Updated solution posted. What do you see?
PaulH
@Paul - looks good.
Hans Passant