C++ is powerful language. Generally, C++ can do everything from lower level operations to higher level GUI controls. There are some higher level programming languages(like C#, Java etc) which can create a very good GUI with less effort. So, generally, C++ is being used in low level intensive processing or memory level operations which is very fast as compared to other higher level programming language. We can fully use the power of C++ pointers.
So, the general convention is we design the GUI related things in C# and the intensive processing lower level tasks are assigned to C++. We develop all the require functions in C++ and then generate a dynamic link library which contains all the methods. If you are using Microsoft Visual Studio, creating dlls is not a big deal.
After you have a dll file, I am gonna expain how this dll can be used in C#. You may have already noticed that in C# project, the C++ dlls can not be added as reference. So, we have to do something lengthy to get them work. I have studied two P/Invoke methods that are working perfectly:
Before I start the method, I have sample C++ files(Sample.h,Sample.cpp) under the project "CppProject".
//Sample.h
#define DllExport __declspec(dllexport)
extern "C" DllExport double findSquareRoot(double);
//Sample.cpp
double findSquareRoot(double value){
return sqrt(value);
}
Method 1: Simple Method
(i) Create a C# file Test.cs under the project CsharpProject and include the following content:
//Test.cs
using System.Runtime.InteropServices;
namespace CsharpProject
{
[DllImport(@"CppProject.dll")]
public static extern double findSquareRoot(double value);
class Test{
private void displayRoot(double value)
{
double root=findSquareRoot(value);
System.Console.WriteLine("Square root is "+root);
}
}
}
(ii)Create dynamic link library CppProject.dll from Microsoft Visual Studio.
(iii)Copy the dll in the bin/debug or bin/release folder of the project and compile the C# project.
(Note: if you get an error message "PInvokeStackImbalance was detected", then you go to Debug-> Exceptions. Then expand "Managed Debugging Assistants", and disselect "PInvokeStackImbalance". Then it will work)
Method 2: Lengthy Method:
(i) Create a C# file Test.cs under the project CsharpProject and include the following content:
//Test.cs
using System.Runtime.InteropServices;
namespace CsharpProject
{
static class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress
(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}
class Test
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate double findSquareRoot(double value);
IntPtr pDll = NativeMethods.LoadLibrary(@"CppProject.dll");
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "findSquareRoot");
findSquareRoot squareRoot =
( findSquareRoot)
Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall,
typeof( findSquareRoot ));
private void displayroot(double value)
{
double root=squareRoot(value);
System.Console.WriteLine("Square root is "+root);
}
}
(ii)Create dynamic link library CppProject.dll from Microsoft Visual Studio.
(iii)Copy the dll in the bin/debug or bin/release folder of the project and compile the C# project.
How to Test?
To test, you go to Program.cs file which contains the startup Main functon, and then write the following piece of code in the Main function:
new Test().displayRoot(12.44);
After your run, you will get the square root of 12.44 displayed in the console.
I think you got something regarding how C++ dll can be used in C# applications.
Any confusion, contact me (krishna444@gmail.com).
Thanks for reading :)
So, the general convention is we design the GUI related things in C# and the intensive processing lower level tasks are assigned to C++. We develop all the require functions in C++ and then generate a dynamic link library which contains all the methods. If you are using Microsoft Visual Studio, creating dlls is not a big deal.
After you have a dll file, I am gonna expain how this dll can be used in C#. You may have already noticed that in C# project, the C++ dlls can not be added as reference. So, we have to do something lengthy to get them work. I have studied two P/Invoke methods that are working perfectly:
Before I start the method, I have sample C++ files(Sample.h,Sample.cpp) under the project "CppProject".
//Sample.h
#define DllExport __declspec(dllexport)
extern "C" DllExport double findSquareRoot(double);
//Sample.cpp
double findSquareRoot(double value){
return sqrt(value);
}
Method 1: Simple Method
(i) Create a C# file Test.cs under the project CsharpProject and include the following content:
//Test.cs
using System.Runtime.InteropServices;
namespace CsharpProject
{
[DllImport(@"CppProject.dll")]
public static extern double findSquareRoot(double value);
class Test{
private void displayRoot(double value)
{
double root=findSquareRoot(value);
System.Console.WriteLine("Square root is "+root);
}
}
}
(ii)Create dynamic link library CppProject.dll from Microsoft Visual Studio.
(iii)Copy the dll in the bin/debug or bin/release folder of the project and compile the C# project.
(Note: if you get an error message "PInvokeStackImbalance was detected", then you go to Debug-> Exceptions. Then expand "Managed Debugging Assistants", and disselect "PInvokeStackImbalance". Then it will work)
Method 2: Lengthy Method:
(i) Create a C# file Test.cs under the project CsharpProject and include the following content:
//Test.cs
using System.Runtime.InteropServices;
namespace CsharpProject
{
static class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress
(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}
class Test
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate double findSquareRoot(double value);
IntPtr pDll = NativeMethods.LoadLibrary(@"CppProject.dll");
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "findSquareRoot");
findSquareRoot squareRoot =
( findSquareRoot)
Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall,
typeof( findSquareRoot ));
private void displayroot(double value)
{
double root=squareRoot(value);
System.Console.WriteLine("Square root is "+root);
}
}
(ii)Create dynamic link library CppProject.dll from Microsoft Visual Studio.
(iii)Copy the dll in the bin/debug or bin/release folder of the project and compile the C# project.
How to Test?
To test, you go to Program.cs file which contains the startup Main functon, and then write the following piece of code in the Main function:
new Test().displayRoot(12.44);
After your run, you will get the square root of 12.44 displayed in the console.
I think you got something regarding how C++ dll can be used in C# applications.
Any confusion, contact me (krishna444@gmail.com).
Thanks for reading :)