FindWindow() issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: FindWindow() issue

  1. #1
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Question FindWindow() issue

    hello,

    I'm using FindWindow() function to find a specific window/dialog.
    as all the windows have the same name I can not use the Windowname in the function.
    (if I use it, the function finds the first, while I need the third)
    using a counter is not an option think of the possibility of next en back buttons
    so I have to use the Classname arg., I think...
    I know all the windows have static text items.
    window 3 has 5 edit control boxes, I also know the ID numbers (Spy++ and Orca)
    they are 1304 to 1308. So window 3 is Unique

    how do I define the function, so it catches the 3 window?

    regards,

    ger

  2. #2
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: FindWindow() issue

    anybody...

    regards,

    ger

  3. #3
    Join Date
    Dec 2008
    Posts
    144

    Re: FindWindow() issue

    I guess if the windows have the same titles their classnames in most cases are also identical. So you can't use it to differentiate them.
    It's hard to answer something without having enough information.
    What's your "initial" problem? Why should you find this window?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,201

    Re: FindWindow() issue

    The general strategy is to use all the criteria you can to get the window you need.

    Here's what you have to work with:
    1) The pid of the application. If you have launched the application, then you have its pid. The pid can be used to eliminate windows that aren't in the target application. Use GetWindowThreadProcessId to determine if a window is part of the target app.
    2) Use the window title. If the title is constant you should always use it. If it changes then try to use a portion of the title that may not change. For example, the top level window title in the IE window I'm typing in is "CodeGuru Forums - Repy to Topic - Windows Internet Explorer". If I'm searching for this window, I know that the "Windows Internet Explorer" portion is constant so I can do a partial match for that.
    3) Use the class name. The strategy here is similar to the title. Use the class name whenever possible and a partial match is better than nothing.
    4) Search with the parent window. A good strategy is always supply a parent window handle with searching for a child window. This eliminates finding windows with the same title and class name, but have a different parent window.
    5) Use all in combination. I usually launch the target application, store its pid, and then immediately find its top level window. I then use the top level window to build a hierarchy of windows as needed. That way all my child windows searches are performed using the hierarchy that I've built up. And because I've restricted the first top level window by the pid, I am ensured that I will only find windows in my application.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,782

    Re: FindWindow() issue

    If the window you need cannot be identified solely by title/classname:
    use EnumWindows(), and provide a callback to do whatever additional testing you need to identify the proper window.

  6. #6
    Join Date
    Oct 2009
    Posts
    4

    Re: FindWindow() issue

    EnumWindows() is what I would use in this case too.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,201

    Re: FindWindow() issue

    While EnumWindows is a great choice for determining the top level window for an application, it's not very performant (because it enumerates all the windows on the system).

    If you are trying to capture child windows of a specific window or dialog, FindWindowEx or EnumChildWindows will yield better performance.

  8. #8
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: FindWindow() issue

    Thank you guys for all the replies !!

    to be more specific.
    I made a program which calls at certain time an installer (Call of Duty -2-).
    I use the call _spawnlp with the _P_NOWAIT option.
    when dialog three shows up a serial has to be inserted.
    using the Sleep(number) in combination with keybd_event('X', 0, 0, 0 )
    I managed to insert the text.
    but the Sleep() option is not waterproof!! (read poor programming)
    so I need to capture the third window iso using a timer.
    using PID is also difficult as the installer also creates new processses.
    As all windows are using the same title "Call odf Duty 2" I can not differentiate on that,
    however dialog three has 5 edit boxes. This an unique point as the installer will not
    move on as long as the correct serail is not inserted.

    is there a way to test on the existence of the edit boxes or on a part of static text?

    I have also been testing with a program called AutoIt v3
    it uses a call like
    WinWaitActive("Call of Duty(R) 2","The Key Code is loca")
    to find the requested window/dialog.

    I will have a go with the posted suggestions

    regards,

    gerwin

  9. #9
    Join Date
    Nov 2007
    Posts
    613

    Re: FindWindow() issue

    You can use EnumChildWindows to enumerate all the child windows of a given top level window.

    Then you can use GetDlgItem to find a specified control in a window. That function requires the identifier of the control. You can find that identifier using the Spy++ application.

    Note that the identifier is a constant and will have the same value every time you run the application. So you need to find it only once, then you can hard code the identifier in your application.

  10. #10
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,915

    Re: FindWindow() issue

    Quote Originally Posted by srelu View Post
    Note that the identifier is a constant and will have the same value every time you run the application. So you need to find it only once, then you can hard code the identifier in your application.
    Not necessarily. It is not a constant. It may be defined as constant in a code but ID of any window can be changed on the fly by the applicationís code.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  11. #11
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: FindWindow() issue

    this should to the trick

    PID = _spawnlp(_P_NOWAIT,"C:\\COD2\\COD2DVD\\setup.exe"," ",NULL);
    lhwnd = NULL;
    lhwnd_test = NULL;
    while (lhwnd == NULL)
    {
    lhwnd = FindWindow(NULL, L"Call of Duty(R) 2");
    if (lhwnd != NULL)
    lhwnd_test = GetDlgItem(lhwnd, 1304);
    if (lhwnd_test == NULL)
    lhwnd = NULL;
    Sleep(100);
    }

    any finetuning comments

    regards.

    ger

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: FindWindow() issue

    Are you trying to break this game's registration by brute force?
    First, this is illegal.
    And second, this is stupid.

    P.S. Your original post smelled like a cheat; didn't want to say anything until I was sure.

    P.P.S. This forum used to be full of interesting questions / answers. Now it mostly has requests from kiddie hackers (who haven't mastered "Hello World" yet) or homework assignment... That's a shame.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  13. #13
    Join Date
    Oct 2009
    Posts
    4

    Re: FindWindow() issue

    Virus and anti-virus technology, we need to study them. Just not do bad things on the list.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,201

    Re: FindWindow() issue

    Understand that on this forum, we are if violation of the AUP if we answer any virus related questions.

  15. #15
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,782

    Re: FindWindow() issue

    A lot of legitimate stuff can be virus related.

    I find myself spending more and more time into projects that need me to interface to other programs, often programs that weren never intended to be interfaced to. So I tend to need a lot of low level stuff that could just as easily be used maliciously.

    Even stuff like this very question. Automating/scripting an installation that hasn't got provesions for it is part of the common problems I'm needing to solve. There's a thin line between filling in a known registration key and filling in a registration key at random trying to break a code. All of the code would be the same except for 3 or 4 lines of code.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center