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

Noufal
  • 115
  • 10
  • This answer might help you: https://stackoverflow.com/questions/9003072/unable-to-load-dll-module-could-not-be-found-hresult-0x8007007e – mb14 Dec 17 '20 at 09:32
  • Gets the error 'An attempt was made to load a program with an incorrect format. (0x8007000B)'. referred your link, but didn't gets the answer – Noufal Dec 17 '20 at 10:10

1 Answers1

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.

Harold
  • 170
  • 7
  • I have given hard code path. then gets another error=> 'An attempt was made to load a program with an incorrect format. (0x8007000B)'. – Noufal Dec 17 '20 at 09:49
  • Can you help me? i have also referred https://stackoverflow.com/questions/9003072/unable-to-load-dll-module-could-not-be-found-hresult-0x8007007e . but didn't gets the answer – Noufal Dec 17 '20 at 10:08
  • you can refer to [this link](https://stackoverflow.com/questions/2023766/an-attempt-was-made-to-load-a-program-with-an-incorrect-format-even-when-the-p) – Harold Dec 17 '20 at 10:12