Making a DLL

General FreeBASIC programming questions.
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Making a DLL

Post by TheMG »

Is there anything special I have to do to a code for it to work as a DLL? (Other than the -dll command line). If you specifically know how to make it work for a thing called Gamemaker then thatd help!
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Post by Fragmeister »

To make a .dll, it's simple. All code must be contained in functions (or subs, but functions make more sense, as you can return values) Put all the sub/function declarations into a .bi file, so that you can #include it into your programs that use it. Here's an example:

Code: Select all

'test.bas (this is the dll and needs to be compiled with -dll)
#include once "test.bi"
function foo(bar) as integer export
    return bar*2
end function

Code: Select all

'test.bi (this is #included in your dll source as well as the program.
declare function foo alias "foo" (bar) as integer

Code: Select all

'tester.bas:  this is just to test the function
#include once "test.bi"
input bar
print foo(bar)
sleep
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Post by TheMG »

Are you sure? Ill show you my code:

Add.bas

Code: Select all

#Include once "Add.bi"

Function AddIt(N1 as double, N2 as double) as double export

    Return N1+N2
 
End Function
Add.bi

Code: Select all

Declare Function AddIt Alias "AddIt" (N1 as double, N2 as double) as double
Is that all right?

EDIT

It compiles fine, but when I try to execute it Im given an 'error defining external function' (basicly it cant run it).
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Post by VirusScanner »

Not sure, but try changing your function line to read

Code: Select all

function AddIt alias "AddIt"(N1 as double, N2 as double) as double export
make sure to compile with -dll.
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Post by Fragmeister »

hmmm. I described it exactly as I remember doing it. I'll double-check. You might try VirusScanner's suggestion, I don't think that extra alias is necessary, but you never know!
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Post by TheMG »

Well it works, but numbers/strings it returns are all scrambled.

Code: Select all

Function HelloWorld Cdecl Alias "HelloWorld" () As String Export
    
    Return "Hello World"
    
End Function
But the return is all scrambled when run?!
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Post by Fragmeister »

maybe remove cdecl?
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Post by TheMG »

Well first I just left it out, but what do I call it as? Becuase I can call as stdcall or cdecl. When i replace both with stdcall it dont work.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

huh, and when i try to compile a dll, i get this:

Code: Select all

D:\FREEBASIC\bin\win32\dlltool.exe: Syntax error in def file proc.def:0
i think i'll try getting cvs later...
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

That happens when no export function is defined, dlltool doesn't like that for some reason.

Dynamic strings (STRING) can't be used when making DLL's for other languages than FB, they know nothing about the FB STRING descriptor, you have to use ZSTRING's + pointers.
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Post by TheMG »

Ahh yes that is a problem I am having. I have to use ZSTRING, but then it says it cant export fixed length strings. How can we fix this?
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Post by tunginobi »

TheMG wrote:Ahh yes that is a problem I am having. I have to use ZSTRING, but then it says it cant export fixed length strings. How can we fix this?
Try using a ZSTRING PTR instead, and tell us how that pans out.
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Post by TheMG »

So do I just set the function as ZString Pointer Export, ye? And then I use it as normal?

EDIT

I have this:

Code: Select all

Function SayHello Cdecl Alias "SayHello" () As ZString Pointer Export
    
    Dim RV as ZString*5 = "Hello"
    
    Return @RV
    
End Function
But it says my assignment of "Hello" to RV is a type mismatch. Is this even how Im supposed to do it?

EDIT

Also I am implementing peek, poke and varptr into another language (using a dll), but how do I make an argument, isntead of being set to the value of what it is, point to the value of what it is.

Say I had a function called RandomFunc1. It had one argument, a single. I do this:

RandomFunc1(MyVar)

How do I instead of the first argument being the value of MyVar, but be its location. Bear in mind this language Im porting to only has single and string.
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Post by VirusScanner »

The only problem with that is you're returning a stack address that will be invalid after you exit. And you're not letting there be room for the NULL character at the end. Try this:

Code: Select all

function ReturnString cdecl alias "ReturnString" ( ) as zstring ptr export
    dim as zstring ptr sp
    sp = allocate( 6 )
    *sp = "Hello"
    return sp
end function
but that also means that the language you are returning a value from will have to clean that up by calling free (FB calls it deallocate).

I don't see much of a point using a language that has only 2 variable types though. I'd personally tell the author to implement more features.
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Post by TheMG »

I have told the author (link's http://www.gamemaker.nl, its just a simple gamemaker). Ill go try those edits. BTW can the allocate be replaced by *6?
Post Reply