简体   繁体   中英

Unable to Call Simple Custom Dll Import from C# Application

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:

在此处输入图片说明

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:

在此处输入图片说明

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM