Solved

terminate application (dialog box)

Posted on 2001-07-17
12
344 Views
Last Modified: 2008-02-07
is there a way to terminate an application (dialogbox) with winAPI?
0
Comment
Question by:reggler
[X]
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
12 Comments
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6290124
Hi,

You can close any windows by sending it a close message using the SendMessage or PostMessage API calls.

Public Const WM_CLOSE = &H10
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

You need to HWND of the windows to close. If you don't know it, but you know the windows title, then you can enumerate all windows to find it. See http://www.allapi.net/api/EnumWindows.php

Zaphod.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6292007
Hi reggler,

Here is a sample:

'Close an application using the API.
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub cmdClose_Click()
    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, Text1.Text)
    Debug.Print winHwnd
    If winHwnd <> 0 Then
        RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
        If RetVal = 0 Then
            MsgBox "Error posting message."
        End If
    Else
        MsgBox Text1.Text + " is not open."
    End If
End Sub

'Hope will help.
0
 

Author Comment

by:reggler
ID: 6292431
hm yes taht works....for normal windows...
but i have a dialogbox ... it's the dialogbox when u receive a net send message. and i can't close it this way...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:reggler
ID: 6292460
i really have to congratulate you...
i'm coding in c++ but i have asked this question invb section because vb-coders are much better in API Stuff :o)
i have to translate the code always but that doesn't matter because i have to write less code than you (c++ rules) :o)
Erm anyway...

i saw that the problem is
hactive = FindWindow(NULL, "Nachrichtendienst");
this Line....it can't find the window....
how can i find that dialogbox?
0
 
LVL 5

Accepted Solution

by:
gwgaw earned 20 total points
ID: 6296967
This may work to get the handle of the dialogbox. Then use SendMessage or PostMessage
as shown above to close it. You may need to use other WS constants or combinations for
the style returned from GetWindowLong. Also, so you don't accidently close the wrong
window, experiment using the ShowWindow api to hide/show the dialogbox.

Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const GWL_STYLE = (-16)
Public Const SW_HIDE = 0
Public Const SW_SHOW = 5
Public Const WS_BORDER = &H800000
Public Const WS_CAPTION = &HC00000
Public Const WS_DLGFRAME = &H400000
Public Const WS_POPUP = &H80000000
Public Const WS_SYSMENU = &H80000
Public Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Public Const WS_THICKFRAME = &H40000

Public Function GetWindowHandle() As Long
Dim lhwnd As Long, style As Long
'Get handle for desktop
lhwnd = GetDesktopWindow()
'It's first child is the 1st top level window
lhwnd = GetWindow(lhwnd, GW_CHILD)
'search top level windows
Do
    'Check the window's style
    style=GetWindowLong(lhwnd, GWL_STYLE)
    if (style And WS_DLGFRAME) And (style And (Not WS_CAPTION)) then
        GetWindowHandle = lhwnd
        Exit Function
    End If
    'Get the next window
    lhwnd = GetWindow(lhwnd, GW_HWNDNEXT)
Loop While lhwnd <> 0
End Function
0
 

Author Comment

by:reggler
ID: 6367729
hi back...sorry for the delay but i was in vacation...

thx gwgaw for your suggestion...
but i have some problems to translate it to c/c++
seems to be very complicated to find that dialog box....

okay, my code:

long GetWindowHandle(void)
  {
  HANDLE lhwnd = NULL;
  long /*lhwnd,*/ style;
  //Get handle for desktop
  lhwnd = GetDesktopWindow();
  //It's first child is the 1st top level window
  lhwnd = GetWindow(lhwnd, GW_CHILD);
  //search top level windows
  do
    {
    //Check the window's style
    style=GetWindowLong(lhwnd, GWL_STYLE);
    if ((style && WS_DLGFRAME) && (style && (!WS_CAPTION)))
      {
      GetWindowHandle = lhwnd;
      exit(0);
      }
    //Get the next window
    lhwnd = GetWindow(lhwnd, GW_HWNDNEXT);
    }
  while (lhwnd != 0);
  }


GetWindowHandle = lhwnd;
on this line i get some problems....what is GetWindowHandle? That's no API call...
0
 

Author Comment

by:reggler
ID: 6367732
oh sorry...that's the return value....huah ****...excuse me...
0
 

Author Comment

by:reggler
ID: 6367770
i have some problems anyway...

my const

#define GW_CHILD 5
#define GW_HWNDNEXT 2
#define GWL_STYLE (-16)
#define SW_HIDE 0
#define SW_SHOW 5
#define WS_BORDER &H800000
#define WS_CAPTION &HC00000
#define WS_DLGFRAME &H400000
#define WS_POPUP &H80000000
#define WS_SYSMENU &H80000
#define WS_POPUPWINDOW (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
#define WS_THICKFRAME &H40000

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  HANDLE lhwnd = NULL;
  long style;
  //Get handle for desktop
  lhwnd = GetDesktopWindow();
  //It's first child is the 1st top level window
  lhwnd = GetWindow(lhwnd, GW_CHILD);
  //search top level windows
  do
    {
    //Check the window's style
    style=GetWindowLong(lhwnd, GWL_STYLE);
    if ((style && WS_DLGFRAME) && (style && (!WS_CAPTION)))
      {
      hactive = lhwnd;
      goto GoOn;
      }
    //Get the next window
    lhwnd = GetWindow(lhwnd, GW_HWNDNEXT);
    }
  while (lhwnd != 0);

  GoOn:

  if (hactive)
     {
     retval = PostMessage(hactive, WM_CLOSE, NULL, NULL);
     if (retval = 0) ShowMessage("Error posting message.");
     }
   else ShowMessage("Nod offe.");

}

my code...

okay...
(i know it's c++ :o) )
erm i receive compiler error "undefined symbol H400000"

what means that & before H400000 ?
it's possible that i have to change that for c/c++...
thx for your support
0
 

Author Comment

by:reggler
ID: 6439763
haha got the problem
the dialogbox from the netsend msg has a space at the end of the msg-box title :o)

so it is:

hWnd = FindWindowEx(NULL,NULL, "#32770", "Nachrichtendienst ");

SendMessage(hWnd, WM_CLOSE, NULL, NULL);


that was the solution. thx anyway for help...
0
 
LVL 3

Expert Comment

by:modder
ID: 6535763
reggler and RideOn are duplicate accounts and have been closed.

Please advise as to who should get the points.

modder
Community Support
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7153502
reggler, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Accept gwgaw's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7182152
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RUNRMTCMD from AS/400 13 100
Formula problem with Excel attachment 6 43
how to Classify of email (pdf attachments) 7 50
Error with a code discussed on this page 5 39
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

733 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