Results 1 to 5 of 5

Thread: Form object reference from window handle

  1. #1

    Thread Starter
    Addicted Member darrenl's Avatar
    Join Date
    Jul 2000
    Location
    Portsmouth, UK
    Posts
    148

    Form object reference from window handle

    I have a handle to a window in a seperate process, I know the window is a VB form. Does anybody know a way to turn that handle into a Form object so that I can invoke methods from my own process?
    Dazzer

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Finding the handle of a running window is easy. What, specifically, do you want to do with it after you have it?

  3. #3
    jim mcnamara
    Guest
    COM objects (like forms) expose an interface to the client. - It is mapped into the client process memory space. And it is just the interface (vtable). In theory, I suppose, you could use QueryVirtualEx or some other api function to read memory from the other process until you found the interface (vtable), then try to make calls to it.

    But you wouldn't be able to make any calls to it, unless you could find a way to modify the user stack for the other process.

    That's abstract theory. Practice is: no way in hell. Code like Form1.Left=100 in VB just isn't going to work from an external process.

    Just use the hWnd to SendMessage to the form. Sorry.

    Short answer: no way

  4. #4
    Junior Member
    Join Date
    Nov 2001
    Location
    Netherlands
    Posts
    22
    Well,

    my subclassing control uses some techniques to get a object reference from a handle.

    - more threory -
    As the Win32 API refers to a window with a handle, COM (or VB) uses object references to refer to a window. That object knows the handle, and all functions/properties in the object are internally using Win32 API's to modify the window.


    - Now the trick -
    Using the ObjPtr() function, you can get the memory location of a object. (a long number). Use the SetProp() API to store that number somewhere at the window, like SetProp(Form1.hWnd, "my_reference", ObjPtr(Form1))

    Somewhere else in your code, you can use GetProp to get the property. Converting that back into a real reference is tricky.
    If the object doesn't exist at that location, the code properly fails!

    VB Code:
    1. Public Function GetObjectByPtr(ByVal ObjectPointer As Long) As Object
    2.   Dim TheObj As Object
    3.   Dim Holder As Object
    4.  
    5.   ' Maybe I confused ByVal and ByRef somewhere,
    6.   ' check it, and please be careful with using this.
    7.  
    8.   Call CopyMemory(ByVal ObjectPointer, Holder, 4) ' Illegal Reference!!
    9.   Set TheObj = Holder ' Make the reference legal (calls the addref method for the COM object)
    10.   Call CopyMemory(Holder, ByVal 0, 4) ' Destroy illegal reference
    11.   Set GetObjectByPtr = TheObj
    12. End Function
    Yet Another Perl Programmer



    Working with the Windows 32 API
    Test my carrier game
    X-Programming Forums (members and testers wanted!)

  5. #5

    Thread Starter
    Addicted Member darrenl's Avatar
    Join Date
    Jul 2000
    Location
    Portsmouth, UK
    Posts
    148
    Thanks guys,

    Yapp. Your code is actually the techique I was using but had all sorts of problems with, Jim's comments expained why.

    Back to the drawing board.
    Dazzer

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