2

DLL の .h コードに従い、getProcAddress を使用して別のコードで DLL を使用しています。

// MathFuncsDll.h

#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport) 
#else
#define MATHFUNCSDLL_API __declspec(dllimport) 
#endif

namespace MathFuncs
{
    // This class is exported from the MathFuncsDll.dll
    class MyMathFuncs
    {
    public:
        int x = 10;
        MyMathFuncs();

           // Returns a + b + x
           MATHFUNCSDLL_API double Add(double a, double b); 

    };
}

対応する .cpp コードは

// MathFuncsDll.cpp : Defines the exported functions for the DLL application. 
//

#include "stdafx.h"
#include "MathFuncsDll.h"
#include <stdexcept>

using namespace std;

namespace MathFuncs
{
    MyMathFuncs ::MyMathFuncs()
    {
            x = 10;
    }

    double MyMathFuncs::Add(double a, double b)
    {
        return a + b + x;
    }

}

関数 Add がエクスポートされ、x = 10 の初期値とともに a と b が追加されます。

同じ DLL ファイルを作成し、LoadLibrary と GetProcAddress を使用して関数を呼び出しました。

コンストラクターを使用せずに 10 を直接追加すると、コードは正常に動作します。つまり、a + b + 10 です。

GetProcAddress を使用してそのようなオブジェクトをインスタンス化し、DLL をロードして関数を呼び出すと、インスタンス化されたオブジェクト メソッドを取得する方法。

4

3 に答える 3