ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 윈도우 사용시간 확인 : GetTickCount, GetTickCount64
    Programming/WINAPI 2010. 12. 9. 17:45
    윈도우가 시작(구동)되고 나서부터의 경과시간을 확인하는 방법입니다.
    GetTickCount 또는 GetTickCount64함수를 이용하여 구동시간을 확인할 수 있습니다.
    GetTickCount함수의 경우 구동시간이 49.7일이 지난 경우 값이 0으로 초기화됩니다. 물론 개인 컴퓨터를 이렇게 오랬동안 켜둘 일은 없겠지만요..
    아무튼 세상에 무슨일이 일어날지 모르기 때문에 가능한한 GetTickCount64함수를 사용하도록 설정을 하였습니다.

    GetTickCount64 함수의 경우 Vista부터 지원되는 함수라 그 이전 버전의 윈도우인 경우 GetTickCount함수를 사용하도록 설정된 소스입니다.

    #include <Windows.h>
    #include <stdio.h>
    #include <conio.h>
    
    typedef ULONGLONG (*fnGetTickCount64)(void);
    
    ULONGLONG GetWindowsUptime()
    {
    	/*
    	윈도우 가동시간확인시 GetTickCount 또는 GetTickCount64 함수를 통해 시간을 확인할 수 있음.
    	GetTickCount함수의 경우 49.7일 경과된 경우 0으로 값이 초기화되기 때문에
    	GetTickCount64함수를 사용하는 것이 좋음.
    	그러나 GetTickCount64의 경우 Windows Vista버전부터 지원되기 때문에
    	호환성을 고려하여 명시적으로 GetTickCount64함수를 호출한다.
    	해당 함수호출에 실패하는 경우(Vista이전 버전인 경우) GetTickCount함수를 호출한다.
    	*/
    	ULONGLONG tickCnt=0;
    	HMODULE hDll=NULL;
    	hDll = LoadLibrary(L"KERNEL32.DLL");
    	if(hDll)
    	{
    		fnGetTickCount64 func=NULL;
    		func = (fnGetTickCount64)GetProcAddress(hDll, "GetTickCount64");
    		if(func)	tickCnt = func();
    		else		tickCnt = (ULONGLONG)GetTickCount();
    		FreeLibrary(hDll);
    	}
    	return tickCnt;
    }
    
    int main(int argc, char* argv[])
    {
    	/*
    	윈도우 구동시간 확인
    	tickCnt의 단위는 1000/1초 (millisecond)
    	*/
    	ULONGLONG tickCnt = GetWindowsUptime();
    	int msec= tickCnt%1000;
    	int sec	= tickCnt/1000;
    	int hour= sec/3600;
    	int min	= (sec%3600)/60;
    	sec		= sec%60;
    
    	
    	printf("윈도우 사용시간 : %02d시간 %02d분 %02d초\n", 
    		hour, min, sec);
    
    	getch();
    	return 0;
    }
    


    'Programming > WINAPI' 카테고리의 다른 글

    GetGUIThreadInfo  (0) 2017.03.01
    RegCreateKeyEx 사용방법  (0) 2010.01.06
    [펌] InternetSetOption - timeout 설정 버그 해결방법  (0) 2009.01.05
Designed by Tistory.