Mam prosta klasę, i chcę sobie stworzyć bibliotekę dll, aby później uzywać jej w programie. Moje pliki:
class Test{
public:
void sayHello();
};
#include "Test.h"
#include <iostream>
using std::cout;
void Test::sayHello(){
cout << "Hello!\n";
}
extern "C" Test* __declspec(dllexport) stworz()
{
return new Test;
}
extern "C" void __declspec(dllexport) usun(Test* t)
{
delete t;
}
#include "Test.h"
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
HINSTANCE winLib = LoadLibrary("windowsLib.dll");
Test *test;
typedef Test* (*funTyp)();
funTyp utworz = (funTyp)GetProcAddress(winLib, "_stworz");
test = utworz();
test->sayHello();
}
No i wszystko ładnie się kompiluje, problem w tym, że nie wiem, jak z tego kodu (plikow bez maina) stowrzyc taka biblioteke. Wiem, ze musi byc w folderze z moim programem (main.cpp).
Tworze w Code Blocks Dynamic Link Library, i po utworzeniu w projekcie mam takie pliki:
#include "main.h"
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT SomeFunction(const LPCSTR sometext);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
Jak za ich pomoca stworzyc dll z mojej klasy Test.cpp? Bo szukalem, ale odpowiedzi nie znalazlem :(