0
我已經作出了非常非常簡單的DLL有兩個文件:廣東話負載功能
dllmain.cpp
dll.hpp
dllmain.cpp:
#include "dll.h"
#include <windows.h>
messageimport::messageimport()
{
}
messageimport::~messageimport()
{
}
void messageimport::message()
{
MessageBox(0, "Hello World from DLL!\n","Hi",MB_ICONINFORMATION);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
case DLL_THREAD_ATTACH:
{
break;
}
case DLL_THREAD_DETACH:
{
break;
}
}
/* Return TRUE on success, FALSE on failure */
return TRUE;
}
dll.hpp:
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif
class DLLIMPORT messageimport
{
public:
messageimport();
virtual ~messageimport();
void message();
};
#endif
我從一個名爲dlltes的文件使用dll t.cpp
dlltest.cpp:
#include <iostream>
#include <windows.h>
using namespace std;
typedef unsigned int(WINAPI* message)(void);
int main(int argc, char** argv) {
HMODULE dll = LoadLibrary("Call.dll");
if(dll != NULL){
cout << "loaded dll \n";
message func = (message)GetProcAddress(dll, "message");
if(func != NULL)
{
cout << "found function";
}else{
cout << "cant find function";
}
}else{
cout << "cant load the library";
}
return 0;
}
但是當我運行dlltest.cpp它的輸出是一樣
加載的DLL 找不到功能
所以我需要改變'void message()'這一行''在dll.hpp中'extern「C」void message()'??? –
sry如何定義BUILDING_DLL –
在您的DLL的預處理器設置中。 – drescherjm