Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 显式链接DLL和类方法_C++_Visual Studio_Dll - Fatal编程技术网

C++ 显式链接DLL和类方法

C++ 显式链接DLL和类方法,c++,visual-studio,dll,C++,Visual Studio,Dll,我想通过创建一个DLL来创建一个动态库,并将其导入我的主程序。 但我无法正确运行我的程序,因为我从LIB切换到DLL 这是我的DLL.h文件: class Connector { public: Connector(std::string _apiKey, std::string _masterCode, std::string _masterSystem, std::string _masterVe

我想通过创建一个DLL来创建一个动态库,并将其导入我的主程序。 但我无法正确运行我的程序,因为我从LIB切换到DLL

这是我的DLL.h文件:

class Connector
{
public:
    Connector(std::string _apiKey, 
              std::string _masterCode,
              std::string _masterSystem, 
              std::string _masterVersion, 
              int INTERNAL_PARAMETER = -1);
    virtual ~Connector();
    std::string query(std::string method, 
                      std::map<std::string, 
                      std::string> params);
    [...]
}
这是我的mainApp中的链接代码:

typedef std::string (CALLBACK* kcDLLFUNC_QUERY)(
         std::string, std::map<std::string, std::string>, std::string);

HINSTANCE kcDLL = LoadLibrary(_T("Connect"));
kcDLLFUNC_QUERY kcDLLFUNC_query = (kcDLLFUNC_QUERY)GetProcAddress(kcDLL, "query");

std::map<std::string, std::string> params;
params["amount"] = "50";
std::string RES = kcDLLFUNC_query("de", params, "");
std::cout << RES << std::endl;
FreeLibrary(kcDLL);
我忘了什么吗?

主要问题是GetProcAddress只适用于外部C函数。要调用的函数是类的成员,并且尚未导出该函数或整个类

我通常通过在DLL项目中添加一个define来实现这一点,然后在DLL项目中创建一个头,该头定义一个宏,该宏指示是导出还是导入函数/类。大概是这样的:

// Assumes IS_DLL is defined somewhere in the project for your DLL
// (such as in the project's Properties: C/C++ -> Preprocessor)
#ifdef IS_DLL
    #define DLL_API __declspec(dllexport)
#else
    #define DLL_API __declspec(dllimport)
#endif
#include "DllExport.h" // name of the header file defined above

class DLL_API Connector
{
public:
    Connector(std::string _apiKey, std::string _masterCode, std::string _masterSystem, std::string _masterVersion, int INTERNAL_PARAMETER = -1);
    virtual ~Connector();
    std::string query(std::string method, std::map<std::string, std::string> params);
    [...]
}
然后按如下方式修改您的类:

// Assumes IS_DLL is defined somewhere in the project for your DLL
// (such as in the project's Properties: C/C++ -> Preprocessor)
#ifdef IS_DLL
    #define DLL_API __declspec(dllexport)
#else
    #define DLL_API __declspec(dllimport)
#endif
#include "DllExport.h" // name of the header file defined above

class DLL_API Connector
{
public:
    Connector(std::string _apiKey, std::string _masterCode, std::string _masterSystem, std::string _masterVersion, int INTERNAL_PARAMETER = -1);
    virtual ~Connector();
    std::string query(std::string method, std::map<std::string, std::string> params);
    [...]
}
在.exe中,包括类的标题,并像往常一样使用它。您还需要链接到DLL。在Visual Studio的最新版本中,这是按如下方式完成的:

在解决方案资源管理器中,展开.exe的项目。 右键单击“引用”,然后选择“添加引用…”。。。。 在对话框中,从左侧列表中选择解决方案。 选中DLL项目旁边的复选框,然后按OK。
如果您最终为您的程序创建了多个DLL,则需要更改定义的名称,以便它们不会冲突。我通常会在每个定义的名称中包含DLL的名称。

如何切换DLL\U API的定义?当我尝试它时,我得到了一些未解析的令牌。ll_API可以工作,因为它将为DLL项目中的所有文件定义,而不会为EXE项目中的文件定义。我忘记提到的一个步骤是EXE项目必须链接到DLL的项目。我会更新我的答案,告诉你如何添加参考。我试过了,效果很好!但我需要.lib文件来完成这个任务。有一种方法可以在没有.lib文件链接的情况下实现这一点吗?我能想到的唯一方法是添加一个外部C函数,该函数接受所有需要的参数,然后在内部创建类的实例并调用所需的方法。同时生成.lib文件有什么问题?我正在为我们的客户创建一些东西,他们告诉我们,他们不能在项目中使用.lib。