Link to home
Start Free TrialLog in
Avatar of wolfjjj
wolfjjj

asked on

Closing an Application using VB Code

OK I've got an application that executes an Application (BDE Administrator - app i didnt write) and changes the selected INI file through command line arguments. So basically what I need to do is close this application using VB Code. I can do this most of the time using the following code

Dim hWindow As Long, hWindow2 As Long
   Dim lngResult As Long, lngResult2 As Long
   Dim lngReturnValue As Long, lngReturnValue2 As Long

   hWindow = FindWindow(vbNullString, ProgramCaption)
   lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
   lngResult = WaitForSingleObject(hWindow, INFINITE)

   'Does the handle still exist?
   DoEvents
   hWindow = FindWindow(vbNullString, ProgramCaption)
   If IsWindow(hWindow) = 1 Then
      'The handle still exists. Use the TerminateProcess function
      'to close all related processes to this handle. See the
      'article for more information.
      'MsgBox "Handle still exists."
   Else
      'Handle does not exist.
      'MsgBox "Program closed."
   End If

This works fine unless you have changed the INI file and then when you exit it brings up a Message Box asking you to confirm the changes you have made. Basically I need to be able to send a message to the application that sends the equivilant of a click of the Yes button on the message box. So how the heck can I do this???

I tried doing a findwindow and it picks up the confirmation window and then I did a sendmessage with various values and couldn't get it to select Yes. Any ideas on how I can do this.
ASKER CERTIFIED SOLUTION
Avatar of egsemsem
egsemsem

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wolfjjj
wolfjjj

ASKER

Nar I got rid of the last bit from the 'Does the handle still exist? down. It's really just closing the Confirmation msgbox that is the problem
I understood that if a message box appeared, you want to send a click to yes button.
my idea is that after you send WM_CLOSE, you check whether the window still exist or not, if it still exists, then perhaps there is a message box, and so the program sends 'y' charachter to press 'yes' button.
If you think the window may still not be closed, add these lines after ' SendKeys "y" ' :

If IsWindow(hWindow) = 1 Then
     'The handle still exists. Use the TerminateProcess function
     'to close all related processes to this handle. See the
     'article for more information.
     'MsgBox "Handle still exists."
  Else
     'Handle does not exist.
     'MsgBox "Program closed."
  End If


If I misunderstood you, let me know

Osama
Avatar of wolfjjj

ASKER

Yep you're right on but how do I do the sendkeys to another application that I didnt write and dont have any control over?
'sendkeys' function sends a key to the active window whatever it's. It acts eactly as if you pressed a button in the keyboard.  And so I firstly call setwindowpos, to set the window as an active window, then I sent 'y' key which presses 'yes' button in the message box.

If the code doesn't work, let me know

Osama
Avatar of Ark
Const ID_OK = &H1
Const WM_COMMAND = &H111

Call SendMessage(hMsg, WM_COMMAND, ID_OK, ByVal 0&)

Cheers
Avatar of wolfjjj

ASKER

Hi wolfjjj,
You've requested to delete this question, but its status remains as 'Pending Delete' because one or more comments have been added.  Normally, the only way to fully delete such a Question is to post a message to Community Support and ask for assistance.

EE is making a one-time database sweep to purge the Pending Delete Questions automatically.  During this sweep:

    wolfjjj -- To allow the deletion to proceed:  Do nothing.
    EXPERTS -- Please DON'T POST a comment except to contest this deletion.

In the future, please refer to https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp#8 for instruction on deleting questions.

DanRollins -- EE database cleanup volunteer
Why delete this question?!!
 I think I gave a fairly good solution! but wolfjjj didn't respond to my last comment, If there is still some problems in my solution, please let me know.

Osama
Recommended disposition:

    Split points between: egsemsem and Ark

DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator

Ark: points for you at https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20315282