Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI ,
I want to use a C++ dll in C#.Net application.How can i use C++ dll in my C#.net code. if i can use more than one way which is the best way to use C++ dll .

Thanks in Advance
Posted
Comments
Argonia 8-Feb-13 8:46am    
if its only for calling methods you can just add it in the References.

Below are the steps to follow.

1. Open Visual C++ Win 32 Project
2. In Application Setting Chose Dll & select Empty Project
3. Add New Item(*.cpp) in Source Folder
4. Select cpp File
5. Write Down the simple Code in cpp file:

#include <stdio.h>

extern "C"
{
__declspec(dllexport) int add(int a,int b)
{
return a+b;
}
__declspec(dllexport) int subtract(int a,int b)
{
return a-b;
}
}
6. After Compile you will see the Debug Folder have example.dll and example.lib files.
7. Then create c# console application
8. Code is as follows

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int subtract(int a, int b);
private void button2_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
int y = Convert.ToInt32(textBox2.Text);
int z = subtract(x, y);
MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
9. In case of Previous Version of .net Code will be (3.5)
C#
[DllImport("example.dll")]
        public static extern int subtract(int a, int b);
        private void button2_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(textBox1.Text);
            int y = Convert.ToInt32(textBox2.Text);
            int z = subtract(x, y);
            MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


10. For .net Frame Work 4 the code will be,

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int add(int a,int b);

Note: Out put will be same for both

Now you can call an C++ dll from your C# application (.net frame work 4)
 
Share this answer
 
v3
There are several ways of doing it.

PInvoke
Create C++/CLI wrapper around your C++ native code (make static library out of C++ native code) and C++/CLI generated assembly can be easily utilized in .net application.
COM, i.e using interop (which is difficult among all the options)
In my suggestion easiest way is to use option 2, but you need to take care of proper marshaling.
 
Share this answer
 
Comments
H.Brydon 8-Feb-13 14:25pm    
Good info, but you left out P/Invoke, which in my experience is the most used mechanism.
P/Invoke can be used to call stand-alone functions from native DLLs. But if you want to use entire classes, when you should go with another approach. The best would probably be to write a C++/CLI wrapper over your native code and expose the desire functionality to the managed application. Another approach, though I find it harder, is to write a COM wrapper around your native DLL. This COM object can then be consumed from your managed application.
 
Share this answer
 
1. Open Visual C++ Win 32 Project
2. In Application Setting Chose Dll & select Empty Project
3. Add New Item(*.cpp) in Source Folder
4. Select cpp File
5. Write Down the simple Code in cpp file:

#include

extern "C"
{
__declspec(dllexport) int add(int a,int b)
{
return a+b;
}
__declspec(dllexport) int subtract(int a,int b)
{
return a-b;
}
}
6. After Compile you will see the Debug Folder have example.dll and example.lib files.
7. Then create c# console application
8. Code is as follows

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int subtract(int a, int b);
private void button2_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
int y = Convert.ToInt32(textBox2.Text);
int z = subtract(x, y);
MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
9. In case of Previous Version of .net Code will be (3.5)
Collapse | Copy Code
[DllImport("example.dll
C++

")]
public static extern int subtract(int a, int b);
private void button2_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
int y = Convert.ToInt32(textBox2.Text);
int z = subtract(x, y);
MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

10. For .net Frame Work 4 the code will be,

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int add(int a,int b);

Note: Out put will be same for both

Now you can call an C++ dll from your C# application (.net frame work 4)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900