以编程方式计算Windows上进程的开始时间

时间:2009-07-25 02:11:35

标签: c++ windows process time

我正在使用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;
}

2 个答案:

答案 0 :(得分:7)

如果我理解你,你想知道你的过程开始的时间,对吗?因此,您需要查看GetProcessTimes

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

答案 1 :(得分:3)

我的问题已经结束。我从流程快照中找到了GetProcessTimes的示例,有些人提供了这个问题的链接。

我发布了这个,这是我的榜样:

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)