1

I am having trouble marshalling data between a C# application and an existing C++ DLL. The caveats that are making this difficult are that it is an unsigned char pointer to an array, and I need access to the data (from all fields) after the call in C#. I would like to avoid using unsafe code, if at all possible.

Here is the C++ signature:

BYTE GetData(unsigned char *Data_Type, unsigned char *Data_Content, unsigned int *Data_Length);

I've tried a bunch of things in C#, but here's what I have now:

[DllImport("somecpp.dll")]
public static extern byte GetData([In][Out] ref byte Data_Type, [In][Out] ref byte[] Data_Content, [In][Out] ref int Data_Length);

Then calling it, I am attempting this:

byte retrievedData = GetData(ref data_type, ref data_content, ref data_length);

This is definitely not working, and I'm not sure what to try next. Any ideas? Thanks!

1
  • 1
    uint would be the correct type for Data_Length, but I don't know why that would cause this problem. As always when talking about things not working, please be specific, are you receiving an error message if so what is the message. Commented Jan 22, 2015 at 19:58

1 Answer 1

2

Your ref byte[] parameter matches unsigned char**. That's one level of indirection too many.

The p/invoke should be

[DllImport("somecpp.dll")]
public static extern byte GetData(
    ref byte Data_Type,
    [In,Out] byte[] Data_Content,
    ref uint Data_Length
);

It is plausible that the function uses cdecl. We can't tell that from here.

I also suspect that the Data_Type argument should be out rather than ref.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.