Take the 2-minute tour ×

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 :

  unsigned __int64 tmpres = 0;
  static int tzflag;

  if (NULL != tv)

    //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*/
    tv->tv_sec = (long)(tmpres / 1000000UL);
    tv->tv_usec = (long)(tmpres % 1000000UL);

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

  return 0;
share|improve this question
GMan, how did you get the code to look nice? I tried using the code tags but it didn't work –  Amir Afghani Jul 25 '09 at 2:18
You have to pre-pend each line with four spaces, not just the first one. –  GManNickG Jul 25 '09 at 2:34
I think you need to be more specific about what you mean by "compute the start time". Do you want to be able to print it out in human readable form, e.g. "July 24, 2009, 8:14PM"? Or do you want to use it in a calculation, e.g. to measure the elapsed time since the last time your program was run? Or something else? The answer depends on what you want to do with the time value. –  Jeremy Friesner Jul 25 '09 at 3:02
add comment

2 Answers

up vote 5 down vote accepted

If I understand you right you want to know what time your process started, correct? So you'll want to look into GetProcessTimes (http://msdn.microsoft.com/en-us/library/ms683223(VS.85).aspx).

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.

share|improve this answer
This is the right answer. –  Foredecker Jul 25 '09 at 4:32
add comment

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

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.
    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)
share|improve this answer
add comment

Your Answer


