Solved

Hwnd to object?

Posted on 2001-08-17
32
2,016 Views
Last Modified: 2013-11-25
Hi experts:

What i would like to know if it is possible, having a window handle, get the object associated with it.

Assumptions:
1). Handle is a valid handle for an app that supports Automation/COM (aka Excel-word...) is running and could be more than one instance at a time.

I would like to change properties or even better, trap events from it.
GetObject function is not an option, nor ROT.
Full working code will be nice.
Clever guesses are welcome too.
Cheers

Richie
0
Comment
Question by:Richie_Simonetti
  • 16
  • 7
  • 3
  • +5
32 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6399070
In the "guesses" department (I am not sure about the clever).  How about comparing to all the active hwnd.

Anthony
0
 
LVL 5

Expert Comment

by:dredge
ID: 6399113
i don't think it's possible to make Automation calls by getting the applicatin's HWND.

you can, however send Windows Messages to the HWND and "script" the running program - example: after getting the HWND of Notepad.exe, you send a windows message with the text "hello." - and "hello." is typed into the currently open file.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6399187
Look at the "Resolving Pointers Demo" sample here:
http://www.mvps.org/vbvision/Sample_Projects.htm
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6399250
Azra, how do you go from hWnd to object pointer?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6399265
Oops, not thinking today...
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6399344
Hmmm... good question there Richie...  Will see If I can contribute.

Dave
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6399418
"I would like to change properties"

A basic tenet of Windows (particularily NT).. one does not violate the memory space of another process.. <sigh>.

"trap events from it"

However.. one can rather creatively subclass the user control interface.. <smile>
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6399728
Dave:
I will be waiting...

dredge:
Notepad not supports automation neither is a COM aware app.

Azra:
I will check this out. Thanks.

acperkins:
I didn't catch your guess.

wsh2:
"....A basic tenet of Windows (particularily NT).. ..."
I don't understand.
Expand regarding subclass, what do you mean...

Thanks all, folks.
:)
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6399738
To be more specific:
Scenario....

Two excel app running....
Getting the hwnd of both...
"converting" those hwnd to object reference... and work with just created objects like if we have been declaring variables as excel.application
It's more clear, i think...
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6400454
"I don't understand. Expand regarding subclass, what do you mean"

Ok.. 2 Excel spreadsheets running.. each is in their OWN process space.. the only way you can enter these processes is through some kind of Interface.. (ie. a GetObject(,"Excel.Application") command.

The problem in doing this.. is that there is no way to tell GetObject WHICH Excel spreadsheet you want to join. Its first come first serve.. so that if you issue a second GetObject.. you will MOST likely be thrown into the session you just joined previous.

The comment about violating process space.. was directed to the thought, that if you know the hWnd.. then very possibly.. you could get its Process / Thread ID.. and then from that its memory space.. and knowing that.. then try to manipulate the object's memory space directly (ie. change object property values). Windows absolutely does NOT like this (one process intruding into another processes web space) making this a VERY difficult (if not impossible) implementation to make.

Subclassing is a methodology where you intercept / issue commands from / into the applications USER graphical interface. In effect you are automating such thingz as keystrokes.. mouse clicks.. menu clicks.. opening / closing application windows (and/or reading / writing the contents thereof), all from within the confines of your program.
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6402110
Wsh, i know regarding subclass, i thought that you were talking something else...

"The problem in doing this.. is that there is no way to tell GetObject WHICH Excel spreadsheet you want
to join. Its first come first serve.. "
That's the problem in first place!!!
Due to that i, said that GetObject is not an option (seee the question body)
Thanks.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6402778
Richie..
Yes, I know what you are saying.. <smile>. What I'm saying is that no, what you want to do (IMHO) just can't be done.. <sigh>.

0
 
LVL 27

Expert Comment

by:Ark
ID: 6402955
>>Some applications such as Excel do register themselves in the ROT and support multiple instances. However, there is no way to connect to a particular instance of Excel with COM. For example, the GetObject() method in Visual Basic returns a random instance, even when a file name is specified.
With DDE you can manipulate any worksheet without regard to the instance hosting it, because Excel offers a separate topic name for each worksheet. For more information on controlling Excel go to http://www.angelfire.com/biz/rhaminisys/ddeapps.html#DDEExcelSpec<<
>>Sample Visual Basic program to control Excel http://www.angelfire.com/biz/rhaminisys/binaries/xlddevb.zip. Requires DDClient http://www.angelfire.com/biz/rhaminisys/binaries/ddclv305.zip<<

Cheers
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6402967
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6402968
Ups!, sorry... Thanks wsh2
;)
0
 
LVL 27

Accepted Solution

by:
Ark earned 125 total points
ID: 6402975
Or, another way:
Parse GetWindowText(hwnd) to find book name (last part after "-"
Smth like this:
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Function GetWndText(hwnd As Long) As String
  Dim k As Long, sName As String
  sName = Space$(128)
  k = GetWindowText(hwnd, sName, 128)
  If k > 0 Then sName = Left$(sName, k) Else sName = "No caption"
  GetWndText = sName
End Function

Dim sCaption As String
Dim sBookName As String
sCaption = GetWndText(hwnd)
sBookName = Mid(sCaption, InStr(1, sCaption, "-") + 1)
xlApp = GetObject(sBookName).Application

Cheers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:Ark
ID: 6402976
Sorry, of course
Set xlApp = GetObject(sBookName).Application

Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 6402981
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6402992
Ark, thanks for your support.
There is a problem with parse function since could be more than one workbook open and i need to manage all of them...;)
0
 
LVL 27

Expert Comment

by:Ark
ID: 6403012
Richie, you'll get Application object, which hold WorkBooks collection, and you can manage all books in this collection

Cheers
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6404658
Well, to much info to analize. Thanks.
i'll be back.
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6424224
I hope i would check this at weekend.
Cheers
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6453730
Ark, regarding this comment:
"....Or, another way:
Parse GetWindowText(hwnd) to find book name (last part after "-"
Smth like this:
......"

There are some problems with it:
1) Workbooks must be maximized to show its name in caption.
2) If there is a default book open (Book1.xls i think, i have excel in spanish), same problem or worst.
3) If there is not a workbook open, i mean, excel is running but without any workbook .....

Cheers
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6453731
...and i got Automation error in this line:
Set xlApp(idx) = GetObject(sBookName).Application

I think it is due to we need the full path of workbook to it works right.
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6472477
Hi, Ark
Did you have time to workaround that?
Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 6473287
Hello Richie
Sorry, can not check anything now - Excell not installed on my machine. I tried this trick with Word and it worked. Did you try DDE?

Cheers
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6474269
Word is fine. Thanks ;)
I think DDE is not i am looking for.
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6513962
I can't do it works.
To use getobject in that way, we need complete path of file.
Besides, it could happen that no one workbook is open or worst, defaul one only.
Any other clue, please.
Chers
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6641502
Well, too much time for an opened question, may i delete it?
0
 
LVL 27

Expert Comment

by:Ark
ID: 6643261
Hi
If no one answered, you can delete it. But did you get answer? May be post 0 points question with a link to this thread?.

Cheers
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6644466
Not a "working" answer. At least, your code is close but there are more facts that we must take care and, to this moment, not close enought.
I did a little trick that uses other code of you (enumerating recentfiles key at registry). It couldn't solve part of the problem, but for a reason that i don't know, i can't manage the issue.
I mean, if you create a workbook object for every entry in recentfiles that matches running opened workbook, it could be solve at least, partially.
but syntax:

dim xlwb as workbook
set xlwb =getobject("c:\myregentry\file.xls",)
doesn't appears to work.
Any clue?
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 6674067
Thanks anyway.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 65
JSON Response and request in VB6 application 11 252
VBA filters 2 51
VB6 - Compare and highlight cell not the same 3 44
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now