Jump to content

How to have a script obtain it's own PID?


Recommended Posts

  • Developers

For the purposes of my AutoIt killer script, I need to know how to find the PID of the script itself so that it may refrain from killing itself until the job is done.

<{POST_SNAPBACK}>

:) I am wondering what the reason for the sudden explosion of post in the "Scripts and Scrapts" forum for regular support is.....

Must be 'Script and Scraps" day ....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Here is something interesting.  I shall read more into it.

<{POST_SNAPBACK}>

This should do it:

$pid = DllCall("kernel32.dll","int","GetCurrentProcessId")

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Thank you JdeB!  I tried:

$pid = DllCall ( "kernel32.dll", "Int", "GetCurrentProcess", "ptr", "")

but it failed probably because I tried passing a parameter.  I thought I had to because the DLLCall documentation doesn't list parameters as optional.

<{POST_SNAPBACK}>

It is optional .....

i guess the helpfile should state:

DllCall ( "dll", "return type", "function"[, "type", param ])

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 4 years later...

Sry, for this really late thread push ... but it's the only (one line) hint for realise "getting a program's own PID"

The code looks really good... but when I've the mini prog

$pid = DllCall("kernel32.dll","int","GetCurrentProcessId")
sleep(1000)
msgbox(0, "my_id", $pid)

the result is an empty msgbox :D

any idea?

Link to comment
Share on other sites

Wow, that was easy ... and I didn't find it on my own :o

However... thanks a lot! :D

Well to your defence it would have been the twelfth result if you search the helpfile for "pid" :D

BTW if you still want to know why your call to "GetCurrentProcessId" didn't work, then read the "Remarks"-section in the helpfile about DllCall or run this code, I think you will get it.

$pid = DllCall("kernel32.dll","int","GetCurrentProcessId")
sleep(1000)
MsgBox(0, "my_id", "PID from GetCurrentProcessId: " & $pid[0] & @CRLF  & "PID from @AutoItPID macro: " & @AutoItPID)
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...