`
java-mans
  • 浏览: 11453422 次
文章分类
社区版块
存档分类
最新评论

《WindowsAPI巡礼》---GetSystemTime和GetFileTime

 
阅读更多

FILETIME结构包含了文件或目录的日期和时间信息:(自160111日以来,单位为100纳秒)

typedef struct _FILETIME {

DWORD dwLowDateTime; //32

DWORD dwHighDateTime; //32

} FILETIME, *PFILETIME;

SYSTEMTIME结构包含了用户可识别的系统日期信息:

typedef struct _SYSTEMTIME {

WORD wYear;

WORD wMonth;

WORD wDayOfWeek;

WORD wDay;

WORD wHour;

WORD wMinute;

WORD wSecond;

WORD wMilliseconds;

} SYSTEMTIME, *PSYSTEMTIME;

=======================================================

函数FileTimeToSystemTime用来将文件时间格式转换为系统时间格式:

BOOL WINAPI FileTimeToSystemTime(

__in const FILETIME *lpFileTime, //文件时间

__out LPSYSTEMTIME lpSystemTime //系统时间

);

函数SystemTimeToFileTime则是将系统时间转换成文件时间格式:

BOOL WINAPI SystemTimeToFileTime(

__in const SYSTEMTIME *lpSystemTime,

__out LPFILETIME lpFileTime

);

将文件时间转换为系统时间的实例如下:

#include <windows.h>

// SetFileToCurrentTime - sets last write time to current system time

// Return value - TRUE if successful, FALSE otherwise

// hFile - must be a valid file handle

BOOL SetFileToCurrentTime(HANDLE hFile)

{

FILETIME ft;

SYSTEMTIME st;

BOOL f;

GetSystemTime(&st); // Gets the current system time

SystemTimeToFileTime(&st, &ft); // Converts the current system time to file time format

f = SetFileTime(hFile, // Sets last-write time of the file

(LPFILETIME) NULL, // to the converted current system time

(LPFILETIME) NULL,

&ft);

return f;

}

=======================================================

GetSystemTime函数用来获得系统时间:

void WINAPI GetSystemTime(

__out LPSYSTEMTIME lpSystemTime

);

GetFileTime函数用来获得一个文件或目录的创建的时间、最后访问的时间以及最后修改的时间:

BOOL WINAPI GetFileTime(

__in HANDLE hFile, //文件或目录句柄

__out_opt LPFILETIME lpCreationTime, //返回的创建的日期和时间信息

__out_opt LPFILETIME lpLastAccessTime, //返回的最后访问的日期和时间信息

__out_opt LPFILETIME lpLastWriteTime //返回的最后修改的日期和时间信息

);

返回值:成功时,返回非零值;失败时,返回零值。

下面的代码使用GetFileTime函数来获得文件的最后写入时间:

#include <windows.h>

#include <tchar.h>

#include <strsafe.h>

// GetLastWriteTime - Retrieves the last-write time and converts

// the time to a string

//

// Return value - TRUE if successful, FALSE otherwise

// hFile - Valid file handle

// lpszString - Pointer to buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString, DWORD dwSize)

{

FILETIME ftCreate, ftAccess, ftWrite;

SYSTEMTIME stUTC, stLocal;

DWORD dwRet;

// Retrieve the file times for the file.

if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))

return FALSE;

// Convert the last-write time to local time.

FileTimeToSystemTime(&ftWrite, &stUTC);

SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

// Build a string showing the date and time.

dwRet = StringCchPrintf(lpszString, dwSize,

TEXT("%02d/%02d/%d %02d:%02d"),

stLocal.wMonth, stLocal.wDay, stLocal.wYear,

stLocal.wHour, stLocal.wMinute);

if( S_OK == dwRet )

return TRUE;

else return FALSE;

}

int _tmain(int argc, TCHAR *argv[])

{

HANDLE hFile;

TCHAR szBuf[MAX_PATH];

if( argc != 2 )

{

printf("This sample takes a file name as a parameter/n");

return 0;

}

hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,

OPEN_EXISTING, 0, NULL);

if(hFile == INVALID_HANDLE_VALUE)

{

printf("CreateFile failed with %d/n", GetLastError());

return 0;

}

if(GetLastWriteTime( hFile, szBuf, MAX_PATH ))

_tprintf(TEXT("Last write time is: %s/n"), szBuf);

CloseHandle(hFile);

}

分享到:
评论

相关推荐

    GetSystemTime

    gettime GetSystemTime GetLocalTime GetTimeFormat

    API之网络函数---整理网络函数及功能

    CreateScalableFontResource 为一种TureType字体创建一个资源文件,以便能用API函数AddFontResource将其加入Windows系统 DrawText 将文本描绘到指定的矩形中 DrawTextEx 与DrawText相似,只是加入了更多的功能 ...

    Windows 60个常用API

    GetSystemTime() GetThreadPriority() GetSystemMetrics() GetUserNameA() GetVolumnInformationA() GetWindowsDirectoryA() GlobalMemoryStatus() LineTo() LoadLibraryA() mciSendStringA() MessageBoxA() Mouse_...

    windows进程控制mttime

    在Windows下实现: 使用CreateProcess()来创建进程 使用WaitForSingleObject()在"mytime”命令和新创建的进程之间同步 调用GetSystemTime()来获取时间 在Linux下实现: 使用fork()/exec()来创建进程运行程序 使用...

    Asprise-OCR-Java 真正破解版

    2.使用 GetSystemTime获得系统的当前时间,和注册表里的键值(二进制格式,估计是第一次使用时间)处理运算,如果时间过期就会弹出信息框提醒需要购买license. 3.注册表中HKEY_LOCAL_MACHINE\SOFTWARE\Asprise OCR\ 有...

    Java通过JNA调用系统API

    Java通过JNA调用系统API示例,调用 Kernel32.dll文件中的GetLocalTime(),GetSystemTime(),SetLocalTime()方法。

    JNA.jar-JNative.jar-jinvoke.jar-dll创建-JAVA调用-VC调用

    包含jna.jar,JNative.jar,jinvoke.jar,VC++创建DLL例子,VC++调用DLL例子,JAVA、JNA、JNative、jinvoke调用DLL例子

    jna5.5.0--5.3.0.rar

    下面测试系统API的几个方法,如GetLocalTime(),GetSystemTime(),SetLocalTime()等方法。 GetLocalTime():获取本地时间。 GetSystemTime():获取格林威治时间。 SetLocalTime():设置本地时间。注:此方法需要系统...

    DLL的动态下载运行.mht

     Lotus Domino 是网络上先进的群件和电子邮件服务器,与客户端应用 Lotus Notes 相配合,具有灵活的安全模式,支持任意大小的多企业构架,全域范围的搜索服务,支持对绝大部分企业系统级的实时访问,功能非常强大...

    mytime.zip_windows myti

    在Windows下实现: &#8226 使用CreateProcess()来创建进程 &#8226 使用WaitForSingleObject()在“mytime”命令和新创建的进程之间同步 &#8226 调用GetSystemTime()来获取时间 在Linux下实现: &#8226 使用fork()/...

    使用GetSystemTimes获得cpu使用率

    相信很多人遇到使用NtQuerySystemInformation获得系统cpu使用率的问题,在server2008上失败,现通过GetSystemTimes同样可以获得使用率,并调试通过

    WinAPI (Delphi版)

    Creating Windows CreateMDIWindow CreateWindow CreateWindowEx RegisterClass RegisterClassEx UnregisterClass Message Processing BroadcastSystemMessage CallNextHookEx CallWindowProc...

    获取电脑信息.zip

    label12.caption:='你的系统时间是:'+getsystemtime else label12.Caption:='未取得系统时间'; if getlocaltime&lt;&gt;'' then label13.caption:='你的本地时间是:'+getlocaltime else label13.caption:='未取得本地...

    node-getrusage:Unix getrusage的C ++端口,用于获取cputime,usertime和其他进程信息

    unix方法getrusage简单包装,用于报告CPU时间和其他进程信息 安装 npm install getrusage 或者: git clone git://github.com/davglass/node-getrusage.git cd node-getrusage npm install 用法 var proc = ...

    操作系统实验,进程控制

    操作系统实验,压缩包中包括Linux和windows进程控制源代码及实验报告。 进程控制实验题目: 设计并实现Unix的“time”命令。“mytime”命令通过命令行参数接受要运行的程序,创建一个独立的进程来运行该程序,并...

    易语言设置优化系统时间

    易语言设置优化系统时间源码,设置优化系统时间,GetSystemTime,SetSystemTime

    e语言-易语言软件加密技术

    是否不依赖于GetLocalTime( )、GetSystemTime( )这样众所周知的函数来获取系统时间? 11. 是否有伪破J功能? 12. 是否在软件中嵌入了反跟踪的代码? 13. 是否对校验函数命名做了刻意隐蔽? ...

    操作系统课程设计进程控制

    在Windows下实现: 使用CreateProcess()来创建进程 使用WaitForSingleObject()在“mytime”命令和新创建的进程之间同步 调用GetSystemTime()来获取时间 在Linux下实现: 使用fork()/execv()来创建进程运行程序 使用...

    winfrom 本地时间同步为北京标准时间

    项目中的一个小功能,与大家分享下 ... public static extern void GetSystemTime(ref SYSTEMTIME time); [DllImport("Kernel32.dll")] public static extern void GetLocalTime(ref SYSTEMTIME time);

    快速解决owin返回json字符串多带了双引号”多了重string转义字符串

    public HttpResponseMessage getsystemtime() { cltime time = new cltime(); time.datetime = DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss); string relsut = JsonConvert.SerializeObject(time); var resp =...

Global site tag (gtag.js) - Google Analytics