Results 1 to 8 of 8

Thread: Terminating a thread

  1. #1

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    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:
    1. Option Explicit
    2. 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
    3.  
    4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    6. Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
    7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    8. Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
    9. Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long)
    10.  
    11.  
    12. Private Sub Command1_Click()
    13. 'display asynch message box
    14.     MessageBoxEx 0&, "Pulchritude" & vbNewLine & "Ansync Messagebox", "Discombobulated", 0&, 0
    15. End Sub
    16.  
    17.  
    18. Private Sub Command2_Click()
    19. 'attempt to close asynch message box
    20. Dim hwnd                As Long
    21. Dim Pid                 As Long
    22. Dim TID                 As Long
    23. Dim lpExitCode          As Long
    24. Dim hThread             As Long
    25.  
    26. hwnd = FindWindow("#32770", "Discombobulated")
    27.  
    28. If hwnd Then
    29.    
    30.     TID = GetWindowThreadProcessId(hwnd, Pid)
    31.     Call GetExitCodeThread(TID, lpExitCode)
    32.     'fine to here but now what?
    33.  
    34. End If
    35. End Sub
    Last edited by Nucleus; Oct 24th, 2001 at 01:06 AM.

  2. #2

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Here is my attempt:

    VB Code:
    1. 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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  4. #4

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Whah

    Damn it, and I really need to be able to terminate an existing thread.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    EndDialog is the business. Does it clean up system resources?

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Ok thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width