Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
838 views
in Technique[技术] by (71.8m points)

.net - How to separate managed and unmanaged DLLs in another directory

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...