Making a DLL
Making a DLL
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!
-
- Posts: 545
- Joined: Nov 08, 2005 14:36
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
Are you sure? Ill show you my code:
Add.bas
Add.bi
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).
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
Code: Select all
Declare Function AddIt Alias "AddIt" (N1 as double, N2 as double) as double
EDIT
It compiles fine, but when I try to execute it Im given an 'error defining external function' (basicly it cant run it).
-
- Posts: 775
- Joined: Jul 01, 2005 18:45
Not sure, but try changing your function line to read
make sure to compile with -dll.
Code: Select all
function AddIt alias "AddIt"(N1 as double, N2 as double) as double export
-
- Posts: 545
- Joined: Nov 08, 2005 14:36
Well it works, but numbers/strings it returns are all scrambled.
But the return is all scrambled when run?!
Code: Select all
Function HelloWorld Cdecl Alias "HelloWorld" () As String Export
Return "Hello World"
End Function
-
- Posts: 545
- Joined: Nov 08, 2005 14:36
huh, and when i try to compile a dll, i get this:
i think i'll try getting cvs later...
Code: Select all
D:\FREEBASIC\bin\win32\dlltool.exe: Syntax error in def file proc.def:0
So do I just set the function as ZString Pointer Export, ye? And then I use it as normal?
EDIT
I have this:
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.
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
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.
-
- Posts: 775
- Joined: Jul 01, 2005 18:45
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:
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.
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
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.
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?