A friend asked me how to write and use a DLL in C++. I told him that I had to check and make sure my understanding was accurate. So here I am. Would this work as a DLL:
myDLL.cpp:
#include "myDLL.h"
void DLL_EXPORT ConstructMyInt(myIntStruct &i){i.val=0;}
void DLL_EXPORT DoSomething(myIntStruct &i){i.val*=i.mul++;}
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
}
myDLL.h:
#include <windows.h>
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct myIntStruct
{
int val;
int mul;
};
void DLL_EXPORT ConstructMyInt(myIntStruct &);
void DLL_EXPORT DoSomething(myIntStruct &);
#ifdef __cplusplus
}
#include "myInt.h"
#endif
myInt.h:
class myInt
{
private:
myIntStruct variables;
public:
myInt(){ConstructMyInt(variables);}
int do(){DoSomething(variables);return variables.val;}
};
myTestProgram.cpp:
#include "myDLL.h"
#include <iostream>
int main()
{
myInt i;
std::cout<<i.do()<<i.do()<<endl;
return 0;
}