olidev
olidev

Reputation: 20674

how to get current process ID and machine name in C++

In C#, it is straightforward to get the current process ID and machine name:

int processID = Process.GetCurrentProcess().Id;
string machineName = Environment.MachineName;

How can I retrieve them in native C++?

Upvotes: 7

Views: 28761

Answers (3)

user7223715
user7223715

Reputation: 31

#ifdef _WIN32
 return GetCurrentProcessId(); 
#else
 return ::getpid();
#endif

Upvotes: 3

hmjd
hmjd

Reputation: 122001

As you commented the platform is Windows 7, the WINAPI provides GetCurrentProcessId() and GetComputerName().

Simple example for GetComputerName():

const int BUF_SIZE = MAX_COMPUTERNAME_LENGTH + 1;
char buf[BUF_SIZE] = "";
DWORD size = BUF_SIZE;

if (GetComputerNameA(buf, &size)) // Explicitly calling ANSI version.
{
    std::string computer_name(buf, size);
}
else
{
    // Handle failure.
}

Upvotes: 8

Nim
Nim

Reputation: 33655

getpid() && gethostname() - use man to learn all about them...

Upvotes: 6

Related Questions