ヘッダー ファイルの次のコードを使用して、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以外の関数のアドレスを取得できません。