|
-
Apr 7th, 2013, 04:38 AM
#1
Thread Starter
PowerPoster
-
Apr 7th, 2013, 04:35 PM
#2
Re: forcing a window\program to close
The DestroyWindow documentation states:
 Originally Posted by MSDN
A thread cannot use DestroyWindow to destroy a window created by a different thread.
However, you can send the WM_CLOSE message instead, or call the TerminateProcess function as a last resort.
Code:
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As Long, Optional ByRef lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function SendMessageTimeoutW Lib "user32.dll" (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long, Optional ByVal fuFlags As Long, Optional ByVal uTimeout As Long, Optional ByRef lpdwResult As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Sub ForceCloseWindow(ByVal hWnd As Long)
Const PROCESS_TERMINATE = &H1&, WM_CLOSE = &H10&
Dim PID As Long, hProcess As Long
GetWindowThreadProcessId hWnd, PID
hProcess = OpenProcess(PROCESS_TERMINATE, 0&, PID)
If hProcess Then
If SendMessageTimeoutW(hWnd, WM_CLOSE, 0&, 0&, uTimeout:=5000&) = 0& Then 'Try closing it first (wait 5 secs.)
PID = TerminateProcess(hProcess, 0&): Debug.Assert PID 'If failed, then terminate it
End If
hProcess = CloseHandle(hProcess): Debug.Assert hProcess
End If
End Sub
References:
How To Programmatically Close a Single Instance of a Windows-Based Program
How To Terminate an Application "Cleanly" in Win32
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 7th, 2013, 05:34 PM
#3
Re: forcing a window\program to close
Code:
void ForceCloseWindow(HWND hWnd)
{
DWORD PID, dwRet;
HANDLE hProcess;
dwRet = GetWindowThreadProcessId(hWnd, &PID);
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, PID);
if (hProcess)
{ // Try closing it first (wait 5 secs.)
if (SendMessageTimeout(hWnd, WM_CLOSE, NULL, NULL, SMTO_NORMAL, 5000, &dwRet) == 0)
{ // If failed, then terminate it
TerminateProcess(hProcess, 0);
}
CloseHandle(hProcess);
}
}
Last edited by Bonnie West; Apr 8th, 2013 at 06:12 AM.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 8th, 2013, 06:53 AM
#4
Thread Starter
PowerPoster
Re: forcing a window\program to close
thanks for all.. now no errors. but the program isn't terminated
Code:
#include <stdio.h>
#include <windows.h>
#include <string.h>
void ForceCloseWindow(HWND hWnd)
{
DWORD PID, dwRet;
HANDLE hProcess;
dwRet = GetWindowThreadProcessId(hWnd, &PID);
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, PID);
if (hProcess)
{ // Try closing it first (wait 5 secs.)
if (SendMessageTimeout(hWnd, WM_CLOSE, NULL, NULL, SMTO_NORMAL, 5000, &dwRet) == 0)
{ // If failed, then terminate it
TerminateProcess(hProcess, 0);
}
CloseHandle(hProcess);
}
}
void main()
{
HWND TopWindowHWND;
HDC TopWindowDC;
char Text[]="Busted";
Sleep(10000);
while(1)
{
//get the top most window(DC and handle)
TopWindowHWND = GetActiveWindow();
TopWindowDC = GetDC(TopWindowHWND);
//put some text(using these you can see if works)
TextOut(TopWindowDC,0,0, Text, strlen(Text));
//these test if the window is maximizated and if isn't the desktop
SetWindowPos(GetDesktopWindow(),HWND_TOPMOST,0,0,1,1,SWP_NOSIZE);
if(TopWindowHWND!= GetDesktopWindow())
{
//ShowWindow(TopWindowHWND,0);
//if is then close it and give us a nice message;)
//SendMessage(TopWindowHWND, WM_SYSCOMMAND, WM_QUIT, NULL);
//DestroyWindow(TopWindowHWND);
ForceCloseWindow(TopWindowHWND);
MessageBox(NULL, "Finnaly the virus was closed... wellcome to desktop", "GoodBye Virus", MB_OK | MB_ICONINFORMATION);
break;
}
Sleep(100);
}
}
i have 100% about the handle, because i use the TextOut().
and see my VB 'virus' code:
Code:
Option Explicit
Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As _
Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As _
Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE
Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED
Private Const SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1
Dim blnExit As Boolean
Private Sub Command1_Click()
blnExit = True
End
End Sub
Private Sub Form_Load()
Me.Show
' Move window Form1 to the upper-left corner of the screen and make
' at appear above all other windows permanently. Note how the function is told not
' to resize the window, so we don't have to worry about the lower-right coordinate.
Dim flags As Long ' the flags specifying how to move the window
Dim retval As Long ' return value
' Do not resize the window, redraw the window in its new location
flags = SWP_NOSIZE Or SWP_DRAWFRAME
Do
If blnExit = True Then Exit Do
retval = SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 1, 1, flags) ' move the window
DoEvents
Loop
End Sub
what you can tell me?
why these 'virus' isn't closed?
PS: sorry i can't Rated you, because of the forum rules.
Moderator: these isn't a virus.. these just use a loop for put, allways, the window in front of all windows. but if theres a problem, please tell me. and if you want move these thread to another subforum, just do thanks for all
-
Apr 8th, 2013, 01:15 PM
#5
Re: forcing a window\program to close
Try these:
Code:
'In a blank Form
Option Explicit
Private Const HWND_TOPMOST As Long = (-1&)
Private Const SWP_NOSIZE As Long = &H1
Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal uFlags As Long) As Long
Private WithEvents Command1 As CommandButton
Private WithEvents Timer1 As Timer
Private Sub Command1_Click()
Timer1 = False
Unload Me
End Sub
Private Sub Form_Activate()
Set Command1 = Controls.Add("VB.CommandButton", "Command1")
With Command1
.Cancel = True
.Caption = "&Close"
.Move (ScaleWidth - .Width) * 0.5!, (ScaleHeight - .Height) * 0.5!
.Visible = True
End With
Set Timer1 = Controls.Add("VB.Timer", "Timer1")
Timer1.Interval = 1&
Timer1 = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set Timer1 = Nothing
Controls.Remove "Timer1"
Set Command1 = Nothing
Controls.Remove "Command1"
End Sub
Private Sub Timer1_Timer()
SetWindowPos hWnd, HWND_TOPMOST, 0&, 0&, 0&, 0&, SWP_NOSIZE
End Sub
Code:
void main()
{
HWND hWnds[3];
int i;
Sleep(10000); // <-- What for?
do // Get topmost window 3x; stop looping when all 3 are the same
{
for (i = 0; i < 3; i++)
{
// Get the topmost window's handle
hWnds[i] = GetTopWindow(GetDesktopWindow());
Sleep(333);
}
}
while (hWnds[0] != hWnds[1] || hWnds[1] != hWnds[2]);
// If topmost window isn't the Shell's desktop window
if (hWnds[0] != GetShellWindow())
{
// Then forcibly close the topmost window immediately
EndTask(hWnds[0], FALSE, TRUE);
MessageBox(NULL,
"Finally, the virus was closed...\n\n Welcome to the desktop!",
"Goodbye Virus!",
MB_OK | MB_ICONINFORMATION);
}
}
I've replaced the ForceCloseWindow procedure with the EndTask API function. The GetTopWindow function, on the other hand, "retrieves a handle to the child window at the top of the Z order".
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 8th, 2013, 02:42 PM
#6
Thread Starter
PowerPoster
Re: forcing a window\program to close
sorry i get these errors:
"--------------------Configuration: Bloquear Virus - Win32 Debug--------------------
Compiling...
Bloquear virus.cpp
c:\users\joaquim\documents\visual c 98\bloquear virus\bloquear virus.cpp(22) : error C2065: 'GetShellWindow' : undeclared identifier
c:\users\joaquim\documents\visual c 98\bloquear virus\bloquear virus.cpp(22) : error C2446: '!=' : no conversion from 'int' to 'struct HWND__ *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\users\joaquim\documents\visual c 98\bloquear virus\bloquear virus.cpp(22) : error C2040: '!=' : 'struct HWND__ *' differs in levels of indirection from 'int'
c:\users\joaquim\documents\visual c 98\bloquear virus\bloquear virus.cpp(25) : error C2065: 'EndTask' : undeclared identifier
Error executing cl.exe.
Bloquear Virus.exe - 4 error(s), 0 warning(s)"
i'm using the VC++ 6(98).
"why the "Sleep(10000); // <-- What for?"?"
it's only for give me time to open the 'virus'
after i create the program i will get it out
thanks for help me
Last edited by joaquim; Apr 8th, 2013 at 02:45 PM.
-
Apr 8th, 2013, 03:07 PM
#7
Re: forcing a window\program to close
GetShellWindow and EndTask are both Win API functions. Sorry, I don't know how to "declare" them in C++. You probably need to update your header files or something. Try downloading the Windows SDK from MSDN. Or, you could also try pasting those API's declaration from the given MSDN links to the appropriate header files. As for the "!=", I'm not sure but, try declaring the hWnds array as DWORD or UINT or even int instead of HWND. If it still doesn't work, then you could try casting the return value of GetTopWindow to int. Sorry, I'm still learning about C++...
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 8th, 2013, 03:15 PM
#8
Thread Starter
PowerPoster
Re: forcing a window\program to close
 Originally Posted by Bonnie West
