Unable to Call Simple Custom Dll Import from C# Application

0

Update

I have now run through the tutorial 3 times with the same results (Visual Studio/Windows is unable to recognise the DLL as a valid file). This must be an issue with my environment, as mike below ran through the tutorial and it worked fine.

I did notice this in the C++ project's solution explorer on my last run:

enter image description here

Does anyone know if this is okay? There seems to be a lot of Red stop-signs which to me would suggest something bad...


Following this tutorial, I have ended up with the following:

C#

using System.Runtime.InteropServices;

namespace TestImport
{
    class Program
    {
        [DllImport("TestLib.dll")]
        public static extern void DisplayHelloFromDLL();

        static void Main(string[] args)
        {
            DisplayHelloFromDLL();
        }
    }
}

C++

In .cpp file Source Files

#include <stdio.h>

extern "C"
{
    __declspec(dllexport) void DisplayHelloFromDLL()
    {
        printf("Hello from DLL !\n");
    }
}

When debugging in Visual Studio:

An unhandled exception of type 'System.DllNotFoundException' occurred in TestImport.exe

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

When running the built EXE (from he same folder as the Dll):

An attempt was made to load a program with an incorrect format.

Have I done anything wrong in the above code? I did do all of this in VS 2015, where the tutorial calls for VS 2010, howeve rI couldn't find a more up-to-date tutorial.


When I try to add the project as a reference by Project -> Add Reference ... I am seeing this error:

A reference to 'Path\To\MyLib.dll' could not be added. Please make sure 
that the file is accessible, and that it is a valid assembly or COM
component.

Which makes me think that the issue is either with the source code or the DLLs configuration.

I have also tried building both files and putting them in the same folder before running the Executable, but without any improved results (same error as when running the EXE above).


Solution Explorer Screenshot:

enter image description here

c#
c++
asked on Stack Overflow Feb 17, 2016 by Bassie • edited Feb 17, 2016 by Bassie

3 Answers

1

Make sure the name of your C++ project and DllImport parameter are the same. Also, if the dll file is not located in the same directory as your C# project, make sure to add the dll into your Debug/Release folder.

Also! Make sure that you are running in the same 32/64 bit mode when compiling.. if you compiled the dll using 32 bit and try to use it in a 64 bit C# program, it won't work.

answered on Stack Overflow Feb 17, 2016 by mike • edited Feb 17, 2016 by mike
0

OK, now build this application, and then copy the previously built DLL into the Debug/Release directory of the current application. The DLL should be in the same directory as your main application.

Did you do the bolded text? Your C# app won't know where to look for the DLL and it looks in the build folder, by default.

answered on Stack Overflow Feb 17, 2016 by Khale_Kitha
0

An unhandled exception of type 'System.DllNotFoundException'

It happened to me, because of the imported c++ dll, its own dependency dlls were missing.

After copying them into Debug/Release folder everything worked.

answered on Stack Overflow May 22, 2019 by Val

User contributions licensed under CC BY-SA 3.0