Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hwnd to object?

Posted on 2001-08-17
32
Medium Priority
?
2,117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 28

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 28

Accepted Solution

by:
Ark earned 500 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
 
LVL 28

Expert Comment

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

Cheers
0
 
LVL 28

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 28

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 28

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 28

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

705 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