//DLL.CPP
#include <iostream>
#include "libhello.h"
__attribute__ ((constructor)) int init(void)
{
std::cout<<"Init library"<<std::endl;
return 0;
}
__attribute__ ((destructor)) int term(void)
{
std::cout<<"Term library"<<std::endl;
return 0;
}
int printhello(const char* name)
{
std::cout<<"Hello, "<<name<<", from library!"<<std::endl;
return 0;
}
//DLL.H
#ifndef LIBHELLO_H
#define LIBHELLO_H
#ifdef BUILD_DLL
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#endif
extern "C" DLLAPI int init(void);
extern "C" DLLAPI int term(void);
extern "C" DLLAPI int printhello(const char*);
#endif// LIBHELLO_H
//MAIN.CPP
#include <iostream>
#include <windows.h>
int main(int argc, char* argv[])
{
std::cout<<"Loading library "<<"libhello.dll"<<std::endl;
HMODULE hlib = LoadLibrary("libhello.dll");
if(hlib == NULL){
std::cerr<<"Error loading library!"<<std::endl;
return 0;
}
int (*printhello)(const char*) = reinterpret_cast<int (*)(const char*)>(GetProcAddress(hlib,"printhello"));
if(printhello == NULL){
std::cerr<<"printhello() proc not found!"<<std::endl;
}
printhello("user");
FreeLibrary(hlib);
return 0;
}