how to call function from DLL in C#/Python
Question: I have next C++ code for create DLL file

    
    
    // MathFuncsDll.h
    
    #ifdef MATHFUNCSDLL_EXPORTS
    #define MATHFUNCSDLL_API __declspec(dllexport) 
    #else
    #define MATHFUNCSDLL_API __declspec(dllimport) 
    #endif
    
    namespace MathFuncs
    {
        // This class is exported from the MathFuncsDll.dll
        class MyMathFuncs
        {
        public: 
            // Returns a + b
            static MATHFUNCSDLL_API double Add(double a, double b); 
    
            // Returns a - b
            static MATHFUNCSDLL_API double Subtract(double a, double b); 
    
            // Returns a * b
            static MATHFUNCSDLL_API double Multiply(double a, double b); 
    
            // Returns a / b
            // Throws const std::invalid_argument& if b is 0
            static MATHFUNCSDLL_API double Divide(double a, double b); 
        };
    }
    
    // MathFuncsDll.cpp : Defines the exported functions for the DLL application.
    //
    
    #include "stdafx.h"
    #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)
        {
            return a / b;
        }
    }
    

after compile I have dll file and i want to call for example ADD function

    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace call_func
    {
        class Program
        {
            [DllImport("MathFuncsDll.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern double  MyMathFuncs::Add(double a, double b);
    
            static void Main(string[] args)
            {
                Console.Write(Add(1, 2));
            }
        }
    }
    

but got this message [error img](http://i.stack.imgur.com/9PJcM.png)

or in python code

    
    
    Traceback (most recent call last):
      File "C:/Users/PycharmProjects/RFC/testDLL.py", line 6, in <module>
        result1 = mydll.Add(10, 1)
      File "C:\Python27\lib\ctypes\__init__.py", line 378, in __getattr__
        func = self.__getitem__(name)
      File "C:\Python27\lib\ctypes\__init__.py", line 383, in __getitem__
        func = self._FuncPtr((name_or_ordinal, self))
    AttributeError: function 'Add' not found
    

please help how I can fix this code, and call for example ADD function.

Thank you


Answer: Since it is C++ you are compiling, the exported symbol name will be
[_mangled_](https://en.m.wikipedia.org/wiki/Name_mangling).

You can confirm this by looking at your DLL's exports list, using a tool like
[DLL export viewer](http://www.nirsoft.net/utils/dll_export_viewer.html).

It's best to provide a plain C export from DLLs when you intend to call them
via an [FFI](https://en.m.wikipedia.org/wiki/Foreign_function_interface). You
can do this using [`extern
"C"`](http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-
effect-of-extern-c) to write a wrapper around your C++ methods.

See also:

  * [Developing C wrapper API for Object-Oriented C++ code](http://stackoverflow.com/questions/2045774/developing-c-wrapper-api-for-object-oriented-c-code)

