[英]dll under windows
我需要从托管C ++代码动态加载.dll。 下面的代码执行此操作
[DllImport("kernel32.dll", SetLastError = true,
CharSet = CharSet::Unicode, ExactSpelling = true,
CallingConvention = CallingConvention::StdCall)]
static HMODULE LoadLibrary(LPCTSTR lpFileName);
[DllImport("kernel32.dll", SetLastError = true,
CharSet = CharSet::Unicode, ExactSpelling = true,
CallingConvention = CallingConvention::StdCall)]
static BOOL FreeLibrary(HMODULE hModule);
[DllImport("kernel32.dll", SetLastError = true,
CharSet = CharSet::Unicode, ExactSpelling = true,
CallingConvention = CallingConvention::StdCall)]
static FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName);
typedef int (*CFunction)();
int _tmain()
{
HMODULE pLib = LoadLibrary(TEXT("FunctionDLL.dll"));
if(pLib != 0)
{
FARPROC function = GetProcAddress(pLib, "QFunction");
if (function != 0)
MessageBox::Show(Convert::ToString(function()));
FreeLibrary(pLib);
}
else
{
MessageBox::Show(L"Error", L"Couldn't load ell");
Close();
}
return 0;
}
但是函数始终为null。 我认为dll我们错了。 下面的dll代码:
.h文件
#ifdef __FUNCTIONDLL__
#define __FUNCTIONDLL__
#ifdef FUNCTIONDLL_EXPORTS
#define FUNCTIONDLL_API __declspec(dllexport)
#else
#define FUNCTIONDLL_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
FUNCTIONDLL_API int QFunction();
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // __FUNCTIONDLL__
.cpp文件
#include "FunctionDLL.h"
int QFunction()
{
return 42;
}
您的c ++项目中是否有一个.def? 如果不只是添加一个.def
LIBRARY FunctionDLL
EXPORTS
QFunction @1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.