来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]

function call from dll [expression preceding parentheses of apparent call must have (pointer-to-) function type]

本文关键字:指向 类型 函数 表达式 表观 函数调用 DLL 调用 来自 前面      更新时间:2023-10-16

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

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

在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 导出函数并在客户端应用中调用它的正确方法吗?这是解决错误的正确方法吗?

getGreetings

是一个const char*,而不是一个函数,你想要的是使用reinterpret_cast<const char*(*)()>()来使其成为函数而不是变量。

相关文章: