Dilip
Dilip

Reputation: 747

Call C++ dll in c# code

I am trying to call a C++ dll in C# code. my header file -

#define MLTtest __declspec(dllexport)
class MLTtest testBuilder
{
public:
    testBuilder(void);
    ~testBuilder(void);


    int testfunc (int iNumber);
};

My .CPP Class

int testBuilder::testfunc (int iNumber)
{

    return iNumber*2 ;
}

Here is my C# code for using that dll.

class Program
{

    [DllImport(@"C:\Sources\Operations\Online.Dev\BIN\Debug\Mlt1090d64.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "testfunc")]
    public static extern int testfunc(int n);

    static void Main(string[] args)
    {
        try
        {
            int x = testfunc (50);
        }
        catch (Exception ex)
        {
        }
    }
}

but I keep getting this exception exception:

Unable to find an entry point named 'testfunc ' in DLL 'C:\Sources\Operations\Online.Dev\BIN\Debug\Mlt1090d64.dll'.

Upvotes: 1

Views: 6190

Answers (1)

Adrian Bura
Adrian Bura

Reputation: 56

The problem is that you try to call a class member method.

Place in the .cpp file the flowing function (not a class member)

extern "C" int __declspec(dllexport) testfunc(int iNumber)
{
  return iNumber*2;
}

and update in the .cs

[DllImport(@"C:\Sources\Operations\Online.Dev\BIN\Debug\Mlt1090d64.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int testfunc(int n);

Upvotes: 3

Related Questions