0

My console app currently makes reference to an unmanaged DLL by using its absolute file path:

public const string dll_path = @"C:\UnmanagedDllPath\externalDLL.dll"

The dll works fine, but now I'm trying to change the file location to somewhere inside my project and include it in the build process. In my first trial, I tried to simply change the file location to the somewhere inside the project. My project structure is:

ProjectName
  --PathA <-- cwd
  --PathB
  --Resources
      -- externalDLL.dll

This is my understanding of what should be the proper relative path:

public const string dll_path = @"..\\Resources\\externalDLL.dll"

The project builds, but whenever it needs to reference a function of the dll, I get an 'System.EntryPointNotFoundException: Unable to load DLL' error.

Next, after doing some research, I figured out that I need to change the dll file properties to: Copy to Output Directory. After building, I can now see the file in the bin directory (in its root, at the same level of the .exe), but I still does not work when I change the file path to:

public const string dll_path = "externalDLL.dll"

The error appears in the first call to the dll function, after I changed the dll_path::

string key = 'x';
string user = 'y';
string pwd = 'z';    
DLLInitializeLogin(key, user, pwd);

[DllImport(dll_path, CallingConvention = CallingConvention.StdCall)]
public static extern sbyte DLLInitializeLogin(
    [MarshalAs(UnmanagedType.LPWStr)] string activationKey,
    [MarshalAs(UnmanagedType.LPWStr)] string user,
    [MarshalAs(UnmanagedType.LPWStr)] string password);

I'm not sure what else I may be missing...

roccaforte
  • 81
  • 5
  • can you show how you load the dll or make use of it? – DarkSquirrel42 Jul 27 '22 at 13:11
  • @DarkSquirrel42 I've just included some additional information in the description. – roccaforte Jul 27 '22 at 13:25
  • @AlexF I don't think that's the case, as I'm able to run the application properly before changing the dll path. – roccaforte Jul 27 '22 at 13:26
  • Try set Environment.CurrentDirectory to folder where located dll `Environment.CurrentDirectory = Application.StartupPath` before use exported function. – Mihal By Jul 27 '22 at 13:38
  • Thanks @MihalBy, but this is actually a console application... – roccaforte Jul 27 '22 at 13:46
  • Does it work if you specify an absolute path to your build directory? I.e. `dll_path = "c:\myProject\bin\externalDll.dll"` ? – JonasH Jul 27 '22 at 13:52
  • @Alex that's the build path 'C:\...etc...\MyProject\bin\Debug\net6.0' – roccaforte Jul 27 '22 at 14:01
  • @JonasH yes, my application is working fine without the file path change. – roccaforte Jul 27 '22 at 14:02
  • does this dll have initializations that could go wrong? (i.e. other DLLs that need to be present?) the Error you get can be interpreted as "oops, something went with a bang while loading that dll" it's not necessarily that the dll is not found... – DarkSquirrel42 Jul 27 '22 at 14:02
  • @DarkSquirrel42 Not actually. It's a simple and straighforward application that only uses this external dll and the method call I exemplified in the description is as simple as it gets. – roccaforte Jul 27 '22 at 14:05
  • can you try loading it dynamically and see if the errorcode is indeed 0x02 (ERROR_FILE_NOT_FOUND) ? https://stackoverflow.com/questions/6452951/how-to-dynamically-load-and-unload-a-native-dll-file – DarkSquirrel42 Jul 27 '22 at 14:10
  • on secound reading... your relative path was @"..\\Resources\\externalDLL.dll" ... try @"..\Resources\externalDLL.dll" – DarkSquirrel42 Jul 27 '22 at 14:18
  • Do focus on what the exception tells you. [EntryPointNotFoundException](https://learn.microsoft.com/en-us/dotnet/api/system.entrypointnotfoundexception?view=net-6.0) means it cannot find an exported function named "DLLInitializeLogin" in that DLL. It does **not** complain about finding the DLL, it found it just fine. Use Dumpbin.exe /exports on the DLL to verify. – Hans Passant Jul 27 '22 at 14:26
  • @HansPassant What I don't get is why the application works fine before changing the dll path... The function exists, indeed. The issue starts after I try to change its location to somewhere inside the application. – roccaforte Jul 27 '22 at 14:34
  • Thanks @DarkSquirrel42. I tried to change the path, and the error was the same. When it comes to loading it dinamically, I did it with both the absolute and relative path approaches and had no errors. – roccaforte Jul 27 '22 at 14:35
  • so ... it only goes sideways if you take the dllimport way and have no absolute path? – DarkSquirrel42 Jul 27 '22 at 14:38
  • @DarkSquirrel42 Right. It goes sideways when I try to get rid of the absolute c:\ path approach. – roccaforte Jul 27 '22 at 14:43
  • 1
    I can only guess at this point, but if I had to take a shot at what's going on: My best guess is that the DLL does something funny in its DllMain, some assumption that's not really safe to use but works when you just directly call loadLibrary, and that's not satisfied with how non-absolute paths are handled along the way when using the dllimport attribute. Then when it's going sideways all that happens is the DllMain call dies ... despite its name, the exception really just says "something with the entry point went wrong..." – DarkSquirrel42 Jul 27 '22 at 14:56

0 Answers0