Microsoft KB Archive/828736

= How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005 =

Article ID: 828736

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Write a Managed DLL
 * Register the Managed DLL for Use with COM or with Native C++
 * Call the Managed DLL from Native C++ Code
 * Complete Code Listing
 * REFERENCES



SUMMARY
This step-by-step article describes how to call a managed DLL from native C++ code. To do this, create a simple managed DLL by using Microsoft Visual C# .NET or Microsoft Visual C# 2005, and then call this managed DLL from native C++ code.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Writing Component Object Model (COM) clients
 * COM interoperability in the Microsoft .NET Framework

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Visual C# .NET or Visual C# 2005
 * Visual C++ .NET or Visual C++ 2005

back to the top

Write a Managed DLL
To create a simple managed DLL that has a public method to add two numbers and return the result, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. The New Project dialog box opens. Under Project Types, click Visual C# Projects.

Note In Visual Studio 2005, click Visual C# under Project Types. Under Templates, click Class Library. In the Name text box, type sManagedDLL, and then click OK. Open the Class1.cs file in Code view.  To declare a public interface that has a method to add two numbers, add the following code to the Class1.cs file: // Interface declaration. public interface ICalculator {   int Add(int Number1, int Number2); }; </li>  To implement this public interface in a class, add the following code to the Class1.cs file: // Interface implementation. public class ManagedClass:ICalculator {   public int Add(int Number1,int Number2) {           return Number1+Number2; } } </li> Click Start, and then point to Programs.</li> If you are using Microsoft Visual Studio .NET 2003, point to Microsoft Visual Studio .NET 2003, and then point to Visual Studio .NET Tools. If you are using Microsoft Visual Studio .NET 2002, point to Microsoft Visual Studio .NET, and then point to Visual Studio .NET Tools. If you are using Visual Studio 2005, point to Microsoft Visual Studio 2005, and then point to Visual Studio 2005 Tools.</li> If you are using Visual Studio .NET 2003, click Visual Studio .NET 2003 Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio .NET 2002, click Visual Studio .NET Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio 2005, click Visual Studio 2005 Command Prompt to open a Visual Studio 2005 command prompt.</li> To create a strong name for your class library, type the following command at the Visual Studio .NET command prompt:

sn.exe -k MyKeyFile.SNK

</li> Copy the MyKeyFile.SNK file to your project folder.</li> Double-click the AssemblyInfo.cs file to open the file in Solution Explorer.</li>  Replace the following lines of code in the AssemblyInfo.cs file [assembly: ComVisible(false)] [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile(&quot;&quot;)] with the following. [assembly: ComVisible(true)] [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile(&quot;..\\..\\MyKeyFile.SNK&quot;)] </li> Press CTRL+SHIFT+B to generate the managed DLL.</li></ol>

back to the top

Register the Managed DLL for Use with COM or with Native C++
To use the managed DLL with COM or with native C++, you must register the assembly information of your DLL in the Windows registry. To do this, follow these steps: <ol> Click Start, and then point to Programs.</li> If you are using Microsoft Visual Studio .NET 2003, point to Microsoft Visual Studio .NET 2003, and then point to Visual Studio .NET Tools. If you are using Microsoft Visual Studio .NET 2002, point to Microsoft Visual Studio .NET, and then point to Visual Studio .NET Tools. If you are using Visual Studio 2005, point to Microsoft Visual Studio 2005, and then point to Visual Studio 2005 Tools</li> If you are using Visual Studio .NET 2003, click Visual Studio .NET 2003 Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio .NET 2002, click Visual Studio .NET Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio 2005, click Visual Studio 2005 Command Prompt to open a Visual Studio 2005 command prompt.</li> At the Visual Studio .NET or Visual Studio 2005 command prompt, change the directory path of the directory that contains your managed DLL.</li> To register the assembly information of your managed DLL in the Windows registry and to create a corresponding type library, run the following command at the Visual Studio .NET command prompt:

RegAsm.exe ManagedDLL.dll /tlb:ManagedDLL.tlb /codebase

</li></ol>

back to the top

Call the Managed DLL from Native C++ Code
To call the managed DLL that you just created from native C++ code, follow these steps: <ol> Start Visual Studio .NET or Visual Studio 2005.</li> On the File menu, point to New, and then click Project.</li> <li>If you are using Visual C++ .NET 2003, click Visual C++ Projects under Project Types, and then click Win32 Console Project under Templates. If you are using Visual C++ .NET 2002, click Visual C++ Projects under Project Types, and then click Win32 Project under Templates. If you are using Visual C++ 2005, click Visual C++ under Project Types, and then click Win32 Project under Templates</li> <li>In the Name text box, type CPPClient, and then click OK.</li> <li>If you are using Visual C++ .NET 2003 or Visual C++ 2005, click Finish. If you are using Visual C++ .NET 2002, click Application Settings, click Console Application, and then click Finish.</li> <li>Open the CPPClient.cpp file in Code view.</li> <li> To import the type library that RegAsm.exe generates, add the following code to the CPPClient.cpp file: // Import the type library. Change the path of the type library if the path on your computer differs from this path. </li> <li> To declare the namespace to use, add the following code to the CPPClient.cpp file: using namespace ManagedDLL; </li> <li> To call the managed DLL, add the following code to the _tmain function: // Initialize COM. HRESULT hr = CoInitialize(NULL);
 * 1) import &quot;..\ManagedDLL\bin\Debug\ManagedDLL.tlb&quot; raw_interfaces_only

// Create the interface pointer. ICalculatorPtr pICalc(__uuidof(ManagedClass));

long lResult = 0;

// Call the Add method. pICalc->Add(5, 10, &lResult);

wprintf(L&quot;The result is %d&quot;, lResult);

// Uninitialize COM. CoUninitialize; return 0; </li> <li>Press CTRL+F5 to run the application.</li></ol>

back to the top

Complete Code Listing
Managed DLL // Class1.cs // A simple managed DLL that contains a method to add two numbers. using System;

namespace ManagedDLL {   // Interface declaration. public interface ICalculator {       int Add(int Number1, int Number2); };

// Interface implementation. public class ManagedClass:ICalculator {      public int Add(int Number1,int Number2) {               return Number1+Number2; }   } } C++ Client // CPPClient.cpp: Defines the entry point for the console application. // C++ client that calls a managed DLL.

// Import the type library.
 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;tchar.h&quot;

using namespace ManagedDLL; int _tmain(int argc, _TCHAR* argv[]) {   // Initialize COM. HRESULT hr = CoInitialize(NULL);
 * 1) import &quot;..\ManagedDLL\bin\Debug\ManagedDLL.tlb&quot; raw_interfaces_only

// Create the interface pointer. ICalculatorPtr pICalc(__uuidof(ManagedClass));

long lResult = 0;

// Call the Add method. pICalc->Add(5, 10, &lResult);

wprintf(L&quot;The result is %d\n&quot;, lResult);

// Uninitialize COM. CoUninitialize; return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample. To do this, follow these steps:
 * 1) Click Project, and then click ProjectName Properties.

Note ProjectName represents the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler options, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

back to the top

<div class="references_section">