0

I have below function on C++ (header)

string               __declspec(dllexport) *GetReaders(int& readerCount);

I wrote below method on C# for invoking

[DllImport("ABC.dll", CharSet = CharSet.Auto )]
        extern static string[] GetReaders(out IntPtr readercount);

But after run I can got readercount but the app got below error:

Cannot marshal 'return value': Invalid managed/unmanaged type combination.

What is wrong?

I did it by java and work perfectly. But I have problem with .Net.

2
  • Marshalling is almost never trivial but marshalling strings is always guaranteed to be complicated. Is that a std::string array that you're returning from your method? How's that allocated? Commented Jan 13, 2015 at 21:13
  • 1
    99% chance you have a memory leak Commented Jan 14, 2015 at 5:53

1 Answer 1

2

It's impossible to call that method with p/invoke. That's because you cannot marshal C++ classes using p/invoke. And string is, presumably, std::string.

You'll need to either use a C++/CLI wrapper, or re-design the C++ interface to be p/invoke friendly.

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.