Compiling and using a DLL from BlitzMax


Attention! 👉Starting November 2024, BlitzCoder.org will now be BlitzBasic.org.

 

Tweet blitzmax dll library tutorials
BlitzCoder

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

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 :)

BlitzCoder 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.

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..

BlitzCoder 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.

hardcoal commented:

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

BlitzCoder 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.

hardcoal commented:

im using blitzmax version 1.50

BlitzCoder 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

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?

BlitzCoder 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.

hardcoal commented:

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

hardcoal commented:

Im working on a DLL maker..
I already made it work.. so it produce a Dll
But its not ready for share..

Im also planning to make a module maker..

But now im experimenting with DLLS..

Im still working on my Editor slowly..

And I started learning Unity again.. which I believe is the best Game Developing platform currently

BlitzCoder commented:

Sounds great HC. keep us posted with your work on this. cheers.

hardcoal commented:

How do i free a library after i loaded it?
is it possible?

I tried to find a way but didnt

BlitzCoder commented:

How do i free a library after i loaded it? is it possible? I tried to find a way but didnt

afaik it only works on vanilla blitzmax and not on NG so it should work when you call FreeLibrary above

hardcoal commented:

Anyway.. some update...

Ive managed to do what I desired..

Ive Managed to Edit a DLL While another program is running, and than call it back from the program and see the different outcome.
This means i can Edit Game Code while the Game is running.. and compile only the DLL..
This opens up options ..

Hope it sound Interesting..

hardcoal commented:

Topic moved here: https://www.blitzcoder.org/forum/topic.php?id=834

Moderated by Admin

Reply To Topic (minimum 10 characters)

Please log in to reply