Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ DLL
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 7-Feb-13 19:33pm
y3ec255169
Comments
Argonia at 8-Feb-13 8:46am
   
if its only for calling methods you can just add it in the References.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
H.Brydon at 8-Feb-13 14:25pm
   
Good info, but you left out P/Invoke, which in my experience is the most used mechanism.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
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)
[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)
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
")]
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)
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 372
1 ridoy 313
2 DamithSL 305
3 OriginalGriff 209
4 George Jonsson 140
0 OriginalGriff 7,968
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,524
3 Maciej Los 5,438
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 22 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100