我对 DLL 编程非常陌生。
我创建了一个 DLL 项目,
在 DLL 项目中SimpleH.h
namespace ME{
class base
{
public:
static __declspec(dllexport) void Hello();
};
}
__declspec(dllexport) void HelloWorld();
DLL.cpp
#include "stdafx.h"
#include <iostream>
#include "SimpleH.h"
using namespace std;
namespace ME
{
void base::Hello()
{
cout << "Hello World\n";
}
}
void HelloWorld()
{
cout << "Hello I am world\n";
}
我创建了一个.exe
.
Main.exe
#include "stdafx.h"
#include <stdlib.h>
#include <Windows.h>
#include <iostream>
using namespace std;
typedef void (*FunctionFunc)();
typedef void (*FunctionAno)();
int _tmain(int argc, _TCHAR* argv[])
{
FunctionFunc _FunctionFunc;
FunctionAno _FunctionAno;
HINSTANCE hInstLibrary = LoadLibrary(L"MyDLL.dll");
if(hInstLibrary)
{
/*Problem Happens here*/
_FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary, "Hello");
_FunctionAno = (FunctionFunc)GetProcAddress(hInstLibrary, "HelloWorld");
/**/
if(_FunctionFunc)
{
_FunctionFunc();
}
if(_FunctionAno)
{
_FunctionAno();
}
DWORD error = ::GetLastError();
if (error)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
}
}
FreeLibrary(hInstLibrary);
system("pause");
return 0;
}
hInstance 正在正确更新。但是GetProcAddress()
没有得到更新。请帮我。它哪里出错了?