Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hey everyone.

I have a question to ask.

I'm trying to create a library in C++ and use it in C#.

Here's the guidance that I'm using to achieve:
How to[^]

I created the files in C++(source and header) as follows:

C#
// MathFuncsDll.h

namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}


C#
// MathFuncsDll.cpp
// compile with: /EHsc /LD

#include "MathFuncsDll.h"

#include <stdexcept>

using namespace std;

namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b)
    {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b)
    {
        if (b == 0)
        {
            throw new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}


I build the files, create a dll.
After that I put the dll file in my C# solution(with the same folder of .exe file)

Then, I import the dll in my C# project:
[DllImport("SimpleDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern double Add(double a, double b);

static void Main(string[] args)
{
    double a = Add(1.0, 3.0));
}


When I excecute the program, it gives me an exception.

Why does it happen? Any ideas?

My best regards...

Note: I've tried to add extern "C", but linkage problems occured.
Posted
Updated 3-Dec-11 3:19am
v3
Comments
Menon Santosh 15-May-15 9:46am    
http://www.dotnetperls.com/dllimport
https://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx

Your C++ code is in a namespace MyMathFuncs, and C# will not be able to resolve that through the PInvoke mechanism as far as I am aware. Remove the namespace and make the functions simple C style, and also use extern "C" on the definitions; something like:
C++
// MathFuncsDll.h

// Returns a + b
extern "C" __declspec(dllexport) double Add(double a, double b);

and
C++
// MathFuncsDll.cpp
#include "MathFuncsDll.h"

double Add(double a, double b)
{
    return a + b;
}
 
Share this answer
 
Comments
Un_NaMeD 3-Dec-11 17:48pm    
Thank you.
Jeetha P J 4-Sep-13 23:40pm    
Hi

I have done the same.But for me its showing EntrypointnotFound exception

[DllImport("MathFuncsDll.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Add")]
public static extern double Add(double a, double b);

Please help.Its urgent.

Jeetha
WuFei07 29-May-12 14:49pm    
Thanks. Your solution helped me a lot.
Richard MacCutchan 5-Sep-13 2:58am    
Open a new question and provide full detail. And please don't say it's urgent - it has no urgency to us.
Try to open your DLL file with dependency walker and you must see your function name there. If you cannot see then you have not exported correctly. But if you see something like ?Add@@MyMathFunc@@....then you must use that whole string as your fucntion name like this

[DLLImport("SimpleDLL.dll",Entrypoint="?Add@@MyMathFunc@@....",CallingConvention=CallingConvention.Cdecl)]
 
Share this answer
 
Comments
Member 11616166 18-May-15 11:30am    
Thanks for the reminder to use Dependency Walker. Forgot about it. Using it I realized my function I was trying to call had additional text beforehand (i.e. GetMyString() was really il5_GetMyString()). Thanks!
For that type of interaction, you CAN USE an intermediary -- another DLL (writtin in CLI) that will expose your functions to the C#.

You could of course write the first DLL in C++/CLI or you can make the intermediary.

Check out this thread on DaniWeb[^] for possibilities.
 
Share this answer
 

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