Results 1 to 16 of 16

Thread: [RESOLVED] objptr alternative

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    6

    Resolved [RESOLVED] objptr alternative

    Hi,
    I have two questions:
    1- is there another way to get object pointer rather than objptr?
    2- is there any way to get object pointer from another application window?

  2. #2
    Addicted Member Wolfgang Enzinger's Avatar
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    137

    Re: objptr alternative

    Quote Originally Posted by Amervb View Post
    1- is there another way to get object pointer rather than objptr?
    Well, VarPrt() of any object variable points to the ObjPtr, so you would only have to dereference VarPrt(). I wonder, however, why you would want to do that?

    Quote Originally Posted by Amervb View Post
    2- is there any way to get object pointer from another application window?
    Probably there is, but you cannot do anything useful with it since it's originating from a, say, "foreign" address space.

    Wolfgang

  3. #3

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,199

    Re: objptr alternative

    Quote Originally Posted by Wolfgang Enzinger View Post
    Well, VarPrt() of any object variable points to the ObjPtr, so you would only have to dereference VarPrt(). I wonder, however, why you would want to do that?
    ?
    Code:
    Sub main()
    Dim AClass As MyClass
    
        Set AClass = New MyClass
        
        Debug.Print ObjPtr(AClass)
        Debug.Print VarPtr(AClass)
    
    End Sub
    'Returns
    '413199992 
    '2290512
    EDIT: Just saw, that you wrote "dereference VarPtr", so that's actually quite correct what you wrote.
    Last edited by Zvoni; May 3rd, 2019 at 03:37 AM.
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  5. #5
    Addicted Member Wolfgang Enzinger's Avatar
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    137

    Re: objptr alternative

    Quote Originally Posted by Zvoni View Post
    ?
    Code:
    Sub main()
    Dim AClass As MyClass
    
        Set AClass = New MyClass
        
        Debug.Print ObjPtr(AClass)
        Debug.Print VarPtr(AClass)
    
    End Sub
    'Returns
    '413199992 
    '2290512
    !

    Code:
    Declare Sub GetMem4 Lib "msvbvm60" (ByVal Addr As Long, RetVal As Long)
    
    Sub Main()
    Dim c As VBA.Collection
    Dim i As Long
    Set c = New VBA.Collection
    GetMem4 VarPtr(c), i
    Debug.Assert i = ObjPtr(c)
    End Sub
    Wolfgang

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    6

    Re: objptr alternative

    Quote Originally Posted by Wolfgang Enzinger View Post
    Well, VarPrt() of any object variable points to the ObjPtr, so you would only have to dereference VarPrt(). I wonder, however, why you would want to do that?
    Wolfgang , Thank you for reply, yes CopyMemory returns the object pointer from variable pointer.
    I'm just making an experiment with ReadProcessMemory.

    Quote Originally Posted by Wolfgang Enzinger View Post
    Probably there is, but you cannot do anything useful with it since it's originating from a, say, "foreign" address space.
    Is it possible to get object pointer or variable pointer from another process?

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    6

    Re: objptr alternative

    Quote Originally Posted by The trick View Post
    Why do you need the pointer? To identify an object in COM you should use monikers. As the alternative of pointers you can use OBJREF moniker.
    The Trick, Thank you for reply, I'm just making an experiment with ReadProcessMemory.
    But i'm not sure about OBJREF moniker.
    Is it possible to get object pointer or variable pointer from another process same as objptr(Form1)?
    Last edited by Amervb; May 3rd, 2019 at 05:37 AM. Reason: Correc a word

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    6

    Re: objptr alternative

    Duplicated reply edited because there is no delete option.
    Last edited by Amervb; May 3rd, 2019 at 05:31 AM. Reason: Duplicate

  9. #9
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,589

    Re: objptr alternative

    The Trick, Thank you for reply, I'm just making an experiment with ReadProcessMemory.
    But i'm not sure about OBJREF moniker.
    Is it possible to get object pointer or variable pointer from another process same as objptr(Form1)?
    I asked you why do you need the pointer? What do you want to do with that pointer? If you want to work with it to call the methods you should use marshaling. OBJREF moniker ensures that.

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    6

    Re: objptr alternative

    Quote Originally Posted by The trick View Post
    If you want to work with it to call the methods you should use marshaling. OBJREF moniker ensures that.
    Yes its OBJREF, Many thanks.

  11. #11
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,589

    Re: objptr alternative

    Quote Originally Posted by Amervb View Post
    Yes its OBJREF, Many thanks.
    Moreover, it even supports the calls between machines apart from processes. You can very easy connect two applications or VBScript or 64 bit applications to VB6 using the monikers.
    This is the small example (on Russian) where i created a VBScript object in the one machine and call the method of that object from the second machine using VB6.
    You can, for example, also create a object in VBA (even x64) and communicate with VB6 app using moniker.
    The same behavior with x64 C++ application and VB6 application, or between two or more VB6 applications.

  12. #12
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,040

    Re: [RESOLVED] objptr alternative

    This is the meat of the snippet from the link above

    Code:
    Public Function RegisterObject( _
                    ByVal cObj As Object) As String
        Dim cMoniker    As IMoniker
        Dim cBind       As IBindCtx
        Dim pStr        As Long
       
        Set cBind = CreateBindCtx()
        Set cMoniker = CreateObjrefMoniker(cObj)
       
        cMoniker.GetDisplayName ObjPtr(cBind), 0, pStr
       
        RegisterObject = SysAllocString(pStr)
       
        CoGetMalloc(1).Free pStr
      
    End Function
    Now if someone can port cMoniker.GetDisplayName to DispCallFunc and provide the declares for CreateBindCtx, CreateObjrefMoniker and CoTaskMemFree. Unfortunately SysAllocString cannot be declared correctly (outside of typelib) and has to be mimiced w/ lstrlenW and CopyMemory.

    Once you have this display string returned, pass it cross thread/process (e.g. using WM_COPYMEMORY/WM_SETTEXT) and just use VB's built-in GetObject to "hydrate" a proxy.

    cheers,
    </wqw>

  13. #13

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,923

    Re: [RESOLVED] objptr alternative

    Well, 15 years ago or so, I've posted the approach below into some NewsGroup -
    which is using the ROT (in conjunction with a FileMoniker) - to allow CrossProcess-access to any (Singleton-) Object.

    I've not bothered with API-Declares here - instead the Example needs a reference to Eduardo Morcillos OleLib.tlb:
    Code:
    Option Explicit
     
    Private RegID As Long
    
    Private Sub Form_Load()
      Dim Col As New Collection: Col.Add "Hello World", "Key" 'prepare an Obj with something in it
      
      With GetRunningObjectTable 'now we register the Col in the ROT globally under a FileMoniker
        RegID = .Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, Col, CreateFileMoniker("MyMoniker"))
      End With
    End Sub
    
    Private Sub Form_Click() 'request the now global Collection over its Moniker
      Debug.Print GetObject("MyMoniker").Item("Key")
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer) 'cleanup the registered Obj over its associated ID
      If RegID Then GetRunningObjectTable.Revoke RegID
    End Sub
    Forgot to mention...
    To test this also cross-process, it is enough to copy the Form_Click-Handler -
    into another (fresh started) IDE-instance - whilst the Main-instance is running (with the code above)...

    HTH

    Olaf
    Last edited by Schmidt; May 3rd, 2019 at 10:59 AM.

  15. #15
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,040

    Re: [RESOLVED] objptr alternative

    Here is an implementation of PutObject (the opposite of the built-in GetObject) w/ no external typelibs.

    thinBasic Code:
    1. Option Explicit
    2.  
    3. Private Declare Function CreateFileMoniker Lib "ole32" (ByVal lpszPathName As Long, pResult As IUnknown) As Long
    4. Private Declare Function GetRunningObjectTable Lib "ole32" (ByVal dwReserved As Long, pResult As IUnknown) As Long
    5. Private Declare Function DispCallFunc Lib "oleaut32" (ByVal pvInstance As Long, ByVal oVft As Long, ByVal lCc As Long, ByVal vtReturn As VbVarType, ByVal cActuals As Long, prgVt As Any, prgpVarg As Any, pvargResult As Variant) As Long
    6.  
    7. Private Sub Form_Load()
    8.     Dim lCookie     As Long
    9.     Dim oCol        As Collection
    10.     Dim lColCookie  As Long
    11.    
    12.     lCookie = PutObject(Me, "MyMoniker")
    13.     Debug.Print TypeName(GetObject("MyMoniker"))    '--- returns Form1
    14.     '--- warning: "MyMoniker" path is "stacked"
    15.     Set oCol = New Collection
    16.     lColCookie = PutObject(oCol, "MyMoniker")
    17.     Debug.Print TypeName(GetObject("MyMoniker"))    '--- still Form1
    18.     '--- "pops" Form1
    19.     RevokeObject lCookie
    20.     Debug.Print TypeName(GetObject("MyMoniker"))    '--- returns Collection now
    21.     RevokeObject lColCookie
    22. End Sub
    23.  
    24. Private Function PutObject(oObj As Object, sPathName As String) As Long
    25.     Const ROTFLAGS_REGISTRATIONKEEPSALIVE As Long = 1
    26.     Const IDX_REGISTER  As Long = 3
    27.     Dim pROT            As IUnknown
    28.     Dim pMoniker        As IUnknown
    29.    
    30.     Call GetRunningObjectTable(0, pROT)
    31.     Call CreateFileMoniker(StrPtr(sPathName), pMoniker)
    32.     DispCallByVtbl pROT, IDX_REGISTER, ROTFLAGS_REGISTRATIONKEEPSALIVE, ObjPtr(oObj), ObjPtr(pMoniker), VarPtr(PutObject)
    33. End Function
    34.  
    35. Private Sub RevokeObject(ByVal lCookie As Long)
    36.     Const IDX_REVOKE    As Long = 4
    37.     Dim pROT            As IUnknown
    38.    
    39.     Call GetRunningObjectTable(0, pROT)
    40.     DispCallByVtbl pROT, IDX_REVOKE, lCookie
    41. End Sub
    42.  
    43. Private Function DispCallByVtbl(pUnk As IUnknown, ByVal lIndex As Long, ParamArray A() As Variant) As Variant
    44.     Const CC_STDCALL    As Long = 4
    45.     Dim lIdx            As Long
    46.     Dim vParam()        As Variant
    47.     Dim vType(0 To 63)  As Integer
    48.     Dim vPtr(0 To 63)   As Long
    49.     Dim hResult         As Long
    50.    
    51.     vParam = A
    52.     For lIdx = 0 To UBound(vParam)
    53.         vType(lIdx) = VarType(vParam(lIdx))
    54.         vPtr(lIdx) = VarPtr(vParam(lIdx))
    55.     Next
    56.     hResult = DispCallFunc(ObjPtr(pUnk), lIndex * 4, CC_STDCALL, vbLong, lIdx, vType(0), vPtr(0), DispCallByVtbl)
    57.     If hResult < 0 Then
    58.         Err.Raise hResult
    59.     End If
    60. End Function
    You get RevokeObject too to test ROT registration at will.

    Also note that you would probably need to use "MyMoniker" & App.hInstance just to be safe with these moniker paths cross-process "interferences".

    cheers,
    </wqw>

  16. #16

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    6

    Re: [RESOLVED] objptr alternative

    Quote Originally Posted by wqweto View Post
    Here is an implementation of PutObject (the opposite of the built-in GetObject) w/ no external typelibs.
    Thank you wqweto, i appreciate it.

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