代码之家  ›  专栏  ›  技术社区  ›  Amir Afghani

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

  •  3
  • Amir Afghani  · 技术社区  · 17 年前

    我正在使用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 回复  |  直到 17 年前
        1
  •  8
  •   Ajay    7 年前

    GetProcessTimes

    如果您感兴趣的流程是当前流程,则可以使用 GetCurrentProcess() 要获取您需要调用的进程句柄 GetProcessTimes()

        2
  •  3
  •   Alexey1993    13 年前

    我的问题结束了。我正在从process shapshot中找到获取ProcessTime的示例,一些人给出了与此问题的链接。

    我发布了以下内容,这是我的示例:

    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)