[Windows 驱动开发] 获取时间

开机时间

void MyGetTickCount(PULONG  msec) //进行传出
{
    LARGE_INTEGER la;
    ULONG MyInc;
    MyInc = KeQueryTimeIncrement(); //返回滴答数

    //下方 KeQueryTickCount 的宏的原型.

    KeQueryTickCount(&la);
    
    la.QuadPart *= MyInc;
    la.QuadPart /= 10000;
    
    *msec = la.LowPart;

}

当前时间

PTCHAR GetTimeYMS()
{
    //获取年月日.
    LARGE_INTEGER SystemTime;
    LARGE_INTEGER LocalTime;
    TIME_FIELDS TimeFiled;
    TCHAR *time_str = ExAllocatePoolWithTag(PagedPool, 32, 0);

    KeQuerySystemTime(&SystemTime);
    ExSystemTimeToLocalTime(&SystemTime,&LocalTime);
    RtlTimeToTimeFields(&LocalTime,&TimeFiled);
#ifdef UNICODE
#define RtlStringCchPrintf RtlStringCchPrintfW
#else
#define RtlStringCchPrintf RtlStringCchPrintfA
#endif // UNICODE

    RtlStringCchPrintf(
        time_str,
        32,
        TEXT("%4d-%2d-%2d %2d-%2d-%2d"),
        TimeFiled.Year,
        TimeFiled.Month,
        TimeFiled.Day,              //年月日时分秒
        TimeFiled.Hour,
        TimeFiled.Minute,
        TimeFiled.Second);
    return time_str;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
{
    PTCHAR pTime = NULL;
    pDriverObj->DriverUnload = DriverUnLoad;
    pTime = GetTimeYMS();

    DbgPrint("%Ls \r\n", pTime);
    return STATUS_SUCCESS;
}
原文地址:https://www.cnblogs.com/csnd/p/15613332.html