简体   繁体   中英

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

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

getpid() && gethostname() -使用man来了解所有关于它们的信息...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM