Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

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

Writer Olivia Zamora

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++?

5

3 Answers

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.
}
8

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.