Solved

Hwnd to object?

Posted on 2001-08-17
32
2,005 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
Comment Utility
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
Comment Utility
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
Comment Utility
Look at the "Resolving Pointers Demo" sample here:
http://www.mvps.org/vbvision/Sample_Projects.htm
0
 
LVL 38

Expert Comment

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

Expert Comment

by:AzraSound
Comment Utility
Oops, not thinking today...
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
Hmmm... good question there Richie...  Will see If I can contribute.

Dave
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
0
 
LVL 16

Author Comment

by:Richie_Simonetti
Comment Utility
Ups!, sorry... Thanks wsh2
;)
0
 
LVL 27

Accepted Solution

by:
Ark earned 125 total points
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

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

Cheers
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
0
 
LVL 16

Author Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
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
Comment Utility
Well, to much info to analize. Thanks.
i'll be back.
0
 
LVL 16

Author Comment

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

Author Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
...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
Comment Utility
Hi, Ark
Did you have time to workaround that?
Cheers
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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
Comment Utility
Word is fine. Thanks ;)
I think DDE is not i am looking for.
0
 
LVL 16

Author Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
Well, too much time for an opened question, may i delete it?
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks anyway.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

11 Experts available now in Live!

Get 1:1 Help Now