繁体   English   中英

如何在C ++中获取当前的进程ID和计算机名称

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

在C#中,很容易获得当前进程ID和计算机名称:

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

如何在本机C ++中检索它们?

正如您所评论的,该平台是Windows 7,WINAPI提供了GetCurrentProcessId()GetComputerName()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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