Oh man.. This is might sound rough (I don't mean to be if it seems that way).. You might want to learn how to export functions from a dll before attempting to make any sort of plugin.
1. The declaration of dllmain isn't exactly right. The declaration for dllmain looks like:
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);
and if you decided to use the regular types instead of the microsoft typedefs, then it would look like:
bool __stdcall DllMain(HINSTANCE hinstDLL, unsigned int fdwReason, void* lpReserved);
Yes. You missed the calling convention and a DWORD is unsigned int. Not just int. Thought just int would still work, the calling convention you had was cdecl and that isn't correct.
2. You never exported the function "Add". Any program including Simba won't be able to get the function's address. This seems to be the problem you are currently having. Exporting it should fix it.
3. You might need C-Linkage for dllmain.
4. You don't exactly need a header for a basic plugin. You can have one but it isn't "required".
5. To specify C linkage for all functions, then you do:
extern "C"
{
void Add (int a, int b , int & res);
GetPluginABIVersion();
GetFunctionCount() ;
GetFunctionInfo(int index, void*& address, char*& def);
}
All in all, your code would then be:
#if defined _WIN32 || defined _WIN64
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <cstring>
#include <iostream>
#define NumExports 1
#if defined _WIN32 || defined _WIN64
HMODULE dllinst = NULL;
#endif
static char* exports[] =
{
(char*)"Add",(char*)"procedure Add(a, b :integer; var result : integer);"
};
extern "C" void __declspec(dllexport) Add (int a, int b , int & res)
{
res = a + b;
}
extern "C" int __declspec(dllexport) GetPluginABIVersion()
{
return 2;
}
extern "C" int __declspec(dllexport) GetFunctionCount()
{
return NumExports;
}
extern "C" int __declspec(dllexport) GetFunctionInfo(int index, void* &address, char* &def)
{
if (index < NumExports)
{
#if defined _WIN32 || defined _WIN64
address = (void*)GetProcAddress(dllinst, exports[index * 2]);
#else
address = dlsym(RTLD_DEFAULT, exports[index * 2]);
#endif
strcpy(def, exports[index * 2 + 1]);
return index;
}
return -1;
}
#if defined _WIN32 || defined _WIN64
extern "C" __declspec(dllexport) bool __stdcall DllMain(HINSTANCE instance, unsigned int reason, void* checks)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
{
dllinst = instance;
std::cout<<"Plugin Loaded..\n";
}
break;
case DLL_PROCESS_DETACH:
{
std::cout<<"Plugin UnLoaded..\n";
}
break;
}
return true;
}
#else
void OnProcessAttach() __attribute__((constructor))
{
std::cout<<"Plugin Loaded..\n";
}
void OnProcessDetach() __attribute__((destructor))
{
std::cout<<"Plugin UnLoaded..\n";
}
#endif