Jump to content

DllCall OpenThread SuspendThread ResumeThread issue


tricky808
 Share

Recommended Posts

I have this code I'm using to try and suspend or resume a thread and I can't get it to work...

For my function params, I'm calling callThreads(3436, "suspend").

In this case 3436 is the decimal equivalent of the threadID I want to suspend (an AutoIt process).

I am using Number() as my conversion function from a string value that is '3436'.

In other words, my function is being called like this: callThreads(Number($szThreadID), "suspend"))...

Func callThreads($iThread, $szAction); gets a thread handle and performs a suspend or resume function on it
  ; setup constants and variables
   Const $THREAD_SUSPEND_RESUME = 0x0002
   Local $hThread, $iThreadAction
   
  ; get the thread handle
   $hThread = DllCall("kernel32.dll", "hwnd", "OpenThread", "int", $THREAD_SUSPEND_RESUME, "int", 0, "int", $iThread)

  ; return an error if there is a problem getting the thread
   If $hThread = 0 Then Return ("" & SetError(1))

  ; perform a suspend or resume action on the thread
   If StringLower($szAction) = "suspend" Then
      $iThreadAction = DllCall("kernel32.dll", "int", "SuspendThread", "hwnd", $hThread)
      If @error Then MsgBox(0,"suspendthread error")
   ElseIf StringLower($szAction) = "resume" Then
      $iThreadAction = DllCall("kernel32.dll", "int", "ResumeThread", "hwnd", $hThread)
      If @error Then MsgBox(0,"resumethread error")
   EndIf

  ; close the thread handle
   $hThread = 0

  ; throw an error if there is a problem
   If $iThreadAction = -1 Then Return ("" & SetError(1))
EndFunc

No errors are being returned, but my thread is not suspending. I'm thinking maybe it's because I'm not converting the threadID to a dword value. Can someone tell me where I'm going wrong with this?

Aut viam inveniam aut faciam

Link to comment
Share on other sites

Re-read the documentation for DllCall() again. Pay particular attention to the return value section. Also re-read MSDN. I don't imagine just writing "$hThread = 0" is properly closing the thread. I'm sure OpenThread() mentions something about CloseHandle() needing to be called. Lastly, you might want to read how operator = works. The calls to StringLower() are not necessary.

Those are the 3 significant problems I see off-hand.

Link to comment
Share on other sites

Oops, yeah forgot it returned an array...

Good call on the CloseHandle, you preempted any future issues I would've had after fixing the handle/array/return value deal...

and yeah, the stringlower thing is something I do to prevent me sabotaging myself when I call the function later in my code. I guess I need to quit enabling myself and pay attention to my case when I type.

Thanks for the fishing lessons!

Aut viam inveniam aut faciam

Link to comment
Share on other sites

Oops, yeah forgot it returned an array...

Good call on the CloseHandle, you preempted any future issues I would've had after fixing the handle/array/return value deal...

and yeah, the stringlower thing is something I do to prevent me sabotaging myself when I call the function later in my code. I guess I need to quit enabling myself and pay attention to my case when I type.

Thanks for the fishing lessons!

You got 2 out of 3 right.

Run the following code to understand the 3rd.

If "ENABLE" = "enable" Then MsgBox(4096, "", "I bet operator = is case insensitive.")
If "ENABLE" == "enable" Then 
    MsgBox(4096, "", "If this is visible, AutoIt is broke.")
Else
    MsgBox(4096, "", "It looks lile operator == is case sensitive.")
EndIf
Link to comment
Share on other sites

You got 2 out of 3 right.

Run the following code to understand the 3rd.

If "ENABLE" = "enable" Then MsgBox(4096, "", "I bet operator = is case insensitive.")
If "ENABLE" == "enable" Then 
    MsgBox(4096, "", "If this is visible, AutoIt is broke.")
Else
    MsgBox(4096, "", "It looks lile operator == is case sensitive.")
EndIf
Understood. Thank you for explaining that nuance...

Aut viam inveniam aut faciam

Link to comment
Share on other sites

  • 15 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...