Best approach to mixing managed code with unmanaged code

Hi,

I have written a console version of a code in native C++. The code is computationally expensive so speed is an issue. I have developed using Windows Forms in visual c++ 2010 a simple GUI interface.

I hear that only Visual C++ allows you to mix managed and unmanaged(native) code. I also read that it is possible to create a static library of a native C++ code, include the library in the project and call its function from managed code.

I am wondering what would be the best approach, and if anyone had any tutorials on how to do that.

Thanks
Put C++ code in a DLL with C linkage and then call exported functions from your managed executable.

Or use C++/CLI to mix both in one source code.
You can mix your C++ code using #pragma managed / #pragma unmanaged to control what's going on. See, for example:

"c++/cli pass (managed) delegate to unmanaged code"
http://stackoverflow.com/questions/2972452/c-cli-pass-managed-delegate-to-unmanaged-code

If you're worried about performance, you might want to read up about "Blittable and Non-Blittable Types"
http://msdn.microsoft.com/en-us/library/75dwhxf7.aspx

Blittable types have the same representation in managed and native code so there is no marshaling cost.

Andy
Last edited on
Topic archived. No new replies allowed.