以编程方式计算 Windows 上进程的启动时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:51:59  来源:igfitidea点击:

Programmatically compute the start time of a process on Windows

c++windowsprocesstime

提问by Amir Afghani

I'm writing c/c++ code on Windows using Visual Studio. I want to know how to calculate the start time of my process effectively. Can I just use gettimeofday()? I've found the following code from google but I don't understand what it's doing really :

我正在使用 Visual Studio 在 Windows 上编写 c/c++ 代码。我想知道如何有效地计算我的流程的开始时间。我可以只使用 gettimeofday() 吗?我从谷歌找到了以下代码,但我不明白它在做什么:

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
  FILETIME ft;
  unsigned __int64 tmpres = 0;
  static int tzflag;

  if (NULL != tv)
  {
    GetSystemTimeAsFileTime(&ft);

    //I'm lost at this point
    tmpres |= ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    /*converting file time to unix epoch*/
    tmpres /= 10;  /*convert into microseconds*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
    tv->tv_sec = (long)(tmpres / 1000000UL);
    tv->tv_usec = (long)(tmpres % 1000000UL);
  }

  if (NULL != tz)
  {
    if (!tzflag)
    {
      _tzset();
      tzflag++;
    }
    tz->tz_minuteswest = _timezone / 60;
    tz->tz_dsttime = _daylight;
  }

  return 0;
}

回答by Curt Nichols

If I understand you right you want to know what time your process started, correct? So you'll want to look into GetProcessTimes

如果我理解正确,您想知道您的流程是什么时候开始的,对吗?所以你会想看看GetProcessTimes

If the process you're interested in is the current process, you can use GetCurrentProcess()to get the process handle that you'll need to call GetProcessTimes()this returns a pseudo-handle that you don't need to close.

如果您感兴趣的进程是当前进程,您可以使用它GetCurrentProcess()来获取您需要调用的进程句柄,GetProcessTimes()这将返回一个不需要关闭的伪句柄。

回答by Alexey1993

My question is closed. I'm find example to GetProcessTime from process shapshot and some guys give link with this question.

我的问题已经结束。我从 process shapshot 找到了 GetProcessTime 的例子,有些人给出了这个问题的链接。

I'm released this and that is my example:

我发布了这个,那是我的例子:

HANDLE hSnapshot; //variable for save snapshot of process
PROCESSENTRY32 Entry; //variable for processing with snapshot
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //do snapshot
Entry.dwSize = sizeof(Entry);    //assign size of Entry variables
Process32First(hSnapshot, &Entry); //assign Entry variable to start of snapshot
HANDLE hProc; //this variable for handle process
SYSTEMTIME sProcessTime; // this variable for get system (usefull) time
FILETIME fProcessTime, ftExit, ftKernel, ftUser; // this variables for get process start time and etc.
do 
{
    hProc = OpenProcess( PROCESS_ALL_ACCESS, FALSE, Entry.th32ProcessID);//Open process for all access
    GetProcessTimes(hProc, &fProcessTime, &ftExit, &ftKernel, &ftUser);//Get process time
    FileTimeToSystemTime(&fProcessTime, &sProcessTime); //and now, start time of process at sProcessTime variable at usefull format.
} while (Process32Next(hSnapshot, &Entry)); //while not end of list(snapshot)