Jump to content

Pid from Handle...


Recommended Posts

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.

Link to comment
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.
Link to comment
Share on other sites

This post is obsolete.

Please use new _SingleScript at http://www.autoitscript.com/forum/index.php?showtopic=178681

------------------------------------

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
Edited by Exit
point to new #include location

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

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

Link to comment
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]

Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

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