How to call c++ function from windows form

1

My c++ function is given below,

# define MyFunction _declspec(dllexport)

extern "C" {
MyFunction int AddNumbers(int a, int b)
{
    return a + b;
}
MyFunction int SubtractNumbers(int a, int b)
{
    return a - b;
}
 }

Calling c++ function from windows application is given below

    private void btnNumber_Click(object sender, EventArgs e)
    {
        GetNumber();
    }

    public const string cppFunctionsDll = @"..\..\..\Debug\CPP.dll";

    [DllImport(cppFunctionsDll, CallingConvention = CallingConvention.Cdecl)]

    public static extern int AddNumbers(int a, int b);

    [DllImport(cppFunctionsDll, CallingConvention = CallingConvention.Cdecl)]
    public static extern int SubtractNumbers(int a, int b);
    public void GetNumber()
    {
        SubtractNumbers(1,2);
    }

When executing code getting error in windows side => 'Unable to load DLL '......\Debug\AccurynCPP.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)'

My cpp dll path is > D:\Project\December\17-12-2020\Project_Name\Debug

c#
c++
winforms
asked on Stack Overflow Dec 17, 2020 by Noufal

1 Answer

1

Please try hard code the absolute dll path or put in your corresponding debug bin folder.

The msvc will resolve path in current directory / System folder: e.g. C:\windows\system32 / $Path environment variable.

answered on Stack Overflow Dec 17, 2020 by Harold

User contributions licensed under CC BY-SA 3.0