Solved

terminate application (dialog box)

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

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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 …
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…

758 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

17 Experts available now in Live!

Get 1:1 Help Now