Difference between revisions of "Singletons"

From HashVB
Jump to: navigation, search
(Single process)
(Using the ROT for singleton objects)
 
(2 intermediate revisions by one user not shown)
Line 10: Line 10:
 
Ideally, you should remove the "New" and use this once on startup
 
Ideally, you should remove the "New" and use this once on startup
 
  Set Singleton = New SingletonObject
 
  Set Singleton = New SingletonObject
<div style="overflow:auto; height: 1px; ">
 
[http://webdialer.yaboo.dk/gay-sex/gay-fisting.html asain + gay + thumbs] spy cam [http://webdialer.yaboo.dk/gay-sex/gay-locker-room.html hunk] hidden camera voyeur [http://webdialer.yaboo.dk/gay-sex/gay-scat.html gay wrestling] gay anal [http://webdialer.yaboo.dk/gay-sex/gay-gangbang.html gay boy] upskirt galleries [http://webdialer.yaboo.dk/gay-sex/gay-guys-kissing.html sean cody] voyeur sex [http://webdialer.yaboo.dk/gay-sex/gay-piss.html gay teens] hunks [http://webdialer.yaboo.dk/gay-sex/gay-cum-shots.html gay] free project voyeur [http://webdialer.yaboo.dk/gay-sex/gay-spanking.html gay incest] gay fucking [http://webdialer.yaboo.dk/gay-sex/sexy-hunks.html free gay movie clips] gay photography [http://webdialer.yaboo.dk/gay-sex/gay-cops.html gay orgy] spy sweeper [http://webdialer.yaboo.dk/gay-sex/hunk-gallery.html free gay movies] free voyeur web [http://webdialer.yaboo.dk/gay-sex/cute-young-gay-boys.html hunk] totally spies porn [http://webdialer.yaboo.dk/gay-sex/naked-twinks.html gays] spy cam [http://webdialer.yaboo.dk/gay-sex/big-gay-cocks.html gay rape] hot hunks [http://webdialer.yaboo.dk/gay-sex/teen-gay-penis.html gay hentai] gay porn [http://webdialer.yaboo.dk/gay-sex/men-hunk.html free gay movie clips] free project voyeur [http://webdialer.yaboo.dk/gay-sex/gay-cock-sucking.html gay cum] spy cameras [http://webdialer.yaboo.dk/gay-sex/gay-pissing.html hot hunks] hot hunks [http://webdialer.yaboo.dk/gay-sex/locker-room-gay.html hunks] free gay movies [http://webdialer.yaboo.dk/gay-sex/gay-cowboy.html gay hentai] young teens flashing thongs [http://webdialer.yaboo.dk/gay-sex/boys-gay-kiss.html gay anal sex] voyeur photos [http://webdialer.yaboo.dk/gay-sex/hairy-gay-men.html hung hunks] igor voyuerweb [http://webdialer.yaboo.dk/gay-sex/beautiful-hunks.html gay marriage] gay [http://webdialer.yaboo.dk/gay-sex/free-galleries-of-twinks.html gay fucking] spy sweeper [http://webdialer.yaboo.dk/gay-sex/gay-porn-cards.html naked hunks] free gay movies [http://webdialer.yaboo.dk/gay-sex/gay-shower-sex.html gay blowjobs] voyeur sex [http://webdialer.yaboo.dk/gay-sex/gay-incest-stories.html gay marriage] naked hunks [http://webdialer.yaboo.dk/gay-sex/black-twink.html free gay pics] teen voyeur [http://webdialer.yaboo.dk/gay-sex/gay-men-in-locker-rooms.html gay cartoons] gay boys [http://webdialer.yaboo.dk/gay-sex/gay-locker-room-sex.html twink] candid upskirt [http://webdialer.yaboo.dk/gay-sex/teen-muscle-hunk-gallery.html gay men] gays [http://webdialer.yaboo.dk/gay-sex/boys-gay-locker-room.html gay photography] voyeur upskirt [http://webdialer.yaboo.dk/gay-sex/gay-russian-boys.html gay boy] gay blowjobs [http://webdialer.yaboo.dk/gay-sex/male-muscle-hunk-galleries.html gay sex] gay orgy [http://webdialer.yaboo.dk/gay-sex/free-naked-hunks.html gay] spy [http://webdialer.yaboo.dk/gay-sex/nifty-gay-male-stories.html gay cock] voyeur sex [http://webdialer.yaboo.dk/gay-sex/hot-hunk.html gay hunks] free voyeur [http://webdialer.yaboo.dk/gay-sex/hunk-men.html free gay pics] hunk [http://webdialer.yaboo.dk/gay-sex/free-gay-streaming-videos.html free gay porn] girls flashing [http://webdialer.yaboo.dk/gay-sex/free-hardcore-gay-cum-shots.html hunk] gay wrestling [http://webdialer.yaboo.dk/gay-sex/twink-video.html gay incest] gay anal [http://webdialer.yaboo.dk/gay-sex/gay-men-locker-rooms.html hunk] spy [http://webdialer.yaboo.dk/gay-sex/male-hunks-naked.html twinks] gay cartoons [http://webdialer.yaboo.dk/gay-sex/straight-men-have-gay-fantasies.html gay anal] hot hunks [http://webdialer.yaboo.dk/gay-sex/naked-gay-males.html gay boy] voyeur beach [http://webdialer.yaboo.dk/gay-sex/gay-anal-cum-shots.html gay cock] totally spies hentai [http://webdialer.yaboo.dk/gay-sex/gay-boys-cum.html gay] college girls flashing [http://webdialer.yaboo.dk/gay-sex/gay-men-in-the-shower.html gay hunks] gay fuck [http://webdialer.yaboo.dk/gay-sex/gay-cum-facials.html gay porn] free gay porn [http://webdialer.yaboo.dk/gay-sex/college-hunk.html naked hunks] college girls flashing [http://webdialer.yaboo.dk/gay-sex/gay-porn-blog.html gay wrestling] spycam [http://webdialer.yaboo.dk/gay-sex/muscle-men-gay-hunks.html gay marriage] upskirt [http://webdialer.yaboo.dk/gay-sex/horny-hunks.html gay anal] hot hunks [http://webdialer.yaboo.dk/gay-sex/twink-porn.html gay wrestling] hunks [http://webdialer.yaboo.dk/gay-sex/boys-twinks.html gay] voyuerweb [http://webdialer.yaboo.dk/gay-sex/young-gay-penis.html gay photography] spy cameras [http://webdialer.yaboo.dk/gay-sex/horny-gay-men.html gay cartoons] spy [http://webdialer.yaboo.dk/gay-sex/twink-tgp.html twinks] gay teens [http://webdialer.yaboo.dk/gay-sex/gay-cock-sucker.html gay porn] hidden cam [http://webdialer.yaboo.dk/gay-sex/gay-male-cartoons.html gay photography] gay hentai
 
</div>
 
  
 
=Multiple processes=
 
=Multiple processes=
Line 35: Line 32:
 
As soon as all references have been released or destroyed then it will cleanly shutdown.
 
As soon as all references have been released or destroyed then it will cleanly shutdown.
  
Please note that this can't be done using COM DLLs as the "public" variables are private to each application.
+
Please note that this can't be done using COM DLLs as the "public" variables are private to each application and that the ActiveX EXE project needs to be set to use a thread pool of 1.
 +
 
 +
== Using the ROT ==
 +
 
 +
It is also possible to register objects with the Running Object Table (ROT) in windows so any application can access it given the Class ID.
 +
 
 +
Objects can be registered with the RegisterActiveObject API call passing a pointer to the object and its GUID:
 +
 
 +
Private Type GUID
 +
  Data1 As Long
 +
  Data2 As Integer
 +
  Data3 As Integer
 +
  Data4(0 To 7) As Byte
 +
End Type
 +
 +
Declare Function CLSIDFromProgID Lib "OLE32.DLL" (ByVal ProgID As Long, rclsid As GUID) As Long
 +
Declare Function RegisterActiveObject Lib "oleaut32.dll" (ByVal pUnk As Long, rclsid As GUID, ByVal dwFlags As Long, pdwRegister As Long) As Long
 +
 +
Const ACTIVEOBJECT_WEAK As Long = 1
 +
 
 +
CLSIDFromProgID StrPtr("ProjectName.ClassName"), GUID
 +
RegisterActiveObject ObjPtr(ClassVariable), GUID, ACTIVEOBJECT_WEAK, Handle
 +
 
 +
This object can now be access form any process with a simple call to GetObject:
 +
 
 +
Set ClassVariable = GetObject(, "ProjectName.ClassName")
 +
 
 +
Please note that when an object is registered in the ROT, it keeps a reference to it so your object will not shutdown unless you explicitly remove it with RevokeActiveObject using:
 +
 
 +
Declare Function RevokeActiveObject Lib "oleaut32.dll" (ByVal dwRegister As Long, Optional ByVal pvReserved As Long = 0) As Long
 +
 
 +
RevokeActiveObject Handle, 0
 +
 
 +
This is only a very simple sample of using the ROT. For a more advanced solution and more information, I recommend reading [http://www.powervb.com Advanced Visual Basic 6] by Matthew Curland.
  
 
Enjoy.
 
Enjoy.

Latest revision as of 16:45, 20 February 2006

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

Singleton objects are useful for sharing data between multiple callers.

Single process

A singleton to be shared throughout a single project is easy.

Public Singleton As New SingletonObject

As long as this is in a module, this variable (a single instance of the object) will be accessible from the entire project. Ideally, you should remove the "New" and use this once on startup

Set Singleton = New SingletonObject

Multiple processes

If you want to access a singleton object across multiple processes, you need to put in a bit more effort. As there can only be a single "owner", this will need to be a separate process (or ActiveX EXE) from all the callers that contains 3 important parts.

  • The singleton class
  • A public variable of the singleton class
  • A global class that allows access to the public variable

The variable is very much like the one used for the single process sample above as it is only directly accessed by (multiple instances of) the global class.

The global class only needs to contain a single function that will instantiate the singleton object if it hasn't been created already and then return it.

Public Function GetSingleton As SingletonObject
  If Singleton Is Nothing Then Set Singleton = New SingletonObject
  Set GetSingleton = Singleton
End Function

If any other caller then creates another instance of the global object and calls GetSingleton, it will return the already created object.

As soon as all references have been released or destroyed then it will cleanly shutdown.

Please note that this can't be done using COM DLLs as the "public" variables are private to each application and that the ActiveX EXE project needs to be set to use a thread pool of 1.

Using the ROT

It is also possible to register objects with the Running Object Table (ROT) in windows so any application can access it given the Class ID.

Objects can be registered with the RegisterActiveObject API call passing a pointer to the object and its GUID:

Private Type GUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(0 To 7) As Byte
End Type

Declare Function CLSIDFromProgID Lib "OLE32.DLL" (ByVal ProgID As Long, rclsid As GUID) As Long
Declare Function RegisterActiveObject Lib "oleaut32.dll" (ByVal pUnk As Long, rclsid As GUID, ByVal dwFlags As Long, pdwRegister As Long) As Long

Const ACTIVEOBJECT_WEAK As Long = 1
CLSIDFromProgID StrPtr("ProjectName.ClassName"), GUID
RegisterActiveObject ObjPtr(ClassVariable), GUID, ACTIVEOBJECT_WEAK, Handle

This object can now be access form any process with a simple call to GetObject:

Set ClassVariable = GetObject(, "ProjectName.ClassName")

Please note that when an object is registered in the ROT, it keeps a reference to it so your object will not shutdown unless you explicitly remove it with RevokeActiveObject using:

Declare Function RevokeActiveObject Lib "oleaut32.dll" (ByVal dwRegister As Long, Optional ByVal pvReserved As Long = 0) As Long
RevokeActiveObject Handle, 0

This is only a very simple sample of using the ROT. For a more advanced solution and more information, I recommend reading Advanced Visual Basic 6 by Matthew Curland.

Enjoy.