【问题标题】:Using class functions from a dll使用 dll 中的类函数
【发布时间】:2012-02-10 23:49:04
【问题描述】:

我在 Visual Studio 2010 中有两个项目。一个是简单的 (win32) .exe 项目,另一个是 DLL 项目。

在我创建的 DLL 项目中,只有一个简单的类,其中只有一个方法。我想从我的 EXE 应用程序中访问这个类和方法。但我想访问该类,就好像它是它自己的 EXE 项目的一部分一样,例如:

CTest test = new CTest;
test->TestMethod ( );

有没有办法做到这一点?就像释放DLL的头文件并在EXE项目中的某处添加对DLL的引用之类的......??

【问题讨论】:

标签: c++ dll


【解决方案1】:

根据@Luchian Grigore 给出的建议,您需要在要从DLL 加载的类上正确使用_declspec(dllimport)_declspec(dllexport)

编译 DLL 时使用 dllexport,编译使用 DLL 的可执行文件时使用 dllimport。

--- CTest.h ---

#ifdef CTEST_EXPORT  // You are compiling the DLL
#define CTEST_DLL_EXPORT _declspec( dllexport )
#else
#define CTEST_DLL_EXPORT _declspec( dllimport )
#endif

class CTEST_DLL_EXPORT CTest
{
public:
    bool TestMethod();
};

--- CTest.cpp ---

#define CTEST_EXPORT 
bool CTest::TestMethod()
{
    return( true ); // Success?
}

--- main.cpp ---

#include <iostream.h>  // Whatever cin/cout are declared in...
#include "CTest.h"

int main()
{
    CTest ct;

    if( ct.TestMethod() )
    {
        cout << "Success" << endl;
    }
    else
    {
        cout << "Failure" << endl;
    }
    return( 0 );
}

【讨论】:

    【解决方案2】:

    您包含标头并将由项目生成的.lib 添加到额外的包含库中,该项目还生成.dll

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      相关资源
      最近更新 更多