views:

410

answers:

5

I have the source code of a C# program. I want to create a DLL out of it which I want to use in C++.

Is it possible to create a native DLL in Visual Studio 2008 which can be used in C++?

A: 

It is possible in Visual Studio 2008, but you're not going to be able to write it using C#.

To create a native DLL, you'll have to write your code using one of the unmanaged C++ project types.

Justin Niessner
A: 

You can expose the DLL to COM. Have a look here for some examples.

RichardOD
+3  A: 

If you want the program to be native, and not managed, you'll need to port it to C++, instead of using C#.

That being said, you can compile it in C# into a library, and use it from C++ by using C++/CLI. This just requires that you compile the files that use the C# library with the /clr flag. This provides C++ access to the .NET framework, and lets you use libraries made in C# directly from C++.

Alternatively, you can use .NET's COM interop to expose the C# class(es) as COM objects, and then use those from native C++.

Reed Copsey
A: 

yes you can.
you need to create second project.
the project must be unmanaged (like "visual c++"->class library).
the name of this procedure is "calling from unmanaged code to managed code".

good to read unmanaged to managed (codeproject)
you must be aware, that any computer that using your dll must have preinstalled DotNet and Visual C++ Redistributable Package

Avram
+4  A: 

native <-> .Net interop is one of my pet disciplines, which is why I needed this as straightforward and reliable as possible.

The result was that I made me an MSBuild task which I just need to drag into a project to allow me to export static methods from pretty much any .Net language. And since the whole marshalling infrastructure works for exports as well, you can do pretty much anything with it that you want (like passing native/managed objects as IUnknown).

The resulting assembly will look to the consuming process like a real DLL, which means you can't have it to scale up to 64bit automatically anymore.

However, since you do have native bits in your application, you already have this issue anyways. ;-)

When you do not specifiy the CPU target in your C# project, my task will emit a warning that it created a folder for all targets (x86,x64 and Itanium), and there you'll have *.dll and *.pdb for each platform.

Robert Giesecke
+1 good stuff. I'm looking forward to the release of the source code (which you hint at under "License and stuff...") to see how this works.
Wim Coenen
Thx, releasing the source isn't really on the top of the list. Because I would have to pick a license first. And the widely spread licenses are way too restrictive, IMO...
Robert Giesecke
I you want to minimize restrictions while retaining copyright, the BSD license is the natural choice. But the GPL is also fine for build tools (e.g. the gcc compiler suite is GPL-ed and widely used to build commercial applications). What problematic restrictions did you have in mind?
Wim Coenen
I'll take a look at BSD, then. The code of task resides in an assembly which anyone should be able to do whatever he wants to. The only thing is, that I wouldn't like them to explicitly claim it was there invention, but I don't want to require them to distribute any kind of hint to my work (neither as some text file nor in the about box). Most licenses seem to require way too much mumbo jumbo from the user, IMO.
Robert Giesecke