0

ヘッダー ファイルの次のコードを使用して、VS C++ で (もちろん dll プロジェクトとして) dll を作成しました。

#pragma once
#include <iostream>
#include "..\..\profiles/ProfileInterface.h"

using namespace std;

extern "C" __declspec(dllexport) class CExportCoordinator: public CProfileInterface
{
public:
    CExportCoordinator(void);
    virtual ~CExportCoordinator(void);
    
    CProfileInterface* Create();
    void Initialize();
    void Start();   
};

dll の .cpp ファイルは次のとおりです。

#include "StdAfx.h"
#include "ExportCoordinator.h"

CExportCoordinator::CExportCoordinator(void)
{
}

CExportCoordinator::~CExportCoordinator(void)
{
}

CProfileInterface* CExportCoordinator::Create(){
    
    cout << "ExportCoordinator3 created..." << endl;
    return new CExportCoordinator();
}

void CExportCoordinator::Initialize(){

        cout << "ExportCoordinator3 initialized..." << endl;
}

void CExportCoordinator::Start(){
    
    cout << "ExportCoordinator3 started..." << endl;
}

CExportCoordinatorクラスが提供する 3 つのメソッドをすべて使用する必要があるため、クラス全体をエクスポートしました。以下は、上記の dll をオンザフライでロードするメイン アプリケーションのコードです。

    typedef CProfileInterface* (WINAPI*Create)();
    
    int _tmain(int argc, _TCHAR* argv[])

{    
    HMODULE hLib = LoadLibrary(name);
    

    if(hLib==NULL) {
        cout << "Unable to load library!" << endl;         
        return NULL;
    }
    char mod[MAXMODULE];
    
    GetModuleFileName(hLib, (LPTSTR)mod, MAXMODULE);
    cout << "Library loaded: " << mod << endl;   
        
    Create procAdd = (Create) GetProcAddress(hLib,"Create");
        
    if (!procAdd){
        cout << "function pointer not loaded";
    }
    return;
}

出力では、正しいライブラリがロードされていることがわかりますが、その関数ポインタprocAddは NULL です。名前マングリングと関係があると思いextern "C"、dllのヘッダーでクラスをエクスポートするときに追加しましたが、何も変わりませんでした。ところで、クラスのエクスポートされた関数を表示するためにdllエクスポートビューアーを使用しましたが、クラス全体が正しくエクスポートされました。何か助けはありますか?

UPDATE
dllのヘッダーファイルにエラーがあります。extern "C" __declspec(dllexport)クラスがまったくエクスポートされないため、クラスの前に使用しないでください。私が使用するclass __declspec(dllexport) CExportCoordinatorと、クラスは正しくエクスポートされますが、とにかくNULL以外の関数のアドレスを取得できません。

4

2 に答える 2

1
extern "C" __declspec(dllexport) class CExportCoordinator: public CProfileInterface 
{ 

これはナンセンスです。クラスを「extern C」にすることはできません

... inside the class ...
    CProfileInterface* Create();  

これにより、クラスのメンバー関数が作成されますが、これはおそらく必要なものではありません。1 つには、DLL 内でマングルされます。2 つ目は、this ポインターなしでは呼び出すことができません。おそらく、次の宣言が必要です。

extern "C" __declspec(dllexport) CProfileInterface* Create();  

と実装:

extern "C" __declspec(dllexport) CProfileInterface* Create(){
    cout << "ExportCoordinator3 created..." << endl;     
    return new CExportCoordinator();     
}   
于 2010-08-18T10:20:16.147 に答える
1

Createメソッドをメソッドとして宣言し、staticこのメソッドのみをエクスポートする必要があるようです。引き続き使用する場合は、DLL のエクスポートを Dependency Walker ( http://www.dependencywalker.com/を参照)に関して調べ、関数「Create」の名前を「_Create」または「_Create」などに変更する必要がありますNULL。@2".GetProcAddress

于 2010-08-18T10:17:43.813 に答える