我编写了一个非常简单的 C# 程序,用于打开 DLL。有两个项目...(1) 一个项目是作为 DLL 构建的 C 函数,(2) 使用 DLL 的 C# 程序。
C# 程序有以下行:
[DllImport("C_dll.dll", CharSet = CharSet.Unicode)]
但是当我运行时它找不到 DLL。
由于构建在此处显示了 DLL:
"W:\Dropbox\DSI (His)\Windows Apps\Debug\C_dll.dll"
我将 DllImport 更改为:
[DllImport("W:\\Dropbox\\DSI (His)\\Windows Apps\\Debug\\C_dll.dll", CharSet = CharSet.Unicode)]
当然,它会运行。但我希望位置是相对的。所以我尝试了这些路径:
[DllImport("..\\..\\..\\Debug\\C_dll.dll", CharSet = CharSet.Unicode)]
(that is relative to where the projects are)
and
[DllImport("Debug\\C_dll.dll", CharSet = CharSet.Unicode)]
(that is relative to where the solution is)
为了检查我的相对路径,我做了一些目录:
W:\Dropbox\DSI (His)\Windows Apps\Testing\C_sharp\C_dll>dir /s/b "..\\..\\..\\debug\\C_dll.dll" C_dll.vcxproj
W:\Dropbox\DSI (His)\Windows Apps\debug\C_dll.dll
W:\Dropbox\DSI (His)\Windows Apps\Testing\C_sharp\C_dll\C_dll.vcxproj
W:\Dropbox\DSI (His)\Windows Apps>dir /s/b "Debug\C_dll.dll" Apps.sln
W:\Dropbox\DSI (His)\Windows Apps\Debug\C_dll.dll
W:\Dropbox\DSI (His)\Windows Apps\Apps.sln
所以我很困惑。有谁知道我做错了什么??????
构建输出是:
1>------ Rebuild All started: Project: C_dll, Configuration: Debug Win32 ------
1> C_dll.c
1> Creating library W:\Dropbox\DSI (His)\Windows Apps\Debug\C_dll.lib and object W:\Dropbox\DSI (His)\Windows Apps\Debug\C_dll.exp
1> C_dll.vcxproj -> W:\Dropbox\DSI (His)\Windows Apps\Debug\C_dll.dll
2>------ Rebuild All started: Project: Call_C, Configuration: Debug Any CPU ------
2> Call_C -> W:\Dropbox\DSI (His)\Windows Apps\Testing\C_sharp\Call_C\bin\Debug\Call_C.exe
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========
我的解决方案的树如下:
W:\DROPBOX
└───DSI
└───Windows Apps
├───c_sharp_test
│ ├───bin
│ │ ├───Debug
│ │ └───Release
│ ├───obj
│ │ └───Debug
│ │ └───TempPE
│ └───Properties
└───Debug
.sln 文件路径为:
W:\Dropbox\DSI (His)\Windows Apps\Apps.sln
两个项目路径是:
W:\Dropbox\DSI (His)\Windows Apps\Testing\C_sharp\Call_C\Call_C.csproj
W:\Dropbox\DSI (His)\Windows Apps\Testing\C_sharp\C_dll\C_dll.vcxproj
最佳答案
这个[DllImport("C_dll.dll", CharSet = CharSet.Unicode)]
是正确的。只需在 EXE (Call_C.exe) 的同一目录中配置 C_dll.dll 的输出即可。在 Visual Studio 中,转到配置管理器并在配置属性/常规中配置 C_dll.dll 的输出目录
关于c# - C# 程序在哪里寻找 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269068/