Solved

How to control another applications message box from VB6

Posted on 2004-04-30
10
812 Views
Last Modified: 2013-12-25
I have a program that runs the ImportExportFavorites command of ShellUIHelper.  Natually this causes IE to ask "are you sure you wanna import your favs?" and "operation complete (click okay)".  Ideally i'd like to run ShellUIHelper with an option that tells it not to show these messages, but i didnt see that as an option.  So now the only thing i can think of is sendkeys "y" and "<enter>".  Problem is that since this is an import command, the time varies.  I could just loop for a while, or set a timer and send them- but that is sloppy.  I'm trying to find a way to search for the message box, but i have know idea what its name is.  It has no taskbar real estate and the name "Import Favorites" (which is shown ontop of the box) didnt work with appactivate.  My plan is to have this program run late at night when no one is using the computer; however i cant be sure that someone wont send an IM or something.  Please help.... running out of ideas.

e
0
Comment
Question by:irkgreen
  • 5
  • 2
10 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10960091
Have you already tried using FindWindow API to find the handle of the Window?
0
 

Author Comment

by:irkgreen
ID: 10960125
No... how can i do that?  Will it give me a unique name for that window?  Will i hafta run findwindow while the message is open?
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 250 total points
ID: 10960498
It will return the handle for the window,then you could use FindWindowEx to get the handle of the button that needs to be clicked.It would have to be running in the background.You could use a Timer with the interval set to 1000 and use something like this:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Const BM_CLICK = &HF5

Private Sub Timer1_Timer()
Dim lWin As Long, Control As Long
' get handle to the window by finding caption
lWin = FindWindow(vbNullString, "The Windows Caption Goes Here")
If lWin <> 0 Then
    ' get handle to Ok button
    Control = FindWindowEx(lWin, 0, "Button", "Ok")
    ' click the button
    SendMessage Control, BM_CLICK, 0, 0
End If

End Sub
0
 

Author Comment

by:irkgreen
ID: 10960841
I tried that and i think i am close.  I think "import favorites" is not the actual caption name though.  My code:

Again:
lWin = FindWindow(vbNullString, "Import Favorites")
If lWin <> 0 Then
    'get handle to Ok button
    Control = FindWindowEx(lWin, 0, "Button", "Yes")
    'click the button
    SendMessage Control, BM_CLICK, 0, 0
Else
GoSub Again:
End If

"Import Favorites" is the tilte of the message box (yes/no) but perhaps not the caption.  Sicnce it doesnt have a taskbar slot i dont think finwindow is finding it. or perhaps it has a different name that i dont know about.  Any ideas???
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:irkgreen
ID: 10961854
i changed the lwin line to:

lWin = FindWindow("#32770", Null)

lWin = FindWindow("#32770", "Import Favorites")

I used a small app to determine the classname of the message box window and found it (and most messageboxes) to be named #32770.  A null for the caption is supposed to accept anything... but neither worked :(

not sure what to do.  I think that it is possible that the message box is actually part of my application and not an extension of Internet Explorer (which i assumed it was).  So i might rephrase my question to "How to control an application's message box from VB6"?
0
 

Author Comment

by:irkgreen
ID: 10962040
ok this actually made it into the IF satement:

Again:

lWin = FindWindow("#32770", vbNullString)
If lWin <> 0 Then
    MsgBox ""
    'get handle to Ok button
    Control = FindWindowEx(lWin, 0, "Button", "Okay")
    'click the button
    SendMessage Control, BM_CLICK, 0, 0
Else
GoSub Again:
End If

it seems the vbnullstring is the proper variable to use; however i have to click yes to import favorites and okay to dismiss the successful import before lwin<>0.  So it seems that perhaps the program is on hold until the user clicks an option.

Could there be a way to start a new sub that triggers when a message box appears?
0
 

Author Comment

by:irkgreen
ID: 10989492
I've solved my problem, sorta.  I realized that the message box was putting a stop on the thread and since VB6 only supports a single thread I had to go another route. Since i dont have VB.NET i had to compile a second exe to enumarate all of the windows searching for the message box and send a key to dismiss it.  The original exe simply called on the 2nd to do this.  Works fine.

I am not sure how or if i can close this thread.
0
 

Expert Comment

by:satishpreddy
ID: 11807338
general function, that will display message boxes with minimun width and height. If message exceeds minimum width and height, then it should resize and adjust to text.
 
And, for errors, show exlamation, for successful messages show question mark.
 
So, on a high level, the message box function shall look like,
 
displaymessage(tMessage as string, tMessageType, tMessageIcon)
 
Include parameters as required.
 
Thanks and regards

Sai Gopal
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now