Sign in to follow this  
Followers 0
Negative1

Pid from Handle...

10 posts in this topic

So I have searched help and I found plenty of topics on how to get handle from PID!!! success I thought... but no wait i need PID from handle, blast!

I used _singleton to get the handle of the existing process. Now i want to kill the existing process for sake of the new process to run.

Thanks in advance

-1


What are we going to do tonight Brain?Same thing we do every night Pinky try to automate the world.

Share this post


Link to post
Share on other sites



So I have searched help and I found plenty of topics on how to get handle from PID!!! success I thought... but no wait i need PID from handle, blast!

I used _singleton to get the handle of the existing process. Now i want to kill the existing process for sake of the new process to run.

Thanks in advance

-1

If you can get the handle from a PID then the reverse must be just a question of looking through the pids.

ProcessList to get the list of processes

for each process

if handleof(process) = handleIwant then ProcessIwant = process.

(Excuse my lack of language skills :D )


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

If you just want to kill the previous script, use this:

;---  _SingleScript.au3
;---  remove all running scripts wirh the same name
;---  Usage: #include "_SingleScript.au3"
;---------------------------------------------------------------------
__SingleScript()
Func __SingleScript()
    Local $O = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    Local $OI, $CI = $O.ExecQuery("SELECT * FROM Win32_Process", "WQL", 0x30)
    For $OI In $CI
        If $OI.ProcessId = @AutoItPID Then ContinueLoop
        If $OI.Name = StringTrimRight(@ScriptName, 4) & ".EXE" Then ProcessClose($OI.ProcessId)
        If $OI.Name = "AutoIt3.exe" And StringInStr($OI.CommandLine, StringTrimRight(@ScriptName, 4) & ".au3") Then ProcessClose($OI.ProcessId)
    Next
EndFunc   ;==>__SingleScript

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Why do you use WMI for this simple task? ProcessList does the same much faster :D

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Why do you use WMI for this simple task? ProcessList does the same much faster :D

ProcessList has no information of "AutoIt3.exe" command parameter.

This is necessary for noncompiled scripts See this lime:

If $OI.Name = "AutoIt3.exe" And StringInStr($OI.CommandLine, StringTrimRight(@ScriptName, 4) & ".au3") Then ProcessClose($OI.ProcessId)

Btw, it takes 250 milliseconds to execute. Not much for a function running only once per script. :D

Edited by forumer100

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Oh, i missed that line :D

I have another Method:

$UniqueIsntanceKey = "My Unique String, e.g a GUID 31eafd0c-5d98-442f-ab88-639512c0c128"

While WinClose($UniqueIsntanceKey) ; closes all windows with this title (you could also use WinKill)
WEnd
AutoItWinSetTitle($UniqueIsntanceKey) ; set title for own script

This still works if multiple Scripts have the same name or if a script is executed twice with different names.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

PID from windows handle? Just use this, it's foolproof -

GetWindowThreadProcessId()

Share this post


Link to post
Share on other sites

Foolproof? Really?

The OP wants to get the PID of a process by using that process' handle. Can you explain how _WinAPI_GetWindowThreadProcessId() is going to do that?

_WinAPI_GetWindowThreadProcessId($hWnd, ByRef $iPID)

You emphasized the wrong word there. ByRef means the $iPID parameter is passed by reference and the function is able to update the value directly.

Here's how:

#Include <WinAPI.au3>

Run("Notepad.exe")
WinWait("[CLASS:Notepad]")
Local $hWnd =WinGetHandle("[CLASS:Notepad]")
Local $iPID
_WinAPI_GetWindowThreadProcessId($hWnd,  $iPID)
ConsoleWrite("PID set by function: " & $iPID & @CRLF)

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

Yep. I was thinking of the direct DllCall (didn't know it was in WinAPI.au3), but same result.

@Thanubis

Also, don't confuse the term 'handle' with 'hWnd'. 'handle' usually refers to a process handle returned by OpenProcess, for example. 'hWnd' usually refers to a process' window's handle. Close, but not quite interchangeable. If you want more info, search that function on MSDN.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0