Discussion:
How do I import a .net dll to a vc++ project?
(too old to reply)
Ricardo Vázquez
2009-11-11 12:29:56 UTC
Permalink
I need to use a .NET DLL I've written in C#, within a VC++ 6.0/MFC project.
Googling I find plenty of topics related with using a VC++ DLL in a .NET
project...
But I need exactly the other way round: a .NET DLL in a VC++ project.
Is this possible?
How do I import the .net dll to the vc++ project? (both the .net dll and the
vc++ project are my code)

Thank you very much.

Ricardo Vazquez.
Scott M.
2009-11-11 12:42:30 UTC
Permalink
You need the .NET .dll to have been compiled in the "Register for InterOp"
mode. Then you can use the .NET "regasm.exe" tool to finish the process.

If all you have is the .NET .dll (and not access to the project source
code), then you're out of luck.

-Scott
Post by Ricardo Vázquez
I need to use a .NET DLL I've written in C#, within a VC++ 6.0/MFC project.
Googling I find plenty of topics related with using a VC++ DLL in a .NET
project...
But I need exactly the other way round: a .NET DLL in a VC++ project.
Is this possible?
How do I import the .net dll to the vc++ project? (both the .net dll and
the vc++ project are my code)
Thank you very much.
Ricardo Vazquez.
Kerem Gümrükcü
2009-11-11 14:51:46 UTC
Permalink
Hi Scott,
Post by Scott M.
If all you have is the .NET .dll (and not access to the project source
code), then you're out of luck.
thats partially true and partially false. You still could write
a wrapper (proxy/assembly dll) and make it COM visible, this
even has some advantages, e.g encapsulating difficult calls
and stuff, possibly extending the functionality of the old net dll
with new things and many more,...this is just a example,...

Regards

Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
Kerem Gümrükcü
2009-11-11 12:48:10 UTC
Permalink
Hi Ricardo!
Post by Ricardo Vázquez
I need to use a .NET DLL I've written in C#, within a VC++ 6.0/MFC project.
Googling I find plenty of topics related with using a VC++ DLL in a .NET
project...
Ok,...
Post by Ricardo Vázquez
But I need exactly the other way round: a .NET DLL in a VC++ project.
Is this possible?
Yes it is and there is very good support on that! You have to expose the
managed .net library via COM to your operating system and other applications
that want to use your managed library. Here are several good examples and
explantations/backgrounders on that topic:


[NET Framework Developer's Guide
Exposing .NET Framework Components to COM]
http://msdn.microsoft.com/en-us/library/zsfww439(VS.71).aspx

[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/en-us

[Understanding Classic COM Interoperability
With .NET Applications]
http://www.codeproject.com/KB/COM/cominterop.aspx

[Building COM Servers in .NET]
http://www.codeproject.com/KB/COM/BuildCOMServersInDotNet.aspx

[Calling Managed .NET C# COM Objects
from Unmanaged C++ Code]
http://www.codeproject.com/KB/cs/ManagedCOM.aspx


Hope this helps,..

Regards

Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
Ricardo Vázquez
2009-11-12 08:18:47 UTC
Permalink
Scott! Kerem!

Thank you so much!

Kindest regards,

Ricardo.
Post by Kerem Gümrükcü
Hi Ricardo!
Post by Ricardo Vázquez
I need to use a .NET DLL I've written in C#, within a VC++ 6.0/MFC project.
Googling I find plenty of topics related with using a VC++ DLL in a .NET
project...
Ok,...
Post by Ricardo Vázquez
But I need exactly the other way round: a .NET DLL in a VC++ project.
Is this possible?
Yes it is and there is very good support on that! You have to expose the
managed .net library via COM to your operating system and other applications
that want to use your managed library. Here are several good examples and
[NET Framework Developer's Guide
Exposing .NET Framework Components to COM]
http://msdn.microsoft.com/en-us/library/zsfww439(VS.71).aspx
[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/en-us
[Understanding Classic COM Interoperability
With .NET Applications]
http://www.codeproject.com/KB/COM/cominterop.aspx
[Building COM Servers in .NET]
http://www.codeproject.com/KB/COM/BuildCOMServersInDotNet.aspx
[Calling Managed .NET C# COM Objects
from Unmanaged C++ Code]
http://www.codeproject.com/KB/cs/ManagedCOM.aspx
Hope this helps,..
Regards
Kerem
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
Gregory A. Beamer
2009-11-11 16:09:53 UTC
Permalink
Post by Ricardo Vázquez
I need to use a .NET DLL I've written in C#, within a VC++ 6.0/MFC
project. Googling I find plenty of topics related with using a VC++
DLL in a .NET project...
But I need exactly the other way round: a .NET DLL in a VC++ project.
Is this possible?
How do I import the .net dll to the vc++ project? (both the .net dll
and the vc++ project are my code)
The short answer is set up a COM callable wrapper. This can be as simple
as using tlbexp.exe (type library export) and creating the wrapper. I
believe you have to register the wrapper in the registry, but this is
fairly easy to do. You can google tblexp.exe for more info on the
command line tool.

Regasm.exe shortens the steps a bit, as it handles a couple of tasks,
but, if I remember correctly, you can regsrv32.exe the wrapper if you
need basic COM support. I would double check this.

As for "both in same project and need a project reference", it is not
going to happen with interop, so you have to separate the two out a bit.
Logical separation (in your head) may be enough, but putting in two
projects may be easier to ensure you keep the wrapper updated with the
assembly. It really depends on how you work.

Peace and Grace,
Greg
--
Vote for Miranda's Christmas Story
http://tinyurl.com/mirandabelieve

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
Tom Shelton
2009-11-11 17:45:17 UTC
Permalink
Post by Ricardo Vázquez
I need to use a .NET DLL I've written in C#, within a VC++ 6.0/MFC project.
Googling I find plenty of topics related with using a VC++ DLL in a .NET
project...
But I need exactly the other way round: a .NET DLL in a VC++ project.
Is this possible?
How do I import the .net dll to the vc++ project? (both the .net dll and the
vc++ project are my code)
Thank you very much.
Ricardo Vazquez.
A non-com alternative is to use C++/CLI:

http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/
--
Tom Shelton
Ricardo Vázquez
2009-11-12 14:23:26 UTC
Permalink
Wonderful solution, Tom.
Clean, neat, no problems when installing my app, and so clearly documented.

Thank a lot.

Best regards,

Ricardo.
Post by Tom Shelton
Post by Ricardo Vázquez
I need to use a .NET DLL I've written in C#, within a VC++ 6.0/MFC project.
Googling I find plenty of topics related with using a VC++ DLL in a .NET
project...
But I need exactly the other way round: a .NET DLL in a VC++ project.
Is this possible?
How do I import the .net dll to the vc++ project? (both the .net dll and the
vc++ project are my code)
Thank you very much.
Ricardo Vazquez.
http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/
--
Tom Shelton
Loading...