Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2147
  • Last Modified:

Hwnd to object?

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
Richie_Simonetti
Asked:
Richie_Simonetti
  • 16
  • 7
  • 3
  • +5
1 Solution
 
Anthony PerkinsCommented:
In the "guesses" department (I am not sure about the clever).  How about comparing to all the active hwnd.

Anthony
0
 
dredgeCommented:
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
 
AzraSoundCommented:
Look at the "Resolving Pointers Demo" sample here:
http://www.mvps.org/vbvision/Sample_Projects.htm
0
Technology Partners: 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!

 
PaulHewsCommented:
Azra, how do you go from hWnd to object pointer?
0
 
AzraSoundCommented:
Oops, not thinking today...
0
 
Dave_GreeneCommented:
Hmmm... good question there Richie...  Will see If I can contribute.

Dave
0
 
wsh2Commented:
"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
 
Richie_SimonettiAuthor Commented:
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
 
Richie_SimonettiAuthor Commented:
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
 
wsh2Commented:
"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
 
Richie_SimonettiAuthor Commented:
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
 
wsh2Commented:
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
 
ArkCommented:
>>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
 
Richie_SimonettiAuthor Commented:
0
 
Richie_SimonettiAuthor Commented:
Ups!, sorry... Thanks wsh2
;)
0
 
ArkCommented:
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
 
ArkCommented:
Sorry, of course
Set xlApp = GetObject(sBookName).Application

Cheers
0
 
ArkCommented:
0
 
Richie_SimonettiAuthor Commented:
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
 
ArkCommented:
Richie, you'll get Application object, which hold WorkBooks collection, and you can manage all books in this collection

Cheers
0
 
Richie_SimonettiAuthor Commented:
Well, to much info to analize. Thanks.
i'll be back.
0
 
Richie_SimonettiAuthor Commented:
I hope i would check this at weekend.
Cheers
0
 
Richie_SimonettiAuthor Commented:
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
 
Richie_SimonettiAuthor Commented:
...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
 
Richie_SimonettiAuthor Commented:
Hi, Ark
Did you have time to workaround that?
Cheers
0
 
ArkCommented:
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
 
Richie_SimonettiAuthor Commented:
Word is fine. Thanks ;)
I think DDE is not i am looking for.
0
 
Richie_SimonettiAuthor Commented:
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
 
Richie_SimonettiAuthor Commented:
Well, too much time for an opened question, may i delete it?
0
 
ArkCommented:
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
 
Richie_SimonettiAuthor Commented:
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
 
Richie_SimonettiAuthor Commented:
Thanks anyway.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 16
  • 7
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now