views:

30

answers:

2

I have an unmanaged (C/C++) DLL which I need to call from a C# application.

The DLL call needs to return data (from a C++ class that is created by the DLL) to the C# application.

I have control over the DLL's exported function, so I can create a wrapper around the C++ class if necessary. I know how to access the DLL with p/Invoke, so I'm just looking for some guidance on how to return the data.

The data that is received from the DLL is a series of key-value pairs, so I was thinking about returning a JSON string and deserializing it to an object with Json.NET.

What is the preferred way to access this type of data from an unmanaged source from a C#/.NET application?

A: 

There is no real reason to add extra layers of serialization just to transfer code between managed and unmanaged applications. But if it would be simpler for your code and you aren't worried about performance than any way you can move data from one layer to another is perfectly fine.

If you want to try the PInvoke way here are a few resources from MSDN...

Matthew Whited
+1  A: 

If you're able to modify the C++ DLL then C++/CLI could be cleanest. You'll be able to write a set of .NET wrapper classes directly in C++, avoiding the need to write separate P/Invoke declarations.

Tim Robinson