【问题标题】:function call from dll [expression preceding parentheses of apparent call must have (pointer-to-) function type]来自 dll 的函数调用 [明显调用括号前的表达式必须具有(指向)函数类型]
【发布时间】:2019-03-27 12:54:32
【问题描述】:

我对 c++ 完全陌生,并试图创建一个示例 dll 和一个从 dll 调用函数的客户端。

我在一个 dll 和一个控制台中创建了一个使用 VC++ 和两个项目的解决方案。

在 plugin_dll 项目中,我有一个头文件和一个 cpp 文件:

plugin.h    
    #pragma once
    #define EXPORT extern "C" __declspec (dllexport)
    EXPORT char const* Greetings();

plugin.cpp
    #include "stdafx.h"
    #include "plugin.h"

    char const * Greetings()
    {
        return "Hello From  Plugin";
    }

在我的控制台应用项目中

#include "pch.h"
#include "stdafx.h"
#include <iostream>

using namespace std;
int main()
{

    HMODULE DllHandler = ::LoadLibrary(L"plugin.dll");
    char const* const getGreetings=reinterpret_cast<char const*>(::GetProcAddress(DllHandler, "Greetings"));
    cout << getGreetings() << endl; // Here I get the Error
    cin.get();
 }

在 cout 行我得到错误

E0109   expression preceding parentheses of apparent call must have (pointer-to-) function 

和编译时错误

C2064   term does not evaluate to a function taking 0 arguments 

首先,这是创建 dll 导出函数并在客户端应用程序中调用它的正确方法吗?这是解决错误的正确方法吗?

【问题讨论】:

  • 在调用 GetProcAddress 之前检查 DllHandler == NULL。其他替代方法是加载 *.lib 文件(使用 #pragma 指令)而不是 dll。

标签: c++ visual-c++ dll casting dllexport


【解决方案1】:

getGreetings 是一个const char*,而不是一个函数,你想要的是使用reinterpret_cast&lt;const char*(*)()&gt;() 来代替它在一个函数而不是一个变量中。

【讨论】:

  • 谢谢,所以我将其分隔为“使用 type=const char*()();”然后直接投 "(type)(GetProcAddress(,*)) "
  • 是的,确实在两步中更具可读性,但不要使用 C 样式转换,使用 reinterpret cast&lt;type&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
相关资源
最近更新 更多