EnumWindows (API)

im having trouble with the EnumWindows API; the fact that i know hardly anything about API doesnt help...what i am trying to do is get a list of the handles of the windows on the screen...the EnumWindows function is the best thing i could find (atleast it seems like it's what i'm lookin for to me)...i set it up the way it is in the API Text Viewer:
[code]Private Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long[/code]
from looking at the help in the msdn library it seems like lpEnumFunc is supposed to be some kind of function ([b]EnumWindowsProc[/b]) that is user defined and returns [italic]True[/italic] to tell EnumWindows to continue going and [italic]False[/italic] to tell it to stop (im not sure why API TextViewer didnt set it as a Boolean instead of a Long, but as far as working goes i dont think that matters)...lParam seems like its supposed to be some kind of Index of some sort...im not real sure...

whenever i use a [italic]True[/italic] value value for lpEnumFunc (i tried making a function and letting the function return the value) visual basic has an illegal operation...

[red]**NOTE: i did NOT try to assign a Boolean to a Long...i used a function that returned a Long and then did [italic]function[/italic] = True[/red]

like i said im new to API, so i'm probably getting it all wrong, plus the help for API being in C++ doesnt help much either...im not good with C++ so it takes me a while to try to convert what it says into vb...if anyone can help at all on this it would be much appreciated (inlcuding if you know another way to find the handle of all the windows on the screen)...thanks

Comments

  • : im having trouble with the EnumWindows API; the fact that i know hardly anything about API doesnt help...what i am trying to do is get a list of the handles of the windows on the screen...the EnumWindows function is the best thing i could find (atleast it seems like it's what i'm lookin for to me)...i set it up the way it is in the API Text Viewer:
    : [code]Private Declare Function EnumWindows Lib "user32" _
    : (ByVal lpEnumFunc As Long, _
    : ByVal lParam As Long) As Long[/code]
    : from looking at the help in the msdn library it seems like lpEnumFunc is supposed to be some kind of function ([b]EnumWindowsProc[/b]) that is user defined and returns [italic]True[/italic] to tell EnumWindows to continue going and [italic]False[/italic] to tell it to stop (im not sure why API TextViewer didnt set it as a Boolean instead of a Long, but as far as working goes i dont think that matters)...lParam seems like its supposed to be some kind of Index of some sort...im not real sure...
    :
    : whenever i use a [italic]True[/italic] value value for lpEnumFunc (i tried making a function and letting the function return the value) visual basic has an illegal operation...
    :
    : [red]**NOTE: i did NOT try to assign a Boolean to a Long...i used a function that returned a Long and then did [italic]function[/italic] = True[/red]
    :
    : like i said im new to API, so i'm probably getting it all wrong, plus the help for API being in C++ doesnt help much either...im not good with C++ so it takes me a while to try to convert what it says into vb...if anyone can help at all on this it would be much appreciated (inlcuding if you know another way to find the handle of all the windows on the screen)...thanks
    :
    :
    [code]
    Public Function EnumWindowsProc(ByVal hWnd As Long, lparam As long) As Long
    [/code]
    That should be the header for your function.

    My guess about how to call EnumWindows is:
    [code]
    EnumWindows AdressOf EnumWindowsProc, 0
    [/code]
    When EnumWindowsProc is called by Windows, hWnd is the handle of the current form being reported on and lparam appears to be the second value you passed to EnumWindows. If you want Windows to stop, then set EnumWindowsProc to False. Otherwise, set it to True.

    [red]EnumWindowsProc must be a public function in a bas module![/red]

    An unrecommended method for finding all the window handles is to use GetWindow. You would pass GetDesktopWindow and GW_HWNDCHILD the first time (to get the desktop's first child window) and then the return from the last function and GW_HWNDNEXT until the return is 0. THis is much easier to use in my opinion, but since it is dependent on the return from the previous call which is a window handle that might not exist anymore, it can be tricked.

    Hope this helps,
    KDL
  • : [code]
    : Public Function EnumWindowsProc(ByVal hWnd As Long, lparam As long) As Long
    : [/code]
    : That should be the header for your function.
    :
    : My guess about how to call EnumWindows is:
    : [code]
    : EnumWindows AdressOf EnumWindowsProc, 0
    : [/code]
    : When EnumWindowsProc is called by Windows, hWnd is the handle of the current form being reported on and lparam appears to be the second value you passed to EnumWindows. If you want Windows to stop, then set EnumWindowsProc to False. Otherwise, set it to True.
    :
    : [red]EnumWindowsProc must be a public function in a bas module![/red]
    :
    : An unrecommended method for finding all the window handles is to use GetWindow. You would pass GetDesktopWindow and GW_HWNDCHILD the first time (to get the desktop's first child window) and then the return from the last function and GW_HWNDNEXT until the return is 0. THis is much easier to use in my opinion, but since it is dependent on the return from the previous call which is a window handle that might not exist anymore, it can be tricked.
    :
    : Hope this helps,
    : KDL
    :



    thanks...that worked...i can now get a list of all [italic]active[/italic] apps running in windows...but now i get TOO MANY...it gives me all the hidden stuff (systray, desktop, the [b]START BUTTON![/b] for crying out loud)...all i wanted was the [italic]visible[/italic] programs...i tried using other API to see if i could find a way to tell the different programs apart (like by seeing which ones were hidden and which were not)...i cant find a way to see if they are hidden or not...the closest i came was by using [b]GetWindowPlacement[/b]...with that i could find if it was maximized or minimized or normal...so i can get values of 1=normal, 2=minimize, 3=maximized...unfortunately this function does not make a distinction between hidden forms and visible forms...all hidden forms also returned a value of 1...i also tried the [b]GetWindowText[/b] function which did narrow the list down by letting me exclude programs without a title, making it work for most forms and getting rid of systray etc...but still a lot of programs do have titles that remain hidden all the time...

    when i found my list i had 3 programs running...my program returned 119 different hWnd values...after using GetWindowText i can cut that almost in half...but still im no where near what i want to have...i want to get it narrowed down to 3...the 3 windows that i have open that i can see...if anyone has any ideas,then please help!

  • : : [code]
    : : Public Function EnumWindowsProc(ByVal hWnd As Long, lparam As long) As Long
    : : [/code]
    : : That should be the header for your function.
    : :
    : : My guess about how to call EnumWindows is:
    : : [code]
    : : EnumWindows AdressOf EnumWindowsProc, 0
    : : [/code]
    : : When EnumWindowsProc is called by Windows, hWnd is the handle of the current form being reported on and lparam appears to be the second value you passed to EnumWindows. If you want Windows to stop, then set EnumWindowsProc to False. Otherwise, set it to True.
    : :
    : : [red]EnumWindowsProc must be a public function in a bas module![/red]
    : :
    : : An unrecommended method for finding all the window handles is to use GetWindow. You would pass GetDesktopWindow and GW_HWNDCHILD the first time (to get the desktop's first child window) and then the return from the last function and GW_HWNDNEXT until the return is 0. THis is much easier to use in my opinion, but since it is dependent on the return from the previous call which is a window handle that might not exist anymore, it can be tricked.
    : :
    : : Hope this helps,
    : : KDL
    : :
    :
    :
    :
    : thanks...that worked...i can now get a list of all [italic]active[/italic] apps running in windows...but now i get TOO MANY...it gives me all the hidden stuff (systray, desktop, the [b]START BUTTON![/b] for crying out loud)...all i wanted was the [italic]visible[/italic] programs...i tried using other API to see if i could find a way to tell the different programs apart (like by seeing which ones were hidden and which were not)...i cant find a way to see if they are hidden or not...the closest i came was by using [b]GetWindowPlacement[/b]...with that i could find if it was maximized or minimized or normal...so i can get values of 1=normal, 2=minimize, 3=maximized...unfortunately this function does not make a distinction between hidden forms and visible forms...all hidden forms also returned a value of 1...i also tried the [b]GetWindowText[/b] function which did narrow the list down by letting me exclude programs without a title, making it work for most forms and getting rid of systray etc...but still a lot of programs do have titles that remain hidden all the time...
    :
    : when i found my list i had 3 programs running...my program returned 119 different hWnd values...after using GetWindowText i can cut that almost in half...but still im no where near what i want to have...i want to get it narrowed down to 3...the 3 windows that i have open that i can see...if anyone has any ideas,then please help!
    :
    :

    You can use an API to detect if a window is visible; I believe it's called IsVisible. That should eliminate a few more. Another API can be used to determine if a window shows up in the taskbar. The API is GetWindowLong and you would use it like this:
    [code]
    If GetWindowLong([italic]WindowHandle[/italic], GW_STYLE) And &H40000) Then
    'Shows up in taskbar
    Else
    'Doesn't
    End If
    [/code]
    That's not perfect either. You can try the GetParent API as well. If it is the one you want, then all the windows you want should return a 0 or GetDesktopWindow; I don't remember which.

    Let me know how it goes!
  • hey thanks...i ended up using IsWindowVisible and it worked perfectly...hah, i would've never found that, i thought i would need to get the window's properties and find out from there or something...but that worked out just right...thanks again!


    : : : [code]
    : : : Public Function EnumWindowsProc(ByVal hWnd As Long, lparam As long) As Long
    : : : [/code]
    : : : That should be the header for your function.
    : : :
    : : : My guess about how to call EnumWindows is:
    : : : [code]
    : : : EnumWindows AdressOf EnumWindowsProc, 0
    : : : [/code]
    : : : When EnumWindowsProc is called by Windows, hWnd is the handle of the current form being reported on and lparam appears to be the second value you passed to EnumWindows. If you want Windows to stop, then set EnumWindowsProc to False. Otherwise, set it to True.
    : : :
    : : : [red]EnumWindowsProc must be a public function in a bas module![/red]
    : : :
    : : : An unrecommended method for finding all the window handles is to use GetWindow. You would pass GetDesktopWindow and GW_HWNDCHILD the first time (to get the desktop's first child window) and then the return from the last function and GW_HWNDNEXT until the return is 0. THis is much easier to use in my opinion, but since it is dependent on the return from the previous call which is a window handle that might not exist anymore, it can be tricked.
    : : :
    : : : Hope this helps,
    : : : KDL
    : : :
    : :
    : :
    : :
    : : thanks...that worked...i can now get a list of all [italic]active[/italic] apps running in windows...but now i get TOO MANY...it gives me all the hidden stuff (systray, desktop, the [b]START BUTTON![/b] for crying out loud)...all i wanted was the [italic]visible[/italic] programs...i tried using other API to see if i could find a way to tell the different programs apart (like by seeing which ones were hidden and which were not)...i cant find a way to see if they are hidden or not...the closest i came was by using [b]GetWindowPlacement[/b]...with that i could find if it was maximized or minimized or normal...so i can get values of 1=normal, 2=minimize, 3=maximized...unfortunately this function does not make a distinction between hidden forms and visible forms...all hidden forms also returned a value of 1...i also tried the [b]GetWindowText[/b] function which did narrow the list down by letting me exclude programs without a title, making it work for most forms and getting rid of systray etc...but still a lot of programs do have titles that remain hidden all the time...
    : :
    : : when i found my list i had 3 programs running...my program returned 119 different hWnd values...after using GetWindowText i can cut that almost in half...but still im no where near what i want to have...i want to get it narrowed down to 3...the 3 windows that i have open that i can see...if anyone has any ideas,then please help!
    : :
    : :
    :
    : You can use an API to detect if a window is visible; I believe it's called IsVisible. That should eliminate a few more. Another API can be used to determine if a window shows up in the taskbar. The API is GetWindowLong and you would use it like this:
    : [code]
    : If GetWindowLong([italic]WindowHandle[/italic], GW_STYLE) And &H40000) Then
    : 'Shows up in taskbar
    : Else
    : 'Doesn't
    : End If
    : [/code]
    : That's not perfect either. You can try the GetParent API as well. If it is the one you want, then all the windows you want should return a 0 or GetDesktopWindow; I don't remember which.
    :
    : Let me know how it goes!
    :

  • : hey thanks...i ended up using IsWindowVisible and it worked perfectly...hah, i would've never found that, i thought i would need to get the window's properties and find out from there or something...but that worked out just right...thanks again!
    :

    Just glad I could help,
    KDL
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories