如何导出静态链接到MFC的常规MFC DLL函数,以便与C+中的非MFC Win32 Console应用程序一起使用+; 我在C++世界和这类任务上是很新的。 我很难解决这个问题。 非常感谢您的帮助
我已经创建了一个常规的MFC DLL,它静态链接到MFC。(这是我第一次创建MFC项目) MFC DLL头文件内容 MFC DLL定义文件内容如何导出静态链接到MFC的常规MFC DLL函数,以便与C+中的非MFC Win32 Console应用程序一起使用+; 我在C++世界和这类任务上是很新的。 我很难解决这个问题。 非常感谢您的帮助,c++,dll,mfc,dllimport,dllexport,C++,Dll,Mfc,Dllimport,Dllexport,我已经创建了一个常规的MFC DLL,它静态链接到MFC。(这是我第一次创建MFC项目) MFC DLL头文件内容 MFC DLL定义文件内容 //我收到链接器错误LNK2019未解析的外部符号“\uu declspec(dllimport)void\uu cdecl Init(void)” 当我使用这种方法时: 1. C/C++ -> General -> Additional Include Directories is set to: D:\C++ PROJECTS\seria
//我收到链接器错误LNK2019未解析的外部符号“\uu declspec(dllimport)void\uu cdecl Init(void)”
当我使用这种方法时:
1. C/C++ -> General -> Additional Include Directories is set to: D:\C++ PROJECTS\serial\Serial
2. Linker -> Input -> Additional Dependencies is set to : Serial.lib
3. Both Serial.lib and Serial.dll are included in the project and in the Debug folder.
4. Both MFC Dll Project and Win32 Console Projects are compiled in Debug x86 mode.
//Serial.h强制我在其前面包含'pch.h'。如果我在Serial.h之前将'pch.h'包含到Win32 Console应用程序中,那么最终会出现大约1000个错误。
对于Win32控制台应用程序项目:
1. C/C++ -> General -> Additional Include Directories is set to: D:\C++ PROJECTS\serial\Serial
2. Linker -> Input -> Additional Dependencies is set to : Serial.lib
3. Both Serial.lib and Serial.dll are included in the project and in the Debug folder.
4. Both MFC Dll Project and Win32 Console Projects are compiled in Debug x86 mode.
问题
谢谢问题通过@dxiv的评论解决: 无法将类似于
SerialApp::Init的成员函数导入到非MFC应用程序,
因为该应用程序不知道SerialApp
类,并且不能
了解它,因为SerialApp
源自CWinApp
,它是一个MFC
类,而您的应用程序不是MFC。如果你想导出一个免费的
函数,然后使其\uuuu declspec(dllexport)void\uuuu cdecl Init(void)
在SerialApp
类之外,或任何与此相关的类之外
1.我删除了声明\uuuu declspec(dllexport)void\uuu cdecl SerialApp::Init()来自头文件的代码>
2.将Serial.cpp文件中Init()的定义重写为:
extern“C”
{
__declspec(dllexport)void uu cdecl Init()
{
std::cout\uuu declspec(dllexport)void\uuu cdecl SerialApp::Init()
这是一个输入错误吗?您尝试\uu declspec(dllimport)的函数
是一个自由函数,不是SerialApp
的成员。对我来说,它们都不是打字错误:)但可能是打字错误。请解释一下我做错了什么?如何使函数成为类的成员?标题声明不足以使其成为类的成员吗?class SerialApp:public CWinApp{public:SerialApp();uuu declspec(dllexport)void uu cdecl SerialApp::Init();
谢谢!非常简单,在main()
中声明一个名为Init()
的函数,但没有为其提供实现(定义),因此链接器失败。成员函数Init()
是另一个函数。它可以通过类实例(例如pSerialApp->Init()
)调用,或者使用类名(例如SerialApp::Init()
)调用(如果它是静态成员函数)。Init()
将生成代码并尝试链接到名为Init()的函数
,它不是任何类的成员。它与::Init()
(这通常用于告诉编译器调用全局命名空间中的函数,而不是类)。谢谢你,康斯坦丁!我如何在Win32控制台应用程序中创建Serial.dll的类实例?老实说,我不明白你的意思。你能给我举个例子吗?在main()中执行\uu declspec(dllimport)void\uu cdecl Init(void);
函数,我想并希望编译器能够理解我想在我提供的Serial.lib和Serial.dll中使用Init()方法。>这部分有什么问题?typedef void(*Init_t)(void);Init_t Serial=(Init_t)GetProcAddress(lib,“Init”)
@swartkatt您无法将像SerialApp::Init
这样的成员函数导入非MFC应用程序,因为该应用程序不知道SerialApp
类,也无法知道它,因为SerialApp
是从CWinApp
派生的,而您的应用程序是非MFC类函数,然后使其\uuuu declspec(dllexport)void\uuuu cdecl Init(void)在 SerialApp类或任何类中,对于C++,您只在最后一个注释中插入图片,无论如何,您不能直接导入任何C++类或成员函数。+ 1用于回答一个答案。如果这不仅仅是一次练习的话,ode将在稍后回击你。非常感谢你的帮助。我会记住你的建议!
#include <iostream>
#include <Windows.h>
int main()
{
//Load the DLL
HMODULE lib = LoadLibrary(L"D:\\C++ PROJECTS\\serial\\Debug\\Serial.dll");
//Create the function
typedef void(*Init_t)(void);
Init_t Serial = (Init_t)GetProcAddress(lib, "Init");
std::cout << GetLastError(); // This output's 127
if (!Serial) {
//ERROR. Handle it.
}
__declspec(dllimport) void __cdecl Init(void);
//Init(); //LNK2019 unresolved external symbol "__declspec(dllimport) void __cdecl Init(void)"
std::getchar();
}
__declspec(dllimport) void __cdecl Init(void);
Init();
typedef void(*Init_t)(void);
Init_t Serial = (Init_t)GetProcAddress(lib, "Init");
std::cout << GetLastError();
#include "Serial.h"
int main()
{
SerialApp pSerial;
pSerial.Init();
std::getchar();
}
1. C/C++ -> General -> Additional Include Directories is set to: D:\C++ PROJECTS\serial\Serial
2. Linker -> Input -> Additional Dependencies is set to : Serial.lib
3. Both Serial.lib and Serial.dll are included in the project and in the Debug folder.
4. Both MFC Dll Project and Win32 Console Projects are compiled in Debug x86 mode.
extern "C"
{
__declspec(dllexport) void __cdecl Init()
{
std::cout << "Succeded";
}
}
int main()
{
//Load the DLL
HMODULE lib = LoadLibrary(L"D:\\C++ PROJECTS\\serial\\Debug\\Serial.dll");
//Create the function
typedef void(*Init_t)(void); // <- You can name Init_t whatever you want.
Init_t Serial = (Init_t)GetProcAddress(lib, "Init");
if (!Serial) {
std::cout << GetLastError();
return 1;
}
Serial(); //<- Init() Function Call from Serial.dll
std::getchar();
return 0;
}