Creating DLL Files with Free Basic

Use of external libraries (DLLs) from ZGE.

Moderator: Moderators

Post Reply
StevenM
Posts: 149
Joined: Thu Jan 20, 2011 10:03 am

Creating DLL Files with Free Basic

Post by StevenM »

It's very easy to create a DLL with free basic.

You need:
http://www.freebasic.net/index.php/about

IDE FBIDE:
http://fbide.freebasic.net/

Here are a couple Free Basic Code Examples for creating a DLL.

A messagebox created with a sub-routine:

Code: Select all

#include once "windows.bi"
Extern "Windows-MS"
sub msgBox Alias "msgBox"(byval msg as string, byval title as string) Export 
  MessageBox( null, msg, title, MB_OK )  
End sub
end extern
A function that adds two numbers:

Code: Select all

Extern "Windows-MS"
Function add2 Alias "add2"(byval x as integer,byval y as integer) As Integer export
    function = x+y
End Function
end extern
Subroutines and functions are the same except the functions return a value.

Some things that tripped me up:
Alias "your_function_name" statement - prevents name mangling - do not forget this or you won't be able to call from ZGE.

The default calling convention is stdcall. If you do not use <Extern> and <end extern> - You won't be able to call it from ZGE.

Export is a necessary keyword when exporting a DLL.

To compile a DLL - you can use the IDE or use a command promt.

From the IDE:

In the Fbide menu view-settings, Click on the freebasic tab. This shows you compiler options. You will see a default Compiler command that looks like this:

Code: Select all

*<$fbc>* *<$file>*
Change it to this:

Code: Select all

*<$fbc>* -dll  *<$file>*
click the menu-->run-compile. The DLL will be found in the folder that contains your source code.

Here are the examples with zge project files - add2 example only outputs to the log window.
Attachments
dll_add2.zip
(8.94 KiB) Downloaded 1079 times
dll_msg.zip
(9.67 KiB) Downloaded 1082 times
Post Reply