Results 1 to 14 of 14

Thread: Attempt #1 to implement Matt Curland's Singleton [Resolved + New Complete Source]

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Resolved Attempt #1 to implement Matt Curland's Singleton [Resolved + New Complete Source]

    All,

    I've tried to implement the Singleton project - discussed in Matt Curland's "Advanced Visual Basic 6". He supplies a sample project called TimeServer which implements the ROTHook lightweight Object.

    Of course his code works fine. But when I tried to create a StateServer based on his Client and Server projects, I get a slightly broken scenario.

    As seen from this figure (the 4 Clients are on the left, the Server is on the right):
    http://www.vbforums.com/attachment.p...postid=1821655

    The first client I load is never in sync with either the server or with the rest of the clients. However every other Client loaded after the 1st (2nd, 3rd, 4th, etc) works correctly and as expected.

    You will need to own a copy of the book to run my projects, so if you do, here they are:
    http://www.vbforums.com/attachment.p...postid=1821658

    I've included both the Client EXE and the ActiveX EXE Server.

    It's as if the 1st Client is getting it's own instance of "a" server, but not the one that's running. However, when the 1st client gets loaded, the server does get loaded.
    Last edited by Dave Sell; Oct 26th, 2004 at 01:54 PM.

  2. #2

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    There can be only one...
    http://www.vbforums.com/showthread.p...hreadid=310205

    The source I am including does not contain the VBoost Objects.
    Attached Files Attached Files

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    You are still missing the ROTHook objects

    Can't get the project to run. Since this is where the most important code is based it would be a good idea to include it in the project

    What is it you're trying to do exactly as I am not familiar with the book...?

    Woka

  4. #4

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Originally posted by Wokawidget
    You are still missing the ROTHook objects
    Can't get the project to run. Since this is where the most important code is based it would be a good idea to include it in the project
    What is it you're trying to do exactly as I am not familiar with the book...?
    Woka
    Hey, Woka.

    Thanks for replying. I cannot post that ROTHook source as it is copyright'ed. You can get the source by getting the book. I think you can also get the source at the desaware website... I made the executables in the other thread so you don't have to compile it yourself. I will post them again here.

    I tried explaining everything I am trying to do in that other thread:
    http://www.vbforums.com/showthread.p...hreadid=310205

    But basically I was successful at making many clients talk to the same ActiveX EXE server, as long as both client and server are on the same machine.

    I went a step further and tried to make the clients work on remote client machines a-la DCOM. It works when the client is the 1st thing that instantiates the server, but no client can get a handle to a server that is running already.

    So my original post here that was broken is now fixed. I just took it to a new level (DCOM) and it's still not where I need it to be.

    I guess that means this thread should be resolved?

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Hmmm...copyrighted...Hmmmmmm...and when's that ever stopped anyone

    I see your point though.

    But it's hard for me to debug code, which I don't have

    I don't have a clue what the code is doing soooo...I'll take your word for it that it's fixed

    Woof

  6. #6

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    These wouldn't upload before. I'll try again. You can run these executables.
    Attached Files Attached Files

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Here is the ROT code I use, and dont recall any problems with it..

    VB Code:
    1. Private Type GUIDs
    2.     Data1 As Long
    3.     Data2 As Integer
    4.     Data3 As Integer
    5.     Data4(0 To 7) As Byte
    6. End Type
    7.  
    8. 'Declares needed to register object in the ROT
    9. Private Const ACTIVEOBJECT_STRONG = 0
    10. Private Const ACTIVEOBJECT_WEAK = 1
    11. Private Declare Function CLSIDFromProgID Lib "ole32.dll" (ByVal ProgID As Long, rclsid As GUIDs) As Long
    12. Private Declare Function CoDisconnectObject Lib "ole32.dll" (ByVal pUnk As IUnknown, pvReserved As Long) As Long
    13. Private Declare Function RegisterActiveObject Lib "oleaut32.dll" (ByVal pUnk As IUnknown, rclsid As GUIDs, ByVal dwFlags As Long, pdwRegister As Long) As Long
    14. Private Declare Function RevokeActiveObject Lib "oleaut32.dll" (ByVal dwRegister As Long, ByVal pvReserved As Long) As Long
    15. Dim OLEInstance As Long
    16.  
    17.  
    18. Private Sub Class_Initialize()
    19. 'This code is responsible for creating the entry in the rot
    20.  
    21. Dim mGUID As GUIDs
    22. Dim lp As Long
    23. OLEInstance = 0
    24. lp = CLSIDFromProgID(StrPtr("UserModule.clsUserMod"), mGUID)
    25. If lp = 0 Then
    26.     lp = RegisterActiveObject(Me, mGUID, ACTIVEOBJECT_WEAK, OLEInstance)
    27. End If
    28.  
    29. End Sub
    30.  
    31. Private Sub Class_Terminate()
    32. 'Once we are done with the main program, lets clean up the rot
    33. 'by removing the entry for our ActiveX Server
    34.  
    35. If OLEInstance <> 0 Then
    36.     RevokeActiveObject OLEInstance, 0
    37. End If
    38.  
    39. CoDisconnectObject Me, 0
    40.  
    41. End Sub
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    I think this code may suffer from the dreaded "circular reference" paradox.

    IE.. the code to remove yourself from the ROT is in the Class_Terminate, which cannot be called because the Object is referenced by the ROT. As long as any object holds a reference to you, you will not be destroyed and won't get a chance to run your Termination code.

    I will definately try it tonight to see how it works.

  9. #9
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    To get round this use the pointer to an object and then use:
    VB Code:
    1. Private Function PtrObj(ByVal Pointer As Long) As Object
    2. Dim objObject   As Object
    3.     CopyMemory objObject, Pointer, 4&
    4.     Set PtrObj = objObject
    5.     CopyMemory objObject, 0&, 4&
    6. End Function
    To retrieve the object.
    This removes any circular reference.

    Woka

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Always worked fine for me. When I set it to nothing, the terminate is still going to attempt to fire. Then the code in there handles removing it from the ROT
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Originally posted by Wokawidget
    To get round this use the pointer to an object and then use:
    VB Code:
    1. Private Function PtrObj(ByVal Pointer As Long) As Object
    2. Dim objObject   As Object
    3.     CopyMemory objObject, Pointer, 4&
    4.     Set PtrObj = objObject
    5.     CopyMemory objObject, 0&, 4&
    6. End Function
    To retrieve the object.
    This removes any circular reference.
    Woka
    How do you get the Pointer to the Object? With AddressOf? Could you show an example of this function in use from code? Just a single line should do it.

  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    4.  
    5. Private mlngPtr As Long
    6.  
    7. Private Sub Form_Load()
    8.     mlngPtr = ObjPtr(Combo1)
    9.     AddItem
    10. End Sub
    11.  
    12. Private Sub AddItem()
    13. Dim objCombo    As ComboBox
    14.     Set objCombo = PtrObj(mlngPtr)
    15.     objCombo.AddItem "Woof"
    16.     Set objCombo = Nothing
    17. End Sub
    18.  
    19. Private Function PtrObj(ByVal Pointer As Long) As Object
    20. Dim objObject   As Object
    21.     CopyMemory objObject, Pointer, 4&
    22.     Set PtrObj = objObject
    23.     CopyMemory objObject, 0&, 4&
    24. End Function
    Works for objects and controls.

    This means that when you have 2 classes that need to reference each other, ie Parent child collections, you only need to store ref to one object, and the other object stores a pointer back to the parent.
    This way there is only ever 1 instance of an object, although 100's of other classes may reference it but by using it's Ptr

    Woka

  13. #13

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Originally posted by Cander
    Here is the ROT code I use, and dont recall any problems with it..
    I really like this code. Well done. I will poke around with it more tonight, but so far I don't seem to have any problems with it, as you stated.

    I hope I can get this to work as a DCOM service - something that has continued to escape me.

  14. #14

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Here is a complete project example based on Cander's code:

    Compile each executable, then you can run a server instance, and then start running alot of client instances. They will all do the same thing.

    I know its not very interesting, but things will get more interesting if I ever get this to work on remote machines using DCOM...
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width