无法从 C DLL 调用该函数

Not able to call the function from C DLL

本文关键字:调用 函数 DLL      更新时间:2023-10-16
//DLL Code
#include <stdio.h>
extern "C"
{

__declspec(dllexport) void DisplayHelloFromDLL()
    {
        printf("Hello from DLL !n");
    }
}

//Program Accessing DLL
#include<windows.h>
#include<iostream>
#include<conio.h>
typedef void (*DisplayHelloFromDLLFuncPtr)();
using namespace std;

int main()
{
    HINSTANCE hGetProcIDDLL = LoadLibrary("L:\C_Learning\Library\MyLib\Debug\MyLib.dll");
    if (!hGetProcIDDLL)
    {
        cout << "nCould Not The Library";
        return EXIT_FAILURE;
    }
    else
    {
        cout << "nDLL is Loaded";
    }
    DisplayHelloFromDLLFuncPtr LibMainEntryPoint=(DisplayHelloFromDLLFuncPtr)GetProcAddress(hGetProcIDDLL, "DisplayHelloFromDLL");
    if (!DisplayHelloFromDLL)
    {
        cout << "nCould not locate the function";
        return EXIT_FAILURE;
    }
    cout << DisplayHelloFromDLL(); 
    return EXIT_SUCCESS;
    _getch();
    return 0;

}
  1. 代码执行直到 cout 语句处于其他条件。

  2. 编译 DLL 中的函数时收到错误。

  3. 收到错误"显示HelloFromDLL":未声明的标识符

  4. Ran Depends.exe它确认 DLL 地址空间中的函数可用性。

  5. DLL 和示例程序是使用 32 位环境编译的。

    6.程序的唯一目的是调用函数C DLL并打印来自DLL的Hello From消息。

    有什么建议吗?

您将包含"DisplayHelloFromDLL"函数指针的变量命名为"LibMainEntryPoint":

DisplayHelloFromDLLFuncPtr LibMainEntryPoint=(DisplayHelloFromDLLFuncPtr)GetProcAddress(hGetProcIDDLL, "DisplayHelloFromDLL");

但随后您尝试使用不同的名称(DisplayHelloFromDLL(:

if (!DisplayHelloFromDLL) ...

与变量名称一致,代码应该可以工作。将其更改为:

DisplayHelloFromDLLFuncPtr

DisplayHelloFromDLL=(DisplayHelloFromDLLFuncPtr(GetProcAddress(hGetProcIDDLL, ">DisplayHelloFromDLL"(;