1

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

4
  • Make sure the C++ library's output folder matches where the C# project's output folder is or make a pre-build step that copies to that location (the C++ dll will need to be in the same folder as the C# application).
    – crashmstr
    Feb 17, 2016 at 16:43
  • Hi crashmstr, thanks for your comment. I have built both projects and put them in the same folder but with the same error. I have also added the dll into the c# debug/release folder but this doesn't help either. Does this seem like the issue is with the dll itself?
    – Bassie
    Feb 17, 2016 at 16:53
  • You can get that error if some other DLL dependency of the DLL you are using cannot be found, but that would not seem likely here.
    – crashmstr
    Feb 17, 2016 at 16:55
  • 4
    And since it is not a .Net DLL or a COM dll, then you cannot add a reference (this is normal).
    – crashmstr
    Feb 17, 2016 at 16:55

3 Answers 3

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.

13
  • Hi Mike thanks for your answer - do you mean change the name of the Dll to TestImport.Dll before building, or is this what I should change the call to?
    – Bassie
    Feb 17, 2016 at 16:37
  • I have tried all of these but with no help - are there any config settings in the dll which I can try changing?
    – Bassie
    Feb 17, 2016 at 16:53
  • You have physically copied the <C++ PROJ NAME>.dll from the Debug/Release folder of the C++ project and pasted it into the Debug/Release folder of your 'C#' project?
    – mike
    Feb 17, 2016 at 16:55
  • If you have done this, and ensured that TestLib.dll is the same exact name as the .dll you generated from the C++ project, this should work.
    – mike
    Feb 17, 2016 at 16:57
  • 1
    I'm not sure about that one.. but check my edit.. go the project properties for the C# one, go to the Build tab, and play with the Platform Target combo and see if either the x64 or x86 works
    – mike
    Feb 17, 2016 at 17:26
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.

1
  • Hi Khale, yes I tried this but with no luck! I even added both the completely built EXE and DLL in the same folder but without any improvement!
    – Bassie
    Feb 17, 2016 at 16:54
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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.