msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Finding open windows

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB WinAPI
Author Message
Juergen Grieb



Joined: 12 Feb 2008
Posts: 3

PostPosted: Tue Feb 12, 2008 11:00 am    Post subject: Finding open windows Reply with quote

Hi,

my Access app starts an instance of Word in an Acc97 form. So far I
have
complete control over Word, except in the circumstance that Word has
other "windows" (including dialogs like the printing dialog) open.

Searching the web I found no reliable way to get Word's status in case
another Word window is open. So I thought, that finding out if Word
has other windows open would be enough. Since I cannot get that
information from Word (it doesn't respond if a dialog is open) I
thought that API would do the trick.

Unfortunatly, I have no experience at all with API. FindWindowEx and
EnumChildWindows seems like what I need, but I wasn't able to figure
out how to use them to get the wanted information.

Can anybody please tell me how to get all open "windows" of an
existing Word instance?

Thanks a lot!

Juergen

Archived from group: microsoft>public>vb>winapi
Back to top
View user's profile Send private message
Juergen Thuemmler



Joined: 04 Oct 2007
Posts: 225

PostPosted: Tue Feb 12, 2008 10:23 pm    Post subject: Re: Finding open windows Reply with quote

> Can anybody please tell me how to get all open "windows" of an
> existing Word instance?

When you have at least one window handle of this Word instance, you can use
"GetWindowThreadProcessId()" to get the ThreadID of Word and then
"EnumThreadWindows()" to get all other windows listed in the corresponding
callback function "EnumThreadWndProc()".

Juergen.
Back to top
View user's profile Send private message
Juergen Grieb



Joined: 12 Feb 2008
Posts: 3

PostPosted: Wed Feb 13, 2008 3:54 am    Post subject: Re: Finding open windows Reply with quote

> When you have at least one window handle of this Word instance, you can use
> "GetWindowThreadProcessId()" to get the ThreadID of Word and then
> "EnumThreadWindows()" to get all other windows listed in the corresponding
> callback function "EnumThreadWndProc()".

I have a window handle and was able to obtain the PID. Unfortunately,
I'm not able to get EnumThreadWindows to work with VBA:

Private Declare Function GetWindowThreadProcessId Lib
"user32.dll" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function EnumThreadWindows Lib "user32" (ByVal
dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Collection) As
Long

Private Function EnumThreadWndProc(ByVal hWnd As Long, ByVal Windows
As Collection) As Long
Windows.Add hWnd
EnumThreadWndProc = 1
End Function

Private Sub Irgendwas()
Dim ret As Long
Dim wrdPID As Long
Dim Windows As Collection

Set Windows = New Collection
ret = GetWindowThreadProcessId(wrdHndl, wrdPID)
ret = EnumThreadWindows(wrdPID, AddrOf("EnumThreadWndProc"),
Windows)
End Sub

I get no error, but my collection is empty all the time. Any
suggestions?

Juergen
Back to top
View user's profile Send private message
Juergen Thuemmler



Joined: 04 Oct 2007
Posts: 225

PostPosted: Wed Feb 13, 2008 2:37 pm    Post subject: Re: Finding open windows Reply with quote

> I have a window handle and was able to obtain the PID. Unfortunately,
> I'm not able to get EnumThreadWindows to work with VBA:

Put the following code into a new VB project module, enter a valid window
handle in Sub Main and run the project.

Juergen.

Option Explicit

Declare Function GetWindowThreadProcessId& Lib "user32" (ByVal Hwnd&,
lpdwProcessId&)
Declare Function EnumThreadWindows& Lib "user32" (ByVal dwThreadId&, ByVal
lpfn&, ByVal lParam&)
Declare Function GetClassName& Lib "user32" Alias "GetClassNameA" (ByVal
Hwnd&, ByVal lpClassName$, ByVal nMaxCount&)

Sub Main()
Dim Hwnd&, TID&, PID&
Hwnd = 394550 '<<<<<< Must be a valid Handle #########
TID = GetWindowThreadProcessId(Hwnd, PID)
Call EnumThreadWindows(TID, AddressOf ThreadWndProc, 0)
End Sub

Public Function ThreadWndProc&(ByVal Hwnd&, ByVal dl&)
Debug.Print Hwnd, WindowClass(Hwnd)
ThreadWndProc = 1
End Function

Function WindowClass$(ByVal Hwnd&)
Dim tbuf$, dl&

tbuf = String$(256, 0)
dl = GetClassName(Hwnd, tbuf, 255)
If dl > 0 Then WindowClass = LCase$(Left$(tbuf, dl))
End Function
Back to top
View user's profile Send private message
Juergen Grieb



Joined: 12 Feb 2008
Posts: 3

PostPosted: Wed Feb 13, 2008 6:21 am    Post subject: Re: Finding open windows Reply with quote

> Put the following code into a new VB project module, enter a valid window
> handle in Sub Main and run the project.

Thanks for your help!

Your code crashes Access badly. I don't use VB but VBA. There is no
AddressOf in VBA but I use:

Public Function AddrOf(strFuncName As String) As Long
Dim hProject As Long
Dim lngResult As Long
Dim strID As String
Dim lpfn As Long
Dim strFuncNameUnicode As String

Const NO_ERROR = 0
Call GetCurrentVbaProject(hProject)
If hProject 0 Then
lngResult = GetFuncID(hProject, strFuncNameUnicode, strID)
If lngResult = NO_ERROR Then
lngResult = GetAddr(hProject, strID, lpfn)
If lngResult = NO_ERROR Then
AddrOf = lpfn
End If
End If
End If
End Function

Do you have any idea what the problem could be?

Juergen
Back to top
View user's profile Send private message
NeilH



Joined: 04 Oct 2007
Posts: 46

PostPosted: Wed Feb 13, 2008 2:28 pm    Post subject: Re: Finding open windows Reply with quote

"Juergen Grieb" wrote in message@q21g2000hsa.googlegroups.com...
> > Put the following code into a new VB project module, enter a valid
window
> > handle in Sub Main and run the project.
>
> Thanks for your help!
>
> Your code crashes Access badly. I don't use VB but VBA. There is no
> AddressOf in VBA but I use:
>
> Public Function AddrOf(strFuncName As String) As Long
> Dim hProject As Long
> Dim lngResult As Long
> Dim strID As String
> Dim lpfn As Long
> Dim strFuncNameUnicode As String
>
> Const NO_ERROR = 0
> Call GetCurrentVbaProject(hProject)
> If hProject 0 Then
> lngResult = GetFuncID(hProject, strFuncNameUnicode, strID)
> If lngResult = NO_ERROR Then
> lngResult = GetAddr(hProject, strID, lpfn)
> If lngResult = NO_ERROR Then
> AddrOf = lpfn
> End If
> End If
> End If
> End Function
>
> Do you have any idea what the problem could be?
>
> Juergen

Yes, very simply problem!
You are in the WRONG newsgroup!
You need a VBA newsgroup, not a VB group.
Back to top
View user's profile Send private message
Juergen Thuemmler



Joined: 04 Oct 2007
Posts: 225

PostPosted: Wed Feb 13, 2008 3:52 pm    Post subject: Re: Finding open windows Reply with quote

> Your code crashes Access badly. I don't use VB but VBA. There is no
> AddressOf in VBA but I use:

> Call GetCurrentVbaProject(hProject)
> lngResult = GetFuncID(hProject, strFuncNameUnicode, strID)
> lngResult = GetAddr(hProject, strID, lpfn)

Sorry, but I don't know the functions you use...My knowledge about VBA is
very poor.

Juergen.
Back to top
View user's profile Send private message
Vinchenzo_vinç



Joined: 04 Oct 2007
Posts: 33

PostPosted: Sat Feb 16, 2008 5:32 pm    Post subject: Re: Finding open windows Reply with quote

"Juergen Grieb" escribió en el mensaje @q21g2000hsa.googlegroups.com...
>> Put the following code into a new VB project module, enter a valid window
>> handle in Sub Main and run the project.
>
> Thanks for your help!
>
> Your code crashes Access badly. I don't use VB but VBA. There is no
> AddressOf in VBA but I use:
>
> Public Function AddrOf(strFuncName As String) As Long
> Dim hProject As Long
> Dim lngResult As Long
> Dim strID As String
> Dim lpfn As Long
> Dim strFuncNameUnicode As String
>
> Const NO_ERROR = 0
> Call GetCurrentVbaProject(hProject)
> If hProject 0 Then
> lngResult = GetFuncID(hProject, strFuncNameUnicode, strID)
> If lngResult = NO_ERROR Then
> lngResult = GetAddr(hProject, strID, lpfn)
> If lngResult = NO_ERROR Then
> AddrOf = lpfn
> End If
> End If
> End If
> End Function
>
> Do you have any idea what the problem could be?


Yes, Juergen, the problem is related to WinAPI, but take into account that not all people knows about Kaplan-Getz AddrOf "workaround", and can seems a VBA problem.

The problem is your function call to EnumThreadWindows:

'*************
ret = GetWindowThreadProcessId(wrdHndl, wrdPID)
ret = EnumThreadWindows(wrdPID, AddrOf("EnumThreadWndProc"), Windows)
'*************

GetWindowThreadProcessId returns the identifier of the thread, and this is the value that you need to pass to EnumThreadWindows instead of the process id 'wrdPID' (look twice at the "Sub Main" that Thuemmler has already posted). Then for instance, your calls should be:

'------------------------
···
Dim wrdThreadID As Long

···
wrdThreadID = GetWindowsThreadProcessId(wrdHndl, 0&) 'Actually, you don't need the ProcID
EnumThreadWindows wrdThreadID, AddrOf("EnumThreadWndProc"), Windows
···
'------------------------


--
Regards
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) Preceding answers in Google:
http://groups.google.com/group/microsoft.public.vb.winapi
( i ) Temperance in the forum:
http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Open Windows How does one locate the handle to "Open" desktop applications? I tried to enumerate all open windows but it returned dozens of hidden programs such as McAffee, Taskbar and so on. I just want to list open user applications which have an actual window visi

2 windows open when i click F1 in VB code when i click F1 in certain code of VB , my library help example( help1.chm) and VB help are opening. actually should open. closing VB help , my library help is also closing , therefore something related to parent relationship. how to hide or remove the

C# code to open a windows directory Hi All, I need the C# code to open a windows directory. Regards, Shilpa

Hide all open windows in an vb-application Hi, i have the following problem. I have an vb 6- application consisting of several Now i have the situation that i have several forms opened modal and these forms belong to more than one of the dll´s. I am on one of these forms and want to

Using DDE to open a file in an open instance of Oracle Disco Hi, When I double click a file with .DIS extention in my computer, it opens the currently opened instance of Oracle Discoverer application, then loads that ..DIS file. In the windows explorer window, by choosing Tools->Folder Options->File
Post new topic   Reply to topic    msvisual.com Forum Index -> VB WinAPI All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group