好吧,我已经危险地接近这里的重新发布,但我的情况与许多其他关于此功能的发帖人略有不同。我正在与当天编写的 DLL 进行交互,而我所拥有的只是文件。我没有 .lib 文件,所以我使用 LoadLibrary 和 GetProcessAddress 函数。我按照 MSDN 网站上的教程来了解基本结构。 DLL 位于项目文件夹中。它编译。在运行时,我得到了“hinstLib”的数值,所以我假设找到了 DLL。我得到“ProcAdd”变量的空值。其他张贴者通过将 extern C 放入 DLL 函数中解决了问题,但我真的没有那个选项。更不用说,据我所知,这个 DLL 是用纯 C 语言编写的。我确实有一个接口(interface)文档,并且非常确定我的函数名称是正确的(出于这些目的,用一个通用示例替换)。老实说,我没有运行任何超过 ProcAdd 分配的东西,因为它出现了 NULL。任何关于为什么这给我函数分配的 0 值的任何想法都将不胜感激。注意:很遗憾,由于各种原因,我无法上传 DLL。
#include <iostream>
#include "stdafx.h"
#include "Windows.h"
#include <stdio.h>
typedef int(__cdecl *MYPROC)(LPWSTR);
using namespace std;
int main()
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
hinstLib = LoadLibrary(TEXT("dllName.dll"));
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "funcName");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
//(ProcAdd) (L"Message sent to the DLL function\n");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
return 0;
最佳答案
编译器通常会打乱函数名称,然后一个名为 funcName
的函数可能会出现在 DLL 中,名称为 funcName@16
,例如...这取决于调用约定并且对于正确调用函数很重要。对于 __cdecl
调用约定,您可能需要 _funcName
:-) 。
关于c++ - 未使用 GetProcAddress() C++ VBexpress 13 查找函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23791995/