|
-
Oct 23rd, 2001, 11:16 PM
#1
Thread Starter
Registered User
Terminating a thread
I have used the messagebox api function to create an async message box. Now I want to terminate the thread containing the messagebox without terminating my applicaiton as well.
I can get the handle of the thread using GetWindowThreadProcessId().
Now to try to terminate the thread. I presume I need to use OpenThread API, before using TerminateThread. Problem is, I can't find a VB declare statement for this API.
Here is the link to the MSDN function definition:
http://msdn.microsoft.com/library/de...thred_8xb8.asp
Any help with a translation would be appreciated 
Here is what I am trying to do:
VB Code:
Option Explicit
Private Declare Function MessageBoxEx Lib "user32" Alias "MessageBoxExA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal uType As Long, ByVal wLanguageId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long)
Private Sub Command1_Click()
'display asynch message box
MessageBoxEx 0&, "Pulchritude" & vbNewLine & "Ansync Messagebox", "Discombobulated", 0&, 0
End Sub
Private Sub Command2_Click()
'attempt to close asynch message box
Dim hwnd As Long
Dim Pid As Long
Dim TID As Long
Dim lpExitCode As Long
Dim hThread As Long
hwnd = FindWindow("#32770", "Discombobulated")
If hwnd Then
TID = GetWindowThreadProcessId(hwnd, Pid)
Call GetExitCodeThread(TID, lpExitCode)
'fine to here but now what?
End If
End Sub
Last edited by Nucleus; Oct 24th, 2001 at 01:06 AM.
-
Oct 23rd, 2001, 11:51 PM
#2
Thread Starter
Registered User
Here is my attempt:
VB Code:
Private Declare Function OpenThread Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwThreadId As Long) As Long
However I get an error "no insertion point OpenThread in Kernel32"?
Also I can't find out the values for the constants but I assume if I pass 1, I get complete access to the thread.
-
Oct 24th, 2001, 11:56 AM
#3
Platform SDK Release: August 2001
Maybe this is WinXP only?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 24th, 2001, 06:00 PM
#4
Thread Starter
Registered User
Whah 
Damn it, and I really need to be able to terminate an existing thread.
-
Oct 25th, 2001, 08:54 AM
#5
You could write the handle of the thread to a secret file and later load it, so you can call TerminateThread directly on the handle. BTW, ExitThread and TerminateThread are bad functions because they imediatly kill the thread without giving it a chance to clean up resources they used. If there is any other way (like setting a global variable), you should use it.
Or maybe finding the window of the messagebox and calling EndDialog on it. Or a WM_COMMAND message with the ID IDOK. There are many ways, most of them better than calling TerminateThread.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 25th, 2001, 06:29 PM
#6
Thread Starter
Registered User
EndDialog is the business. Does it clean up system resources?
-
Oct 26th, 2001, 08:47 AM
#7
I'm not sure. Also, I just found this in MSDN:
An application calls EndDialog from within the dialog box procedure; the function must not be used for any other purpose.
So I'm not sure if it's ok to call this function. I think
SendMessage(hDlg, WM_COMMAND, MAKELONG(IDOK, BN_CLICKED), GetDlgItem(hDlg, IDOK));
is still the cleanest solution. But I don't know how to emulate or access MAKELONG in VB. It basically combines two 16-bit values (integers in VB) to one 32-bit (long in VB).
Here is the C definition of MAKELONG, maybe you know how to convert it:
Code:
#define MAKELONG(a, b) ((LONG)(((WORD)(a)) | ((DWORD)((WORD)(b))) << 16))
long is 32 bit, word is 16 bit and dword is 32 bit.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 27th, 2001, 05:03 PM
#8
Thread Starter
Registered User
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
|