Jump to content


Gffi - Dlls Done Right

  • Please log in to reply
2 replies to this topic

#1 Clam


    GMC Member

  • New Member
  • 55 posts

Posted 22 December 2009 - 10:13 AM

GFFI - DLLs Done Right

Ever wished that Game Maker's dll system wasn't so limited? Four string arguments not enough? Want to pass an argument that's not a double? Sick of having to compile a dll to wrap a single measly WinAPI function? GFFI might be what you've been looking for.

FFI stands for Foreign Function Interface. In plain English, GFFI essentially adds new "builtin" functions to your game which provide better dll functionality. As well as supporting C-style char* strings and double, it also supports all the other basic C data types such as: char, int, long, float etc. On top of this, you can even work with C arrays and structs from Game Maker - it's possible to both create them and read existing ones returned by a dll. Overall, the functionality provided is similar to Python's ctypes module and .NET's P/Invoke system.

Usage is simple, simply merge the provided file with your game. Empty scripts are provided to keep the syntax checker happy, they also contain documentation. The actual functions are "injected" by the GFFI dll using parts of Gmapi when the game runs. For those of you wondering about performance, it should be close to external_call. There is a small amount of overhead because everything is dynamic, but all the typecasting to and from double is normally manually done anyway in a handwritten wrapper .

Probably the biggest limitation is Game Maker's 16 argument limit. This means most of the time, defining a dll function is limited to functions with 13 or fewer arguments. I can add extended functions using ds_list if the need is there, but most of the time 13 is an excessive amount of arguments anyway. The second "limitation" is that this dll cannot interact with .NET assemblies (which aren't really true dlls anyway). Game Maker is not truly object oriented and so it's essentially impossible to do this. There's a .NET layer dll floating around that can work around the problem by calling a static function in a handwritten wrapper assembly.

A big thanks to GearGOD and Messy Mind for hosting, you can download it >Here<. Source is included, it's licensed under LGPL so it's usable in commercial games.

I hope people find this DLL useful, I used it as an opportunity to learn some C (and then hastily tacked on a C++ library). I've included a few basic examples of usage - it's perfect for making a few WinAPI calls without writing an ad-hoc wrapper dll.

Update: GM8 was released shortly after I posted this. I've added GM8 support to Version 1.1

Edited by Clam, 06 March 2010 - 12:20 AM.

  • 0

#2 LoopStan


    North-See Developer

  • GMC Member
  • 1398 posts

Posted 23 December 2009 - 12:24 AM

This dll helped me a lot! I haven't tested with my .NET dll yet, but should this also work for that?
  • 0

Posted Image

#3 Clam


    GMC Member

  • New Member
  • 55 posts

Posted 23 December 2009 - 12:55 AM

Unfortunately one thing this can't do is call .NET dlls, getting that to work involves some acrobatics with Managed and Unmanaged C++. I didn't use Microsoft's C++ compiler for this, so it'd be a big effort to port it (especially the assembly code, most of which I didn't write). Some else has written a .NET layer dll here. You'll still need to write a special wrapper dll which only uses doubles and strings.
  • 0