create c# dll which can be used from both c# and c++ - c#

I'm in the middle of moving my solution from c# to c++. I've created several native c++ dll and they just fine. It's easy to connect native dll to both c++ and c#. For c# i'm using P/Invoke for methods execution and delegate for callback, i just pass pointer to unmanaged memory and read it using Marshall class.
Now I have opposite situation - I need to create C# project that NOW will be used from another C# project but LATER i will rewrite main project to C++ and so I need to access C# project from C++. It should offer just several methods: PlaceOrder(void* pointerToStruct) CancelOrder(void* pointerToStruct) and one call-back delegate OrderUpdated(void* pointerToStruct)
I'm looking for hints and examples. How can I create C# project which will be usable from both native C++ and C# and offer several methods + one callback.
In particular I don't now what should I do with memory - should I write to unmanaged memory at c# and read from it at c++ or should I write to managed memory at c# and read managed memory in c++ somehow... Pointer to structures which I pass at parameters should point to unmanaged memory or managed memory etc.

You may want on creation of COM component: Create COM component and ActiveX controls in .Net
If you are developing in Windows8, you may think of creation Windows Component:
Creating Windows Runtime Components in C#

The problem with dll written on C# is that it's impossible to make export table (mark static methods as being exported):
there's DllImportAttribute but there's no DllExportAttribute in C#. There're two by-ways though:
Write the dll (at least partially) on managed C++ that is specially designed for this purpose.
Change generated IL after C# source code compilation:
e.g. http://winch.pinkbile.com/wiki/index.php/Main/Dlltool
http://www.codeproject.com/Articles/37675/Simple-Method-of-DLL-Export-without-C-CLI
See also
Is is possible to export functions from a C# DLL like in VS C++?

Related

C# talking to C++ library - AAF SDK

I'm trying to read AAF files (that describe edited video and audio sequences) in c#. AAF is open source, and there's a C++ SDK available http://sourceforge.net/projects/aaf/.
I'm a C# coder and while I understand some of the C++ coding paradigms, my C++ is basically non-existent.
I presume the intereaction is possible using pinvoke which I've used before for interacting with Win32. With win32 I've pulled calling information from pinvoke.net, but I'm not entirely sure how I go about defining the pinvoke entry points from scratch with a third party SDK.
Can anyone help me to get started on this? For instance, what are the first steps in pinvoking a third party C++ library? Are there tools for helping automate this process?
You have basically two ways of doing that:
Using C++ Interop (implicit PInvoke)
Visual C++ can interop with native C++ code. So you can use Visual C++ as a bridge between the native C++ and C#. Basically you have to write wrapper (or "proxy") classes in visual C++ to interact with your native C++ objects.
Under the hood, this method uses PInvoke (therefore "implicit PInvoke") with specific options, but it has the advantage of retaining the object-oriented structure of the native C++.
Note that this method is not portable to Mono.
Using good ol' PInvoke
You can use PInvoke with C++ the exact same way as you use PInvoke with C.
There is however a limitation: you cannot PInvoke C++ methods this way, so you lose the C++ object-oriented structure.
If the library that you are using provides a C API, you can use that. Otherwise, you have to create a small C++ wrapper that will wrap the C++ API in the form of C-style free functions.
If you have a lot of classes to wrap this way, you can use SWIG to wrap automatically. As a bonus, SWIG recreates the object-oriented structure in C# via some guru code generation. However using SWIG requires some setup and I don't think it is necessary if you only need a few functions.

Add to C++ application C# GUI

I would like to add my C++ application a C# .NET GUI .
My C++ application is very simple but I have a some Pointer and Reference .
What is the Best way C# will recognize this pointer and Reference?
There are several ways, here are some, and it depends on your taste and on your project.
a) Use COM interop, if your C++ project is written in COM.
b) Use COM interop, you can write COM wrappers to your C++ application and use it from C#.
c) Use C++/CLI, that is, convert your project to become a Managed C++ application and create managed classes in C++ that use your C++ code.
You can also write a managed C++ dll that loads your C++ dll or static library.
d) Use P INVOKE C calls, exports DLL C functions from your C++ code that work with your C++ class, call the C functions from C# using [dllimport] attribute.
You can use type IntPtr and the better and safer SafeHandle to express pointers and references.
All ways have good things and drawbacks, but every listed tehcnique need a "middle layer", you cannot call C++ code directly from C#.
If I understand correctly you want to use the functionality written in C++ program in a C# based UI application.
You can do this by creating a C++/CLI based dll. C++/CLI can call C++ code easily and C# can easily call C++/CLI code. In this way you can use functionality written in C++ in a C# based program
For all possible ways see this post

Mixing c# and vc++

