【发布时间】: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