-2

Using C++ Boost I am creating an Command Line Application. For Some Reason I need the PID of the command prompt which launches my executable. The executable is written in C++ along with boost and built in eclipse IDE

CocoCrisp
  • 807
  • 1
  • 9
  • 26
  • 1
    Is the question, how the code can obtain the PID of the process it's executing in? Or is it about some other program obtaining the PID of a process it launches? Or is it about some other program obtaining the PID of an instance of program Xyz launched independently? Please be a little more clear about the scenario here. – Cheers and hth. - Alf Oct 10 '16 at 12:43
  • 1
    A "prompt" isn't a process, so it doesn't have a PID. What *process* are you trying to identify? – Harry Johnston Oct 10 '16 at 23:53
  • I am opening a command prompt and then executing the exe file by going to that path. So in that case it isn't a prompt but the cmd.exe process that runs on the cmd that is opened and is showing in taskmanager @HarryJohnston – CocoCrisp Oct 12 '16 at 05:57
  • Correct @Cheersandhth.-Alf I couldn't ask the question correctly but you got me right. – CocoCrisp Oct 12 '16 at 05:58
  • There is a program executable which gets launched from the command prompt so I need the PID of that command prompt from which it is launched. @Cheersandhth.-Alf – CocoCrisp Oct 12 '16 at 05:59
  • Technically I think you can look for the set of owners of the console window. Also there is undocumented process parent tree functionality somewhere, I think used by SysInternals' process viewer. But this seems to be clear case of X/Y problem: you have a problem X, you have thought of an impractical solution Y, and you're asking about Y without mentioning X. – Cheers and hth. - Alf Oct 12 '16 at 06:14
  • Okay so I want to store values in environment variables and for that i need a unique string. What my approach was to store the username along with the PID so that even if the user uses multiple command prompt and launches my application still my program can create a different session all together for him. – CocoCrisp Oct 12 '16 at 06:18
  • @Milind: Why stroring values in environment variables? To access them later from your program? Why not passing them directly as arguments to the program? – jpo38 Oct 12 '16 at 06:28
  • I am keeping the track of session using those environment variables @jpo38 – CocoCrisp Oct 12 '16 at 08:57
  • I'm not sure I understand what you're trying to do or how you're expecting it to work. You do realize that any environment variables your program sets affect only your program and its children? The parent won't see them. (If you set them in the registry they will affect future command windows, but not any existing ones.) – Harry Johnston Oct 12 '16 at 20:47
  • Yes Exactly, This is my requirement @HarryJohnston – CocoCrisp Oct 13 '16 at 05:46
  • @Milind: Why not passing parnet PID to child process through an environment variable then? – jpo38 Oct 13 '16 at 06:33
  • Or why not use your own process ID rather than the process ID of the parent? – Harry Johnston Oct 13 '16 at 20:16

1 Answers1

1

No way your application can know the PID of the process who created it.

If you are looking for the PID of the current process, look at ms c++ get pid of current process (as you are apparently running on Windows).

If you are looking for the PID of the parent process, the only solution is to have the parent process pass it to the child process as argument:

  • From Command Line Application, check this post and extract your PID (cmd.exe's PID)
  • Pass it to your program: > myprogram.exe %PID%
  • From your program's main function, get parent's PID passed using argv[1].

Alternatively, you could try this: while your program runs, cmd.exe window title is changed and lets appear the program name in it (the command running). So if you do something equivalent to tasklist /v /fo csv | findstr /i "myprogram" in your code, the output will show you the PID of the running cmd.exe process that launched it.

>tasklist /v /fo csv | findstr /i "myprogram"
"cmd.exe","44372","Console","1","4 900 Ko","Running","DOMAIN\USER","0:00:00","cmd.exe - myprogram"

Note that if many programs were started, you won't be able to know who is who. But if only one works, this should do the trick.

jpo38
  • 20,821
  • 10
  • 70
  • 151