October 20, 2013

Visual C++ DLL in C#

Questions:
- VC++ DLL in C#
- How to call C++ DLL in C#
- How to call a Visual C++ method in C# by DllImport
- DllImport in C# ... Visual C++ Dll
- How to use DllImport a C++ class

Error:
- Unable to find an entry point named ...

Solution:
Add these lines in VC Project
extern "C"
{
    __declspec(dllexport) int Sum(int a, int b) {return a + b;}
    __declspec(dllexport) int Mul(int a, int b) {return a * b;}
}

Add these lines in C-Sharp Project
public static class DllHelper
{
    [DllImport("XYZ.dll")]
    public static extern int Sum(int a, int b);
    [DllImport("XYZ.dll")]
    public static extern int Mul(int a, int b);
}

No comments: