我在Visual Studio解决方案中有3个项目:
每个项目的代码如下所示:
unmanaged_cpp.dll
#include <string>
extern "C" __declspec(dllexport) int joinInt_Native(int x1, int x2) {
std::string s1 = std::to_string(x1);
std::string s2 = std::to_string(x2);
std::string s = s1 + s2;
return atoi(s.c_str());
}
managed_cpp.dll
extern "C" __declspec(dllexport) int joinInt_NetFramework(int x1, int x2) {
System::String ^s = x1.ToString() + x2.ToString();
return int::Parse(s);
}
ConsoleApp.exe
#include <iostream>
#include <Windows.h>
using namespace std;
typedef int(__cdecl *join_fn)(int, int);
HMODULE loadLibrary(LPCSTR dllName, LPCSTR func_name) {
HMODULE dll_id = LoadLibraryA(dllName); //Assume always OK
join_fn f_test = (join_fn)GetProcAddress(dll_id, func_name);
cout << "Loaded DLL " << dllName << ", call = " << f_test(111, 222) << endl;
return dll_id;
}
int main()
{
char c;
HMODULE id1, id2;
cout << "ENTER: 1=load_DLL_1, 2=unload_DLL_1, 3=load_DLL_2, 4=unload_DLL_2, q=exit:" << endl;
while ((c = cin.get()) != 'q') {
switch (c)
{
// Load unmanaged_cpp.dll
case '1':
id1 = loadLibrary("unmanaged_cpp.dll", "joinInt_Native");
break;
case '2':
FreeLibrary(id1);
break;
// Load managed_cpp.dll
case '3':
id2 = loadLibrary("managed_cpp.dll", "joinInt_NetFramework");
break;
case '4':
FreeLibrary(id2);
break;
}
}
return 0;
}
现在,我构建并启动控制台应用程序,尝试这样的操作:
Loaded DLL unmanaged_cpp.dll, call = 111222
This action can't be completed because the file is open in ConsoleApp.exe
==&gt;确定 Loaded DLL managed_cpp.dll, call = 111222
This action can't be completed because the file is open in ConsoleApp.exe
==&gt;确定 This action can't be completed because the file is open in ConsoleApp.exe
==&gt;为什么? 为什么我无法释放托管DLL,因为我可以使用非托管DLL?如何处理这个问题?
只是另一个小问题,上面的代码是否正确?我查看“输出”窗口,它打印出类似这样的内容:
Loaded '...\Debug\unmanaged_cpp.dll'. Symbols loaded.
Unloaded '...\Debug\unmanaged_cpp.dll'
Loaded '...\Debug\managed_cpp.dll'. Symbols loaded.
Loaded many .NET DLL here...
Exception thrown at 0x7586A832 (KernelBase.dll) in ConsoleApp.exe: 0x04242420 (parameters: 0x31415927, 0x73850000, 0x004FEE10).
Loaded many .NET DLL here
控制台应用程序看起来工作正常,但此处抛出异常。那是为什么?