Windows获取开机时间
使用GetTickCount()函数即可,注意单位是毫秒。DWORD可保存。
// uptime_c.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "uptime_c.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
CString formatTime(DWORD timeStamp)
{
DWORD minute = timeStamp/60;
DWORD second = timeStamp - minute*60 ;
DWORD hour = minute / 60 ;
minute = minute - hour * 60;
CString csTime ;
csTime.Format(_T("%u:%02u:%02u"),hour,minute,second);
return csTime;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败
"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
if (argc!=1)
{//有任意参数,则输出时间戳。
_tprintf(_T("%u"), GetTickCount()/1000);
}
else
{
CString csStartToNow(formatTime( GetTickCount()/1000));
_tprintf(_T("%s"),csStartToNow);
}
}
}
else
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: GetModuleHandle 失败
"));
nRetCode = 1;
}
return nRetCode;
}
| 0个评论