好的,所以我在这里危险地接近转发,但我的情况与有关此功能的许多其他海报有点不同。我正在与过去编写的 DLL 进行交互,而我所拥有的只是文件。我没有 .lib 文件,所以我使用的是 LoadLibrary 和 GetProcessAddress 函数。我按照 MSDN 网站上的教程获得了基本结构。DLL 位于项目文件夹中。它编译。在运行时,我得到了“hinstLib”的数值,所以我假设找到了 DLL。我得到“ProcAdd”变量的空值。其他海报通过将 extern C 放入 DLL 函数中解决了问题,但我真的没有这个选项。更不用说,据我所知,这个 DLL 是用纯 C 语言编写的。我确实有一个接口文档,并且很确定我的函数名称是正确的(为了这些目的,用一个通用示例替换)。老实说,我没有通过 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;
}