0

我尝试在 Embarcadero C++ Builder XE3 中创建一个 DLL,并在同一环境中的测试项目中使用它。

我以一个教程为例,该教程的代码对我来说效果不佳(!):http ://docwiki.embarcadero.com/RADStudio/XE3/en/Tutorial:_Using_Dynamic_Linked_Libraries_in_C%2B%2BBuilder_Applications

这是我的 DLL 的内容:

BaseAuth.h 文件:

#ifndef   BaseAuthH
#define   BaseAuthH

#include <System.hpp>
class TBaseAuth
{
public:
    virtual void TestMessage() = 0;
};
#endif // BaseAuthH

Auth.h 文件:

//---------------------------------------------------------------------------
#ifndef AuthH
#define AuthH
//---------------------------------------------------------------------------
#include "BaseAuth.h"
class TAuth : public TBaseAuth
{
public:
    TAuth();
    ~TAuth();   
    void TestMessage();
};
#endif

Auth.cpp 文件:

//---------------------------------------------------------------------------
#pragma hdrstop
#include "Auth.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
TAuth::TAuth()
{
}
TAuth::~TAuth()
{
}
void TAuth::TestMessage()
{
    MessageBox(0, "Test message", "Test", MB_OK);
}

和 File1.cpp :

#pragma hdrstop
#pragma argsused

#include "Auth.h"
extern "C" __declspec(dllexport) void* __stdcall GetClassInstance()
{
    return static_cast<void*>(new TAuth());
}
extern "C" int _libmain(unsigned long reason)
{
    return 1;
}    

现在在测试应用程序中我有:

  • 相同的 BaseAuth.h 文件

  • 带有 Button 的表单:

Test_DLLAuthOrga.h:

#ifndef Test_DLLAuthOrgaH
#define Test_DLLAuthOrgaH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
#include "BaseAuth.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // Composants gérés par l'EDI
    TButton *Button2;
    void __fastcall Button2Click(TObject *Sender);
private:    // Déclarations utilisateur
    TBaseAuth *mpAuth;
public:     // Déclarations utilisateur
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Test_DLLAuthOrga.cpp:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Test_DLLAuthOrga.h"
#include "BaseAuth.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
const wchar_t* library = L"DLLAuthOrga.dll";
extern "C" __declspec(dllimport) void* __stdcall GetClassInstance();
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
    HINSTANCE load;
    try
    { load = LoadLibrary(library); }
    catch(Exception &e)
    { ShowMessage(e.Message); }
    if (load)
    {
        ShowMessage("Library Loaded!");
        void *myFunc;
        myFunc = (void *)GetProcAddress(load, "GetClassInstance");
        mpAuth = (AuthParent*)myFunc;
    }
    else { ShowMessage("Library not loaded!"); }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if (mpAuth == NULL) return;
    try { pRes = mpAuth->TestMessage(); }
    catch(Exception &e) { ShowMessage(e.Message); return; }
}

结果是:

指针 mpAuth 有一个地址。

但是它的方法没有地址,当我调用诸如“void TestMessage()”之类的简单方法时,它会引发访问冲突。

=> 首先似乎是字符串兼容性的问题(但在“C++ Builder XE3”和“C++ Builder XE3”之间,我希望使用相同的字符串格式?!):使用 Unicode Delphi 调用 DLL 时出错

=> 我发现了一个类似的问题,但是 C++ DLL 进入 Delphi,而不是 C++ DLL 进入 C++ ...:在 Delphi 应用程序中调用 C++ DLL

=> 我尝试使用“HMODULE”而不是“HINSTANCE load;” : 同样的结果。

=>我尝试使用没有成功

mpAuth = static_cast<AuthParent*>(myFunc);

代替 :

mpAuth = (AuthParent*)myFunc;

=>我还尝试用“__cdecl”或“”(删除)替换“__stdcall”:库加载但GetProcAdress返回NULL。

=> 在尝试调用 DLL 的方法“TestMessage()”时我做错了什么?

4

1 回答 1

0

要正确绑定 dll 中的函数,您应该给它完整的定义,包括调用约定、参数、返回类型和__dllexport/__dllimport修饰符。最简单的方法 - 使用typedefso 而不是键入(在 Test_DLLAuthOrga.cpp 中)

void *myFunc;
myFunc = (void *)GetProcAddress(load, "GetClassInstance");

利用

typedef __declspec(dllimport) void (__stdcall *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "GetClassInstance");

如果您使用__cdecl约定,您还应该在目标函数名称中添加下划线

typedef __declspec(dllimport) void (__cdecl *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "_GetClassInstance");

您还可AuthParent*以为您的工厂函数显式定义为返回类型,以摆脱不必要的强制转换(在 File1.cppvoid*AuthParent*Test_DLLAuthOrga.cpp 中),因此,最终代码片段如下所示:

typedef __declspec(dllimport) AuthParent* (__cdecl *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "_GetClassInstance");
mpAuth = myFunc();
于 2013-08-05T08:57:11.710 回答