2009-07-25 33 views
3

我正在使用Visual Studio在Windows上編寫c/C++代碼。我想知道如何有效地計算我的流程的開始時間。我可以使用gettimeofday()嗎?我發現從谷歌下面的代碼,但我不明白它在做什麼真:以編程方式計算Windows上的進程的開始時間

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; 
} 
+0

GMAN,你怎麼讓代碼好看嗎?我試過使用代碼標籤,但它不起作用 – 2009-07-25 02:18:25

+0

你必須預先用四個空格預置每行,而不僅僅是第一行。 – GManNickG 2009-07-25 02:34:01

回答

7

如果我理解你的權利,你想知道你的進程啓動是什麼時候,是否正確?所以你會想看看GetProcessTimes(http://msdn.microsoft.com/en-us/library/ms683223(VS.85).aspx)。如果您感興趣的進程是當前進程,那麼可以使用GetCurrentProcess()來獲取調用GetProcessTimes()所需的進程句柄。這會返回一個你不需要關閉的僞句柄。

3

我的問題已關閉。我從進程shapshot找到GetProcessTime的例子,有些人給出了這個問題的鏈接。

我發佈這個,這是我的例子:

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) 
相關問題