我想使用一家公司提供的 c++ 库。我只有 .lib 和 .h 文件。我想统一使用它。所以我需要一个DLL。所以我想创建一个 DLL,允许我访问 .lib 中我需要的函数。
为此,我创建了一个 Visual Studio C++ DLL 项目,其中包含一个从库中调用所需函数的 C++ 文件。.lib 在链接器/输入/其他依赖项中链接到 Visual Studio。
为了简单起见,我试图从普通的 c++ 文件而不是统一调用我的 DLL 的函数。
我的测试文件如下所示:
#include <windows.h>
#include <iostream>
void CallMyDLL(void)
{
HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\path\\museDLL.dll");
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"startConnection");
typedef void (__stdcall * pICFUNC)();
pICFUNC startConnection;
startConnection = pICFUNC(lpfnGetProcessID);
//std::cout << test<< std::endl;
startConnection();
FreeLibrary(hGetProcIDDLL);
}
int main () {
CallMyDLL();
}
我的主要 DLL 文件如下所示:
#include "dllmain.h"
#include <iostream>
#include <fstream>
#include "main_muse.h"
namespace museDLL
{
MainPage* mp;
void startConnection()
{
mp = new MainPage();
}
double getValue()
{
return mp->getValueEEG();
}
double test()
{
return 1.2f;
}
}
dllmain.h
#pragma once
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
namespace museDLL
{
extern "C" {
DLL_API void __cdecl startConnection();
DLL_API double __cdecl getValue();
DLL_API double __cdecl test();
}
}
如果我在我的 DLL 中调用测试函数,它就可以工作。但是,如果我调用使用原始 .lib 中的函数的 startConnection 函数,它会崩溃(没有错误消息)。
所以为了清楚起见,我想要测试文件-> 调用我的自定义 DLL -> 调用下载的 .lib 。
我不明白我做错了什么?
我想知道最初的 .lib 是否包含在 .DLL 中,还是我需要以某种方式链接它?