Author Topic: general rules on making dlls  (Read 4465 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
general rules on making dlls
« on: October 28, 2006 »
ive made a dll and its not working quite right its the first dll ive made and i was wondering.

is it ok to / declare global variables inside the dll / do initialization code like gl stuff at the top of the dll or does a dll have to contain functions only?

also for functions that are to be used internally do i still have to declare them in the .bi file or is t ok to drop the declaration in the dll just like a normal one  and declare them private.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: general rules on making dlls
« Reply #1 on: October 28, 2006 »
You can't use global variables in dlls and any code outside of functions won't be run.
« Last Edit: October 28, 2006 by Stonemonkey »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: general rules on making dlls
« Reply #2 on: October 28, 2006 »
ahh as i thought then cheers mate,

hmm some of my functions require global variables is there any way to mabey create them in the .bi file and let the dll use them also i have a couple of types that my dll uses there in the .bi file so is that cool?
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: general rules on making dlls
« Reply #3 on: October 28, 2006 »
Not sure about that, as far as i know any variables have to be passed to the function.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: general rules on making dlls
« Reply #4 on: October 28, 2006 »
guess ill set some tests up and post what my findings are its not a reall problem though as a simple hack will sort it out.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: general rules on making dlls
« Reply #5 on: October 29, 2006 »
ohh yeah before i forget does anyone know of a way of setting my eviroment varibles for windows xp so that i can run fbc from any location under the cmd prompt its just the way its set up now i have to have my dll right next to fbc every time i want to compile it and its a bit of hassle.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: general rules on making dlls
« Reply #6 on: October 29, 2006 »
You need to have something like
Code: [Select]
c:\program files\freebasicin your path.

Right click 'My Computer'->Properties
Go to 'Advanced' tab.
Click 'Environment Variable' button at the bottom.
In 'User variables for Nino' locate the 'Path' entry and click the 'Edit' button.
To the front of the text box add
Code: [Select]
c:\program files\freeebasic;NB. The semicolon at the end is very important.
Click 'OK'.

The changes will take place when you next open a cmd window.  Any existing windows or programs wont see the change.


DLLS - There is a function called DllMain() that you can declare that has all your dll startup code in it.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: general rules on making dlls
« Reply #7 on: October 29, 2006 »
cheers jim.

cool so i can just do somthing like this in my dll.

sub DllMain()

......init crap

end sub

and that will automatically run at constuction time?

does private functions still have to be declared in the .bi file?

and what about globals vars is it possible to have them in the .bi file and use them in the dll?

sorry for all the questions im very curious :)
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: general rules on making dlls
« Reply #8 on: October 29, 2006 »
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/dllmain.asp
Depending what fdwReason is you can call functions when the dll is (un)loaded or a thread in your app starts or stops.

I don't know if global variables will work like you want them to.  Each process/thread that loads a dll gets a fresh copy of the dll's data space - that means variables aren't shared.  It's possible to arrange for variables that are shared but I don't think freebasic's compiler and linker allow for that.  It's possible.

A better solution is to use get_ and set_ functions in the dll.  Don't worry about speed, worry about making it work.
So in your app, call dll functions...
Code: [Select]
lives = get_n_lives();
put_n_lives(lives);

Jim
<edit in italics - s/aren't/are>

« Last Edit: October 29, 2006 by Jim »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: general rules on making dlls
« Reply #9 on: October 29, 2006 »
yeah thats what i was thinking of doing also i think id be better just having an init function and let other people call it at the top of there code.

cheers for the heads up jim im sure i can get it going now ;)
Challenge Trophies Won: