0

I am new to C#, Need help in the following situation

I have created an executable application (.exe) in C#. The application depends on certain C++ dlls. Hence I have used dllimport method - with the local path to execute the application. When I am executing the .exe as such (without C#) the application works if the dll are placed in the same level of .exe.

I want to place all the DLLs in an folder named DLL and place the folder next to .exe. Now the dllimport will not recognize the path. Now how do i write my code to pick the current path at run time for the .exe is executed independently.

I tried the below code it does not work. Basically it just throws compilation error. Since new to C#, though seen many online answers I am sorry not able to get a solution. Please help me with this

[DllImport(System.Windows.Forms.Application.StartupPath + "\\AP4600_SDK.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ReaderOpen")]
sunshine
  • 39
  • 2
  • 8
  • I'm pretty sure this goes against the Win32 guidelines - have a look at how the dynamic library resolution process works, and you'll see why absolute paths are likely a bad idea. But if you insist, have you tried `DLL\AP4600_SDK.dll` as the path? If that doesn't work, you'll probably have to load the DLL dynamically, using `LoadLibrary` and such. – Luaan Nov 02 '16 at 15:33
  • What kind of compilation error are you receiving? – JDavila Nov 02 '16 at 15:48
  • Possibly, if the files were moved the DllImport could have trouble finding the path specified. – JDavila Nov 02 '16 at 15:49
  • Typically, the .dlls should be in the same folder of the .exe in C#. – JDavila Nov 02 '16 at 15:51
  • I tired all of the methods in the duplicate links sent. None works. The error is receive is "System.Windows.Forms.Application.StartupPath + "\\AP4600_SDK.dll"" needs to be a constant type. I tried making it constant still didnt work. I understand if dlls are same level as .exe things will work. But i believe there should be a way if all dll are placed in a folder and the folder is placed next to .exe how to now get the dll path during run time. – sunshine Nov 02 '16 at 16:04
  • I have posted this comment trying all of the solutions detailed online as mentioned in my initial comment. – sunshine Nov 02 '16 at 16:06

0 Answers0