Compiling and using a DLL from BlitzMax
Attention! 👉Starting November 2024, BlitzCoder.org will now be BlitzBasic.org.
Tweet blitzmax dll library tutorials
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:
- create any folder where you'll create your dll in.
the file names I'll tell you refer to this folder. - create a file "DLLTest.bmx"
- this should be the file content:
Strict
Framework brl.blitz
Function Product ( A , B )
Return A * B
EndFunction
- create a file "DLLTest.def"
- here's the file's content:
EXPORTS
Product=bb_Product
- open a command prompt
- 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.
- 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:
- create a file "DLLCaller.bmx"
- 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
- 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
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 :)
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.
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..
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.
i tried it and i got this error "Cannot export bb_Product: symbol not defined" on the cmd console
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.
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
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?
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.
yea, that was my error. not adding it to the def
everything works fine.. you guys all gonna gain from my understanding.
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
How do i free a library after i loaded it?
is it possible?
I tried to find a way but didnt
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
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..
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