discodowney
discodowney

Reputation: 1507

Finding where a process was run from

So i have an issue that i have an application that gets started. Then through a test i need to turn it off and start it again. But it needs t obe done without hard coding.

So is there a way of finding where a process was run from? I can find a list of all processes running but dont know if this is even possible.

EDIT: Its on a windows 7 OS.

Upvotes: 0

Views: 180

Answers (3)

user1233963
user1233963

Reputation: 1490

Easy and portable way would be using argv[0]. It returns the full .exe file path which is all you need

Upvotes: 1

paulrehkugler
paulrehkugler

Reputation: 3271

First, what do you mean by "find where the process is run from"? I'm assuming you mean the parent's process id, but it could mean current working directory, ip of remote call, etc...

To find the parent's process id, look into getppid().

Edit: this assumes that you (like any sane programmer) are using a unix-like machine.

Edit #2: You're on Windows, so I have no idea.

Edit #3: Since you're looking for the path to the program you are executing, use argv[0]. The first command line arg to int main(int argc, char* argv[]) is always the path to the binary.

Upvotes: 0

hmjd
hmjd

Reputation: 122001

QueryFullProcessImageName() will provide the path to the executable image for a process:

#include <windows.h>
#include <iostream>

int main()
{
    char exe_path[MAX_PATH];
    DWORD exe_path_size = MAX_PATH;
    if (QueryFullProcessImageName(GetCurrentProcess(),
                                  0,
                                  exe_path,
                                  &exe_path_size))
    {
        std::cout << exe_path << "\n";
    }

    return 0;
}

Upvotes: 1

Related Questions