Calling BMax functions from C

BlitzMax Forums/BlitzMax Programming/Calling BMax functions from C

monotonic(Posted 2009) [#1]
Hi Guys,

I know you can call C functions by creating a BMax wrapper function (so to speak), but is there anyway I could call a BMax function from within a C function.

Any help would be great.


grable(Posted 2009) [#2]
Not in a static way im afraid, you have to pass a pointer to the function to C and then call it.

heres a simple example, using a global variable. but you could pass it via a type or parameter if you wanted.

test.bmx
Framework BRL.Blitz
SuperStrict

Import "test.c"

Extern "c"
	Global bmx_test_func()	
	Function c_test()
EndExtern

Function bmx_test()
	WriteStdout "BlitzMax!~n"
EndFunction

bmx_test_func = bmx_test
c_test()

test.c
void (*bmx_test_func)();

void c_test() {
	puts("calling blitzmax function from c");
	if(bmx_test_func)
		bmx_test_func();
	else
		puts("oops");
}



Brucey(Posted 2009) [#3]
is there anyway I could call a BMax function from within a C function.

Sure you can, and in a static way! :-)

Example.

func_test.bmx
SuperStrict
Framework BRL.StandardIO

Import "func_test.c"

Extern
	Function call_test:Int()
End Extern

Print call_test()


Function myFunction:Int()
	Return 15
End Function


func_test.c

extern int bb_myFunction();
int call_test();

int call_test() {
	return bb_myFunction();
}


:o)


Brucey(Posted 2009) [#4]
I don't like function pointers, Mr Grable :-p


monotonic(Posted 2009) [#5]
Excellent, thanks guys that helps me out a lot, so simple too.


grable(Posted 2009) [#6]
Ooh.. nice, i tried that, but not without the preceding _ . nice to know :)
Hehe brucey, i love them. to each his own right? :P


Brucey(Posted 2009) [#7]
but not without the preceding _

If you are calling a Module Type Function, you need to add an _ to the name: eg :
	void _bah_libtcod_TCODSystem__Flush(bool render);

For plain functions, it's just bb_ in front.
Function names are case-sensitive.