I've got a code snippet of c# that I've been trying to translate into vc++, but I'm having some difficulties. So, I'm wondering if it is possible or advisable to mix c# and vc++; that is to say, can I call my c# function from vc++ and vice-versa. If so, are there tricks to it? If, not, why?
Calling C++ code from C# is easy enough and you should be able to find plenty of articles on Pinvoke and other types of interop.
For calling your C# code from C++ - if your C++ app is managed then you should be able to directly reference the C# assembly from your C++ app and use it as normal.
If ypu C++ code is not managed then you will need to use some sort of interop to allow your C++ assembly to call C# code, the only way of doing this that springs to mind is COM.
Try some of these links for more information on COM interop with C#:
http://www.codeproject.com/KB/COM/COM_DOTNET_INTEROP.aspx
http://www.codeproject.com/KB/COM/nettocom.aspx?df=100&forumid=14076&exp=0&select=864859
Is the VC++ app managed? If so, you could create a C# DLL, use it from your c++ app, and if you so desire, ILMerge them into a single EXE.
If you want to mix the two, I'd start with C# as the base project and then register an unmanaged (C++) DLL in your C# project. That way you can combine the reliability of the managed C# parent application with the speed benefits of C++ in any processor-intensive methods you might have.

Sharing objects between C# and C++ code

Is it possible to share references to C# objects between C# and C++ code without massive complexity? Or is this generally considered a bad idea?
The best solution for sharing a C# object between native and managed code is to use COM interop. This allows you to essentially share an interface of an object between managed code and it's equivalent signature in C++.
As for the complexity side of things. The majority of COM interop scenarios are straight forward and really are no more complex than good old COM programming. On the managed side it looks really no different than a normal interface.
Once you introduce multiple threads or start playing around between COM apartments though, things can get a bit tricky.
In my experience, the easiest way to get this working is the following.
Define an interface in C# that you wish to use in C++
Mark the interface with the ComVisible(true) attrbute
Run tlbexp on the assembly which generates a TLB file
Import the TLB into your native project
This will get the interface definition into both of your projects. How to pass that between the projects requires a bit more detail into your architecture.
Another solution I can recommend, from personal experience, is to use a managed C++ interface between the two if the C++ code you want to access is too large or too complex.
For example, I am using the RakNet C++ network library in a C# project. The solutions are to either create a massive wrapper class in C# to access the required C++ functions, create a C++ wrapper around those functions which can than be used as a COM interop or use Managed C++ (Visual C++/CLI).
I chose the latter which allows me to use C++ to access the RakNet library, but the classes created can be used directly in another .NET project as if. So the main logic has been created in those Managed C++ classes, which also allow me to use the .NET framework and some of its wonderful features. In my C# project I simply need to call the Managed C++ library which provides me with all in all 20 functions I need to perform everything.

How can I create a c# object from C code?

I'm using a small C webserver. I'm doing some of the actual request processing with C# libraries.
I'm using a glue layer which appears to be written in something close to c++ to join the two worlds. It provides a 'public ref class' which I can derive from in the C# world.
If I want to derive several different classes from this in C#, how do I create instances of these derived classes from the C/C++?
What information (the class name? A factory function?) shoud I pass (and and in what form) from the C# to the C/C++ code?
I would like a solution compatible with .NET 2.0, and I'm using Visual Studio 2008 to create my code.
I have realized several times that people are sometimes not completely aware of the difference between managed and unmanaged code, so I would just like to summarize:
You cannot simply call managed code from a native C++ application. In order to call it, you will first have to expose your .Net code to COM, and then instantiate a COM object in C++. This way your native app thinks it is creating a COM object like any other, and .Net is doing all the interop in runtime.
Second way is to use managed C++/CLI (which is not native C++, but supports both worlds). This way both C++ and C# apps are managed and can communicate seamlessly. So as long as the "glue layer" is written using managed C++/CLI, you can both work with native and managed data.
As your originally mentioned unmanaged C++, then the answer would be to go for 1st solution: expose the managed object to the native world through COM interop. If you don't mind using managed C++/CLI, then you have a simpler solution - you can instantiate managed classes easily (with some changes in syntax you should quickly get used to).
See How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005.
http://support.microsoft.com/kb/828736
Use the #using directive to import your C# assembly into the C++ code.
#using "ThingLib.dll"
using namespace ThingLib;
If you only want the C++ code to be aware of the base class, you'll need to call some kind of factory method.
ThingBase^ thing = myThingFactory.MakeThing(aParameter);
If you want to actually instantiate the derived classes in C++, use the gcnew operator.
ThingBase^ thing = gcnew DerivedThing(aParameter);
There's a good summary here of the new C++ language features for talking to managed code:
http://msdn.microsoft.com/en-us/library/xey702bw.aspx

Categories