oo dll tutorial

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

oo dll tutorial

Post by idle »

Since there's been a few questions raised about creating dll's I thought an oo tutorial could be in order.

The advantages of using oo methods to make dll's should become fairly obvious

1) No Global vars
2) makes it easier to develop and test the code
3) makes it easier to use and maintain
4) the code is dll ready
5) you only need to export an objects constructor to use from a dll
6) protects your code from unauthorized use, without the interface your codes more or less a mystery

The only disadvantage is getting around dll string boundary issues which means you have to return strings by their address
requiring that the client uses PeekS to get at the string. So any functions that returns a string simply returns the string by address

Code: Select all

;in a class functions return   
ProcedureReturn @*this\SomeString  
;in the client 
PeekS(myObject\pstrGetSomeString())
There are other methods to handle strings but I think this is the simplest method.

For example to use the object from a dll is no different than using the code from source
copy and paste the public part of the class header then add the import

Code: Select all

;-Queue class public header copy and pasted from the class 
Enumeration 1 
  #QueueType_Fifo
  #QueueType_Lifo
EndEnumeration 

;-Methods exposed 
Interface Queue 
   Set(item.i)         ;Add item into the queue
   Get.i()                ;Get item from the queue
   SetGet.i(item.i) ;Add and Get an item from the queue
   Count.i()            ;Get the size of the queue  
   pStrAbout.i()      ;pointer to string About 
   Free()                 ;free the class memory 
EndInterface 
;-End class public header copy and pasted from the class 

;Then Add the import for the constructor   
Import "queue.lib"
  New_Queue(type.i)
EndImport 

Makes it a whole lot easier to develop and test, debugging dlls can drive you nuts, this way it doesn't!

Here's the tutorial, it's fairly short on explanations and comments but hopefully it's enough.
Please feel free to add to it, flame it or what ever!

http://www.idlearts.com/ootutorial.zip
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
skywalk
Addict
Addict
Posts: 4220
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: oo dll tutorial

Post by skywalk »

Thanks for writing this up idle. :wink:
The pdf shows all dots for the code segments?
Maybe its just me, can't load embedded font courier 10pitch-BT bold?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: oo dll tutorial

Post by idle »

That's weird, It's possibly an issue with the embedded fonts, I'll see if changing them to true type fixes it for you.
[edit]
I'm blind I didn't even see that you had told me it couldn't load the font. :lol:

I've uploaded it again it still has the bad font there somewhere in the document
though it is working on my XP vm using the Sumarta pdf viewer (no adobe on it)
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
skywalk
Addict
Addict
Posts: 4220
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: oo dll tutorial

Post by skywalk »

Cool, it's readable now. :D
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply