Results 1 to 15 of 15

Thread: [RESOLVED] How to know if a file-dialog has appeared?

  1. #1

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Resolved [RESOLVED] How to know if a file-dialog has appeared?

    Hello fellow programmers,
    I am automating a file upload routine on some webpage. The webpage 'upload file' button is clicked and a file-dialog appears. I am looping to find the handle of this popup-window. Then I find the handle of the file textfield, update it, and click "OK/send/save". Everything is working perfectly and files are uploaded.

    The above routine is executed rather quickly (1-2 seconds)... The problem is that this routine is entirely executed (and OK/send/save button clicked) even BEFORE the file-dialog actually shows up on screen. The reason for the file-dialog to appear "so slowly" is that the CPU may currently be struggling over some other tasks. Sometimes it will show instantly, sometimes it can take as much as ~30 seconds to show (yes, really).

    My question: would there be a way to wait for the file-dialog to APPEAR on screen before executing the above routine? I thought about looking up the process/application list (Win7's Task Manager), but such a file-dialog popup won't appear in the "applications" tab, or in the "processes" tab. I do have the hwnd of the popup, anything I can do with that? I would have wished for the handle/hwnd extraction to NOT WORK until it appears on screen, but no... it seems that those handles are properly found even with that popup not yet visible. In the end, the popup ends up appearing, doing nothing, waiting for my action.

    Thank you for any ideas.....
    Last edited by Krass; Sep 3rd, 2017 at 08:58 PM.
    Chris

  2. #2
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    160

    Re: How to know if a file-dialog has appeared?

    My mind must have been somewhere else..
    sorry
    Last edited by Inside; Sep 3rd, 2017 at 10:13 PM. Reason: I must have smoking weed or something...
    Programmers are very patient people....while programming....but don't expect them to be patient all the time

  3. #3
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: How to know if a file-dialog has appeared?

    Krass

    Could you perhaps post a screen-shot of what you are talking about?
    Also, could you distinguish between ..
    • which part is your app
    • which part is the webpage


    Spoo

  4. #4

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Re: How to know if a file-dialog has appeared?

    Hello Spooman.
    Here's a link showing up such a file-dialog (found on google): https://social.technet.microsoft.com...getfile/113110
    ...as you can see, this is just a standard file-dialog popup we see all the time in windows.

    The webpage I am talking about will be browsed with the webbrowser control within my vb6 app. The webpage is not mine, only the app I'm building.

    Since my initial post, I've had the idea of using GetWindowRect to get window dimensions of this popup. Unfortunately, this returns the window's height and width even tho it's not yet showing up on screen.

    Would there be a way to use the handle of the popup (which I already have) and ask windows to DRAW/REDRAW/REFRESH it?

    Thank you!
    Chris

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to know if a file-dialog has appeared?

    Say Spoo,

    This all sounds pretty spooky to me. What's Krass plan on doing? Overlaying the file dialog with his own form, thereby spoofing the user and grabbing his file? I'd sure want a more detailed explanation about what's going on here before I got too involved.

    Just Saying,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Re: How to know if a file-dialog has appeared?

    Hi Elroy,
    I don't think I even understand your worries. Spoofing a user, and grabbing his file?! If that was the case, if the file/webpage was not passworded or anything, I could just take the file, couldn't I? But that's not at all what I'm doing here.

    It's very pretty simple: I'm using a webbrowser to automate a file upload. And it's actually working good. I'm interested in tweaking it to get rid of some useless timers/pause and have this app run quicker. This pause I'm talking about is, if you read my initial post, to make sure the popup has appeared. It may take a while before it appears and if I click "SUBMIT" before it has appeared, it won't be uploading anything.

    I can't blame anyone for being suspicious, but I do think you should pick your battles

    Thank you!

    (I've found some functions that will REDRAW a window... I will try it out soon and see if it makes the popup appear more quickly)
    Chris

  7. #7
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    160

    Re: How to know if a file-dialog has appeared?

    What about a simple doevents or a short timer-set (3000) with a doevents inside? It's not that the code have to be written in html code or java or something else where the program-language differs...
    Programmers are very patient people....while programming....but don't expect them to be patient all the time

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to know if a file-dialog has appeared?

    Well, ok Krass. I'll brainstorm a bit for you. First, if it's working, what do you care if the user sees the "Choose File to Upload" dialog or not?

    And, maybe in the end, you're just looking for something as simple as...

    Code:
    
    Option Explicit
    '
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Const GWL_STYLE As Long = (-16)
    Private Const WS_VISIBLE = &H10000000
    '
    
    Public Function WindowIsVisible(hWndOfInterest As Long) As Boolean
        WindowIsVisible = ((GetWindowLong(hWndOfInterest, GWL_STYLE) And WS_VISIBLE) = WS_VISIBLE)
    End Function
    
    
    
    Also, I'm not at all sure I'd use a web browser to upload a file. There are certainly several native VB6 approaches to uploading a file, either HTTP or FTP. If you're into reworking things a bit, you may want to do a search of the codebank.

    Good Luck,
    Elroy

    EDIT1: Since you say you're already locating the window, I assumed you had its hWnd.

    EDIT2: Also, a good point about language differences. If they're not set to "English", your "find window" procedure may get broken, as that "Choose File to Upload" may not read quite the same. My main program is English, but I do have to deal with both French and Spanish locales on occasion.
    Last edited by Elroy; Sep 5th, 2017 at 02:37 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: How to know if a file-dialog has appeared?

    If you know the thread, I would just use SetWindowsHookEx.

    and wait for HCBT_ACTIVATE


    of course this may not be possible with an external app...


    edit:have you tried GetForegroundWindow ? and waiting to see if it matches the hWnd you've got?
    Last edited by DEXWERX; Sep 5th, 2017 at 02:47 PM.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to know if a file-dialog has appeared?

    Well, if he knows the hWnd, getting the ThreadID is easy enough: GetWindowThreadProcessId discussed here.

    Code:
    
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    
    
    EDIT1: Just supplying the hWnd, and leaving the lpdwProcessId = 0 gets it done.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to know if a file-dialog has appeared?

    Browsers sandbox these operations as a security measure. This is yet another thread on topics we do not discuss here.

    If it is legitimate to upload files to this site from bots then they should provide an API for the purpose. If they don't then you shouldn't be attempting it.

  12. #12

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Re: How to know if a file-dialog has appeared?

    Quote Originally Posted by dilettante View Post
    Browsers sandbox these operations as a security measure. This is yet another thread on topics we do not discuss here.

    If it is legitimate to upload files to this site from bots then they should provide an API for the purpose. If they don't then you shouldn't be attempting it.
    I do not agree at all. Just because a programmer is attempting something unusual (or weird?) doesn't mean it's not useful or not legitimate. I could see many reasons and situations for the request I had. On the other hand, do you really think a webmaster would bother with creating an API, while the webpage will suffice 99% of their users? Waste of time on their side. Even tho it's not the case here, I could have been trying to automate some tasks using my OWN website. Those kind of situations/needs comes up when you think outside the box... Have a great day.

    Post coming up right now about how this problem was now solved.
    Chris

  13. #13

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Re: How to know if a file-dialog has appeared?

    Elroy: THANK YOU - this WindowIsVisible procedure was just what I needed and it works like a charm. As you asked: NO, I wouldn't care much if the user sees the file-dialog or not... but STILL, I *had* to wait until it's fully shown or else the 'submit' button would have been clicked before it even shows on screen. Doing so doesn't trigger the file upload at all... the file-dialog ends up appearing and stays there doing nothing. Also I would have been happy to use FTP/HTTP which, in this case, was not possible. And thanks for pointing out the caption of the window that might change - this won't be a problem for this particular project.

    Inside: That's actually what I had until now: a loop with DoEvents (yes, this is a bad habit and should avoid it), but... This loop was made longer just to make sure the OS had time to draw the file-dialog. So if I use a 30 seconds timer and the file-dialog shows after 5 secs.. 25 seconds are lost! I needed some code to tell me the dialog has actually really shown up.

    DEXWERX: Thanks for your solution - looked cool, but since the previous suggestion is working so great, I won't need to try yours
    Chris

  14. #14
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: [RESOLVED] How to know if a file-dialog has appeared?

    Just because a programmer is attempting something unusual (or weird?) doesn't mean it's not useful or not legitimate.
    True, but it does raise enough suspicion for us to at least raise an eyebrow. Particularly when you're talking about automating web site because this is an activity that web site owners typically frown upon and explicitely prohibit in their terms and conditions. With that in mind you shouldn't be too surprised that we'll challenge you before helping. At the very least you should let us know which site you're automating so we can check the Ts and Cs for ourselves.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  15. #15
    New Member
    Join Date
    Jan 2018
    Posts
    1

    Re: [RESOLVED] How to know if a file-dialog has appeared?

    Quote Originally Posted by Krass View Post
    Hello fellow programmers,
    I am automating a file upload routine on some webpage. The webpage 'upload file' button is clicked and a file-dialog appears. I am looping to find the handle of this popup-window. Then I find the handle of the file textfield, update it, and click "OK/send/save". Everything is working perfectly and files are uploaded.

    The above routine is executed rather quickly (1-2 seconds)... The problem is that this routine is entirely executed (and OK/send/save button clicked) even BEFORE the file-dialog actually shows up on screen. The reason for the file-dialog to appear "so slowly" is that the CPU may currently be struggling over some other tasks. Sometimes it will show instantly, sometimes it can take as much as ~30 seconds to show (yes, really).

    My question: would there be a way to wait for the file-dialog to APPEAR on screen before executing the above routine? I thought about looking up the process/application list (Win7's Task Manager), but such a file-dialog popup won't appear in the "applications" tab, or in the "processes" tab. I do have the hwnd of the popup, anything I can do with that? I would have wished for the handle/hwnd extraction to NOT WORK until it appears on screen, but no... it seems that those handles are properly found even with that popup not yet visible. In the end, the popup ends up appearing, doing nothing, waiting for my action.

    Thank you for any ideas.....
    Hi Krass!

    Can you send me your code to send path and click accept in your dialog window??? i am searching this code , thank you

Posting Permissions

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



Click Here to Expand Forum to Full Width