GetShellWindow and EndTask are both Win API functions. Sorry, I don't know how to "declare" them in C++. You probably need to update your header files or something. Try downloading the Windows SDK from MSDN. Or, you could also try pasting those API's declaration from the given MSDN links to the appropriate header files. As for the "!=", I'm not sure but, try declaring the hWnds array as DWORD or UINT or even int instead of HWND. If it still doesn't work, then you could try casting the return value of GetTopWindow to int. Sorry, I'm still learning about C++...
sorry what compiler you use?
-
Apr 8th, 2013, 03:25 PM
#9
Re: forcing a window\program to close
Well, I don't have VC++ installed so I'm just using VB6.
Did you try copying the missing API's declarations from the given MSDN link and pasting them in the appropriate header file (both are supposed to be located in Winuser.h)? That's probably the easiest way to import those APIs.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 8th, 2013, 03:37 PM
#10
Thread Starter
PowerPoster
Re: forcing a window\program to close
 Originally Posted by Bonnie West
Well, I don't have VC++ installed so I'm just using VB6.
Did you try copying the missing API's declarations from the given MSDN link and pasting them in the appropriate header file (both are supposed to be located in Winuser.h)? That's probably the easiest way to import those APIs.
i'm using VC++ 2010 for test, but the EndTask() is give me problems
" 1 IntelliSense: identifier "EndTask" is undefined c:\users\joaquim\documents\visual studio 2010\projects\bloquear virus\bloquear virus\bloquear virus.cpp 26 9 Bloquear virus"
-
Apr 8th, 2013, 03:49 PM
#11
Thread Starter
PowerPoster
Re: forcing a window\program to close
-
Apr 8th, 2013, 04:14 PM
#12
Re: forcing a window\program to close
CloseWindow won't work, it just "minimizes (but does not destroy) the specified window". The API functions you've replaced are crucial to the proper operation of that code. Instead, copy these definitions to your Winuser.h header file (it's probably in a folder called "\Include"):
Code:
#if defined(WINNT)
WINUSERAPI
BOOL
WINAPI
EndTask(
__in HWND hWnd,
__in BOOL fShutDown,
__in BOOL fForce);
#endif
#if(WINVER >= 0x0400)
WINUSERAPI
HWND
WINAPI
GetShellWindow(
VOID);
#endif /* WINVER >= 0x0400 */
Of course, make sure they really aren't defined yet by searching for them first.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 8th, 2013, 04:50 PM
#13
Thread Starter
PowerPoster
Re: forcing a window\program to close
 Originally Posted by Bonnie West
CloseWindow won't work, it just "minimizes (but does not destroy) the specified window". The API functions you've replaced are crucial to the proper operation of that code. Instead, copy these definitions to your Winuser.h header file (it's probably in a folder called "\Include"):
Code:
#if defined(WINNT)
WINUSERAPI
BOOL
WINAPI
EndTask(
__in HWND hWnd,
__in BOOL fShutDown,
__in BOOL fForce);
#endif
#if(WINVER >= 0x0400)
WINUSERAPI
HWND
WINAPI
GetShellWindow(
VOID);
#endif /* WINVER >= 0x0400 */
Of course, make sure they really aren't defined yet by searching for them first.
the getshellwindow is now working but not endtask
Code:
/****************************************************************************
* *
* winuser.h -- USER procedure declarations, constant definitions and macros *
* *
* Copyright (c) 1985-1997, Microsoft Corp. All rights reserved. *
* *
****************************************************************************/
#ifndef _WINUSER_
#define _WINUSER_
//
// Define API decoration for direct importing of DLL references.
//
#if !defined(_USER32_)
#define WINUSERAPI DECLSPEC_IMPORT
#else
#define WINUSERAPI
#endif
#ifdef _MAC
#include <macwin32.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifndef WINVER
#define WINVER 0x0500 /* version 5.0 */
#endif /* !WINVER */
#if defined(WINNT)
WINUSERAPI
BOOL
WINAPI
EndTask(
__in HWND hWnd,
__in BOOL fShutDown,
__in BOOL fForce);
#endif
#if(WINVER >= 0x0400)
WINUSERAPI
HWND
WINAPI
GetShellWindow(
VOID);
#endif /* WINVER >= 0x0400 */
#include <stdarg.h>
#ifndef NOUSER
typedef HANDLE HDWP;
typedef VOID MENUTEMPLATEA;
typedef VOID MENUTEMPLATEW;
#ifdef UNICODE
typedef MENUTEMPLATEW MENUTEMPLATE;
#else
typedef MENUTEMPLATEA MENUTEMPLATE;
#endif // UNICODE
typedef PVOID LPMENUTEMPLATEA;
typedef PVOID LPMENUTEMPLATEW;
#ifdef UNICODE
typedef LPMENUTEMPLATEW LPMENUTEMPLATE;
#else
typedef LPMENUTEMPLATEA LPMENUTEMPLATE;
#endif // UNICODE
typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
#ifdef STRICT
typedef BOOL (CALLBACK* DLGPROC)(HWND, UINT, WPARAM, LPARAM);
typedef VOID (CALLBACK* TIMERPROC)(HWND, UINT, UINT, DWORD);
typedef BOOL (CALLBACK* GRAYSTRINGPROC)(HDC, LPARAM, int);
typedef BOOL (CALLBACK* WNDENUMPROC)(HWND, LPARAM);
typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
typedef VOID (CALLBACK* SENDASYNCPROC)(HWND, UINT, DWORD, LRESULT);
typedef BOOL (CALLBACK* PROPENUMPROCA)(HWND, LPCSTR, HANDLE);
typedef BOOL (CALLBACK* PROPENUMPROCW)(HWND, LPCWSTR, HANDLE);
............................................................................
what isn't right?
-
Apr 8th, 2013, 11:50 PM
#14
Re: forcing a window\program to close
Well, could it be because the compiler directive "WINNT" isn't defined? Although commenting out that compiler directive might "enable" EndTask, doing so could "break" things, so it might be best to just use the previous ForceCloseWindow routine instead. I only used EndTask because it reduces the ForceCloseWindow routine to a one-liner. Since EndTask isn't defined in your headers, you'll have to go back to ForceCloseWindow instead. BTW, in the ForceCloseWindow procedure, you can decrement the uTimeout parameter to 0 so that there'll be no more waiting.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|