Results 1 to 9 of 9

Thread: Check if Pointer is valid ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Check if Pointer is valid ?

    Hi,

    Is there a way of figuring out if an object pointer is valid ? (ie:- points to a valid instanciated object)

    Thanks.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Check if Pointer is valid ?

    Once you start fiddling with interface pointers you have broken the contract and all bets are off. You must take on full responsibility at that point.

    IUnknown.Release() returns the remaining object reference count, but there is no requirement that it be accurate, only 0 or not 0. Once Release() is down to 0 references, the method releases the object.

    Without more context we can't answer this vague question.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Check if Pointer is valid ?

    Ok- let me explain what I am trying to do.

    Basically, I have a class and I have some code that is meant to be ran upon initializing the class (ie:- inside the Class_Initialize event).

    The issue is, I want the initialize code to be ran ONLY for the first class instance... Subsequent class instances should skip the initialize code.

    This is what I am doing ... I just need the necessary code for the IsValidPointer function.

    1- Code of the class client:
    Code:
    Option Explicit
    
    Dim Obj1 As Class1, Obj2 As Class1
    
    Sub Test()
    
        Set Obj1 = New Class1
        SetProp hwnd, "POINTER", ObjPtr(Obj1)
        
        Set Obj2 = New Class1
    
    End Sub

    2- Class code :
    Code:
    Option Explicit
    
    Private Sub Class_Initialize()
        If IsValidPointer(GetProp(hwnd, "POINTER")) = False Then
            '\\ run some code only for first class instance
        End If
    End Sub
    
    
    Private Function IsValidPointer(ByVal Ptr As Long) As Boolean
    
    End Function

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Check if Pointer is valid ?

    GetProp should return 0 on first instance call so IsValidPointer probably can be reduced to IsValidPointer = (Ptr <> 0)

    cheers,
    </wqw>

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Check if Pointer is valid ?

    I have no idea what you are really trying to accomplish, just the wacky way you want to go about it.

    If you GetProp() a window handle and it has never been defined a NULL (0) is returned. So all you really need to do is call SetProp() the first time passing something non-NULL (for example 1). And don't forget the requirement to RemoveProp() as well.

    But the entire enterprise looks dicey to me. You seem to be trying to reinvent the concept of a class factory by severely breaking encapsulation using a trick meant for entirely different purposes.


    This is clearly a job for a DLL. There you would have a creatable public class that is the class factory. When you ask it to create an instance of another non-creatable public class for you it can do any yet-uncompleted initialization there, probably ending up assigning values to data in a static module visible to the non-creatable class.

    If you are playing reindeer games trying to write monolithic programs, then do the same thing. You must hold your mouth right, cross your fingers, and hope for the best by never referencing that static module data elsewhere and never creating instances of the second class explicitly in the client code. This is brittle though... because "playahs gonna play."

    But screwing around with window property lists is a newb move. Exactly the sort of thing that gets people fired.


    Every time I see a stunt like this I begin to understand why Microsoft killed VB and pushed .Net instead.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Check if Pointer is valid ?

    Quote Originally Posted by wqweto View Post
    GetProp should return 0 on first instance call so IsValidPointer probably can be reduced to IsValidPointer = (Ptr <> 0)

    cheers,
    </wqw>
    Thanks... I thought about that but I wanted to make this foolprof so that if the vba project was reset by accident and there was a state loss and therefore the class Terminate-event wouldn't cleanup & reset the hwnd Prop "ie:= SetProp hwnd,"POINTER",0" or RemoveProp.

    I could reset the hwnd Prop in the class client code at the very start of the Test routine but, that was just an simplified example. The code I have is more involved so I would prefer to cleanup the hwnd Prop in the class Terminate event.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Check if Pointer is valid ?

    Quote Originally Posted by dilettante View Post
    If you GetProp() a window handle and it has never been defined a NULL (0) is returned. So all you really need to do is call SetProp() the first time passing something non-NULL (for example 1). And don't forget the requirement to RemoveProp() as well.
    I will probably do that (Also, same suggestion offered by wqweto)
    Thanks.

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Check if Pointer is valid ?

    There is also

    Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long


    API function which is totally *not* what you want but if this returns non-zero for sure the lp pointer is not valid.

    It might return false positives though -- an object is deallocated but the heap (virtual memory) it was created in is still allocated in the current processs (and good to read).

    Besides, there is no reliable way to figure out instance size for the ucb parameter. Probably something like 4 (or 8) is theoretical lower bound though.

    cheers,
    </wqw>

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Check if Pointer is valid ?

    You're making this way harder than it needs to be. You can easily implement the behavior you want by utilizing a global variable accessible by all instances of the class. It could be a simple Boolean. Every time a new object is created, have it check this Boolean, if that Boolean value is False, run the initialization code and set it to True. Every instance after that will see the Boolean being True and not run the initialization code.

    That fact that you didn't consider this solution tells me there is something else going on here. My answer above is based completely and exactly on what you said here:-
    Quote Originally Posted by JAAFAR View Post
    Ok- let me explain what I am trying to do.

    Basically, I have a class and I have some code that is meant to be ran upon initializing the class (ie:- inside the Class_Initialize event).

    The issue is, I want the initialize code to be ran ONLY for the first class instance... Subsequent class instances should skip the initialize code.
    I sort of skimmed through the thread to get to the meat of the issue so if there is something else I missed, my apologies if this is not what you want.
    Last edited by Niya; Jul 24th, 2021 at 09:30 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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