显式调用DLL

Calling a DLL explicitly

本文关键字:DLL 调用      更新时间:2023-10-16

有人能告诉我为什么我的SimpleTest应用程序不显示"Test"吗?DLL加载成功,我只是没有得到任何控制台输出。

SimpleDLL.cpp

#include "stdafx.h"
#include "SimpleDLL.h"
#include "stdafx.h"
#include <iostream>
int Test()
{
    std::cout << "Test" << std::endl;
    return 0;
}

SimpleDLL.h

#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
DECLDIR int Test();
#endif

SimpleTest.cpp

#include "stdafx.h"
#include <iostream>
#include <windows.h>
typedef int (*TestFunc)();
int main()
{
    TestFunc _TestFunc;
    HINSTANCE hInstLibrary = LoadLibrary( _T("SimpleDLL.dll"));
    if (hInstLibrary)
    {
        _TestFunc = (TestFunc)GetProcAddress(hInstLibrary, "Test");
    }
    else
    {
        std::cout << "DLL Failed To Load!" << std::endl;
    }
    if (_TestFunc)
    {
        _TestFunc();
    }
    FreeLibrary(hInstLibrary);
    return 0;
}

您需要在__declspec(...)之前声明extern "C"。这是因为C++在导出C++函数时添加了名称装饰,并且需要将其声明为C函数才能将函数Test导出为"测试"

正如JoesphH所说,您需要extern "C"来防止名称篡改。除此之外,没有指定以下任何一个编译器开关:
  • Gz __stdcall调用约定:"Test"将导出为Test@0
  • Gr __fastcall缩放约定:"Test"将导出为@Test@0

注意:我认为最后一个符号取决于编译器版本,但仍然不仅仅是"测试"

此外,根据我的注释,检查来自GetProcAddress()的返回值,并使用GetLastError()的值来获取返回NULL时失败的原因。