How to separate managed and unmanaged DLLs in another directory

岁酱吖の 提交于 2019-11-26 14:47:24

问题


My Release folder is:

MyApp.exe
MyManagedDLL.dll
NativeDLL.dll

MyApp uses the managed dll which calls with pinvoke the native dll. I tried to move them to another subfolder folder and I referenced the managed dll again, when I run my app it says it can't find the NativeDLL.dll. How to fix that?


回答1:


Windows has no idea that it needs to look in a subdirectory for the DLL. It will only look in a select few places for the DLL, starting from the folder that contains the EXE. Giving it a hard time like that it not very productive. But you can help it by pinvoking SetDllDirectory(). Keep in mind that your user won't care where the DLL is located. IT departments tend to favor the simple solutions, troubleshooting DLL loading problems when the app itself is altering the Windows search path is never fun.

It is otherwise a reasonable way to allow a AnyCPU executable to run both in 32-bit and 64-bit mode. You'd use two directories, one with the 32-bit version of the DLL, the other with the 64-bit version. And pinvoke SetDllDirectory accordingly, based on the value of IntPtr.Size.

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);


来源:https://stackoverflow.com/questions/10044511/how-to-separate-managed-and-unmanaged-dlls-in-another-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!