Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to control another applications message box from VB6

Posted on 2004-04-30
10
825 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

860 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