SRL Archive - Development

C++ Simba Plugin problems


moparisthebest

2010-09-22 17:23:03

I'm trying to figure out the proper way to write and compile a Simba plugin so it will work on all OSs and architectures, the first step of course is to simplify it as much as possible and get it to work on win32 Simba. I took yakman's markov plugin and converted it to this:

#include <windows.h>

#include <assert.h>
#include <cstdio>
#include <cstring>

#include <string>

using namespace std;



//plugin stuff below

#define DLL_EXPORT _declspec(dllexport)
#define SIMBA_EXPORT DLL_EXPORT __stdcall

//#define SIMBA_EXPORT __stdcall
//#define DLL_EXPORT __stdcall

#ifdef __cplusplus
extern "C" {
#endif

int SIMBA_EXPORT
fast_add_ten(int num) {
return num + 10;
}

char* function_defs[] = {
(char*)"function FastAddTen(num: Integer): Integer;"
};

void* function_ptrs[] = {
(void*)&fast_add_ten
};

int SIMBA_EXPORT
GetFunctionCount(void) {
assert(sizeof(function_defs) == sizeof(function_ptrs));
//printf("GetFunctionCount()\n");
return sizeof(function_defs) / sizeof(char*);
}

int SIMBA_EXPORT
GetFunctionInfo(int x, void** proc_addr, char** proc_def) {
//printf("GetFunctionInfo(%d, 0x%08x, 0x%08x)\n", x, proc_addr, proc_def);

if(x < 0 || x > sizeof(function_defs) / sizeof(char*))
return -1;

*proc_addr = function_ptrs[x];
strcpy(*proc_def, function_defs[x]);
return x;
}

DLL_EXPORT BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
//AllocConsole();
return TRUE;
}

#ifdef __cplusplus
}
#endif

I compile it with this makefile:
CC := i586-mingw32msvc-g++
PLATFORM_FLAGS := -mwindows -shared -s
CFLAGS := -O2 -fomit-frame-pointer

OUTPUT := ./libtest.dll

default: $(OUTPUT)

$(OUTPUT):
$(CC) $(CFLAGS) $(PLATFORM_FLAGS) -o $(OUTPUT) test.cpp

clean:
rm -f *.o *.so *.dll

Here is my simba script:
program new;
{.loadlib libtest}
var
num: Integer;
begin
ClearDebug;
num := 32;
Writeln('num = ' + IntToStr(num));
Writeln('num := num + 10;');
num := num + 10;
Writeln('num = ' + IntToStr(num));
Writeln('num := FastAddTen(num);');
num := FastAddTen(num);
Writeln('num = ' + IntToStr(num));
end.

Which produces:
Loading plugin libtest at /home/mopar/simba/test/Plugins\
formWriteln: [Error] (14:8): Unknown identifier 'FastAddTen' at line 13
formWriteln: Compiling failed.

Now the big question: What did I do wrong?

Nava2

2010-09-22 19:08:48

Is it in the plugins folder, did you restart simba? Thats all I got, sorry about that.

moparisthebest

2010-09-22 20:47:51

From: Nava2
Is it in the plugins folder, did you restart simba? Thats all I got, sorry about that.


Yep, and multiple times. It says it loads the plugin, but the function I defined doesn't show up.

Wizzup?

2010-10-15 17:16:42

Well, I don't see SIMBA_EXPORT being defined ATM. (You commented it out) But the calling convention is indeed stdcall.