How to set directory for DLLImport?

-1

I am writing a c# console application in Visual Studio 2017. I am currently importing a DLL using DLLImport from the same directory as my executable file.

Currently, my syntax is as follows.

[DllImport(@"mydll.dll", CallingConvention = CallingConvention.Cdecl)]

However, I would like to move this DLL and all of its dependencies to a "libs" folder and let the application access the DLL and all of its dependencies from the folder. So I have moved all the files to the folder and edited the DLL import.

[DllImport(@"libs\mydll.dll", CallingConvention = CallingConvention.Cdecl)]

I would like the program to be able to run as it was when I was simply using a relative path but I get the error

Unhandled Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at ConsoleApp.Program.functionimportedfromDLL(params...)
   at ConsoleApp.Program.Main(String[] args)

Why might this be the case and how can I fix it?

Thank you all in advance!!

c#
dll
dllimport
asked on Stack Overflow Aug 23, 2019 by Josh Zwiebel

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0