• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

terminate application (dialog box)

is there a way to terminate an application (dialogbox) with winAPI?
0
reggler
Asked:
reggler
1 Solution
 
Z_BeeblebroxCommented:
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
 
Ryan ChongCommented:
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
 
regglerAuthor Commented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
regglerAuthor Commented:
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
 
gwgawCommented:
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
 
regglerAuthor Commented:
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
 
regglerAuthor Commented:
oh sorry...that's the return value....huah ****...excuse me...
0
 
regglerAuthor Commented:
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
 
regglerAuthor Commented:
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
 
modderCommented:
reggler and RideOn are duplicate accounts and have been closed.

Please advise as to who should get the points.

modder
Community Support
0
 
DanRollinsCommented:
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
 
Computer101Commented:
Comment from expert accepted as answer

Computer101
E-E Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now