Compiling and using a DLL from BlitzMax


Tweet blitzmax dll library tutorials
(Posted 11 months ago) RonTek

This is an old excerpt from the old forums by fabian which I will turn into a tutorial to show the possibilities that you can use Blitzmax to create and use your very own Windows DLLs. This also produces .a libraries on Vanilla BlitzMax

here's an example:

  1. create any folder where you'll create your dll in.
    the file names I'll tell you refer to this folder.
  2. create a file "DLLTest.bmx"
  3. this should be the file content:
Strict
Framework brl.blitz

Function Product ( A , B )
  Return A * B
EndFunction
  1. create a file "DLLTest.def"
  2. here's the file's content:
EXPORTS
Product=bb_Product
  1. open a command prompt
  2. if you haven't added the "bin" directory of your BlitzMax installation to the "Path" environment variable, you need to change the current directory to this directory using the CD command.
  3. call
bmk.exe makelib bmx-file-path

for debug or

bmk.exe makelib -r bmx-file-path

for release version
("bmx-file-path" needs to be a path to the "DLLTest.bmx" file)
9. the console should output the message below and the dll is now created, it's named "DLLTest.dll"

Linking:DLLTest.dll
Creating library file: C:/BlitzMax/samples/dlltest/DLLTest.a

to test whether the dll is working do the following:

  1. create a file "DLLCaller.bmx"
  2. the file's contents are:
Strict
Framework brl.blitz

Import pub.win32

Local Library = LoadLibraryA ( "DLLTest.dll" )
If Not Library
  WriteStdout "Library not found!~n"
  Return
EndIf
Local Product ( A , B ) = GetProcAddress ( Library , "Product" )
If Not Product
  WriteStdout "Product function not found!~n"
  Return
EndIf
WriteStdout "The product of 3 and 5 is " + Product ( 3 , 5 ) + ".~n"
FreeLibrary Library

Extern "Win32"
  Function FreeLibrary ( Library )
EndExtern
  1. build and run the file, if the output is

The product of 3 and 5 is 15.

you've successfully built and tested a dll using BlitzMax

(Posted 11 months ago) hardcoal commented:

Youre So Great.. Those small things you post.. are life changers..
Simple and to the point..
I never messed with DLLs but it may have a great effect over my programming in the future.. so Good to have this..

Make it simple.. Is a Great policy :)

(Posted 11 months ago) RonTek commented:

cheers HC. Well, that's what I have in mind when I tried to put up this site a few years back and I'm sure you can remember it as you are the first one's on board here.

We'll continue to filter and weed out more stuff and get most from the old forums and archives that really matters to save us more time.

DLL creation and usage is actually pretty straightforward with pure blitzmax, but the Static Lib (.lib only) counterpart is kind of questionable, as I can't find any resources that makes it possible, without wrapping C/C++ functions to C and that's directly importing from source.

(Posted 11 months ago) hardcoal commented:

Im not an expert on either dlls or Lib..
I know a little about dll's but I never used them directly..
I think it may open new doors for me soon.
we shell see..

(Posted 11 months ago) RonTek commented:

I think it may open new doors for me soon. we shell see..

Sounds great.

The only downside with BlitzMax is it does not fully support all types of linking setup, at least what I have tested so far and reading posts on the archived site.

(Posted 9 months ago) hardcoal commented:

i tried it and i got this error "Cannot export bb_Product: symbol not defined" on the cmd console

(Posted 9 months ago) RonTek commented:

i tried it and i got this error "Cannot export bb_Product: symbol not defined" on the cmd console

this could be the def file not seeing by bmx. Make sure you got DLLTest.def in there. Are you using vanilla or NG?

Edit: either way, it works on both with the exception on the FreeLibrary part on NG.

(Posted 9 months ago) hardcoal commented:

im using blitzmax version 1.50

(Posted 9 months ago) RonTek commented:

yes, it should work out of the box with vanilla bmx. I have uploaded a copy: http://s000.tinyupload.com/index.php?file_id=57248309787498432790

(Posted 9 months ago) hardcoal commented:

your example worked.. now i need to figure whats the diff.. thanks

I had to fix something in the Dll Caller... Change Byte Ptr into Int, because it gave an error.

Another question..

I Tried to add on the DllTest.bmx another function.. but the second one didnt work. after compile.. why is that?

(Posted 9 months ago) RonTek commented:

I had to fix something in the Dll Caller... Change Byte Ptr into Int, because it gave an error.

yes, the copy was tested on a blitzmax dx wip version (ng). You should just use the code above with the FreeLibrary for BlitzMax 1.50 aka vanilla

I Tried to add on the DllTest.bmx another function.. but the second one didnt work. after compile.. why is that?

No clue so you have to post the code and the error you are getting. Make sure to add new functions on the def file as well.

(Posted 9 months ago) hardcoal commented:

yea, that was my error. not adding it to the def
everything works fine.. you guys all gonna gain from my understanding.

Reply To Topic (minimum 10 characters)

Please log in to reply