==============================ModuleE===================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Compiled using VC++/VC++ .Net.....unmanaged... #include #include int main() { //the files handle HANDLE hFile1; FILETIME ftCreate, ftAccess, ftWrite; SYSTEMTIME stUTC, stLocal, stUTC1, stLocal1, stUTC2, stLocal2; //filename char fname1[] = "c:\\testfile.txt"; //temporary storage for file sizes DWORD dwFileSize; DWORD dwFileType; //Opening the existing file hFile1 = CreateFile(fname1, //file to open GENERIC_READ, //open for reading FILE_SHARE_READ, //share for reading NULL, //default security OPEN_EXISTING, //existing file only FILE_ATTRIBUTE_NORMAL, //normal file NULL); //no attribute template if(hFile1 == INVALID_HANDLE_VALUE) { printf("Could not open %s file, error %d\n", fname1, GetLastError()); return 4; } dwFileType = GetFileType(hFile1); dwFileSize = GetFileSize(hFile1, NULL); printf("%s size is %d bytes and file type is %d\n", fname1, dwFileSize, dwFileType); //Retrieve the file times for the file. if(!GetFileTime(hFile1, &ftCreate, &ftAccess, &ftWrite)) { printf("Something wrong lol!\n"); return FALSE; } //Convert the created time to local time. FileTimeToSystemTime(&ftCreate, &stUTC); SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal); //Convert the last-access time to local time. FileTimeToSystemTime(&ftAccess, &stUTC1); SystemTimeToTzSpecificLocalTime(NULL, &stUTC1, &stLocal1); //Convert the last-write time to local time. FileTimeToSystemTime(&ftWrite, &stUTC2); SystemTimeToTzSpecificLocalTime(NULL, &stUTC2, &stLocal2); //Build a string showing the date and time. printf("Created on: %02d/%02d/%d %02d:%02d\n", stLocal.wDay, stLocal.wMonth, stLocal.wYear, stLocal.wHour, stLocal.wMinute); printf("Last accessed: %02d/%02d/%d %02d:%02d\n", stLocal1.wDay, stLocal1.wMonth, stLocal1.wYear, stLocal1.wHour, stLocal1.wMinute); printf("Last written: %02d/%02d/%d %02d:%02d\n", stLocal2.wDay, stLocal2.wMonth, stLocal2.wYear, stLocal2.wHour, stLocal2.wMinute); //close the file's handle and itself CloseHandle(hFile1); return 0; } --------------------------------------------------------------------------------------------------------- #include #include int main() { //the files handle HANDLE hFile1; //FILETIME is another name for struct _FILETIME structure (a typedef) FILETIME ftCreate; //SYSTEMTIME is another name for struct _SYSTEMTIME structure (a typedef) SYSTEMTIME stUTC, stLocal; //filename char fname1[] = "c:\\testfile.txt"; //Opening the existing file hFile1 = CreateFile(fname1, //file to open GENERIC_READ, //open for reading FILE_SHARE_READ, //share for reading NULL, //default security OPEN_EXISTING, //existing file only FILE_ATTRIBUTE_NORMAL, //normal file NULL); //no attribute template if(hFile1 == INVALID_HANDLE_VALUE) { printf("Could not open %s file, error %ul\n", fname1, GetLastError()); return 4; } //Retrieve created file times for the file. if(!GetFileTime(hFile1, &ftCreate, NULL, NULL)) { printf("Something wrong!\n"); return FALSE; } //Viewing the unreadable... //Filing the 32 bit low part into variable low and //another 32 bit high part into variable high //Accessing the FILETIME structures' members, assigning them //to some variables... DWORD low = ftCreate.dwLowDateTime; DWORD high = ftCreate.dwHighDateTime; //Trying to display the content in hex... printf("Unreadable format...\n"); printf("32 bit low part = %0X and high = %0X\n", low, high); //Convert the file created time to local time. FileTimeToSystemTime(&ftCreate, &stUTC); SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal); printf("\nReadable format...\n"); //Build a readable string showing the date and time. //Accessing the SYSTEMTIME structure's member printf("UTC System Time\n"); printf("Created on: %02d/%02d/%d %02d:%02d\n", stUTC.wDay, stUTC.wMonth, stUTC.wYear, stUTC.wHour, stUTC.wMinute); //Accessing the SYSTEMTIME structures' members printf("Local time\n"); printf("Created on: %02d/%02d/%d %02d:%02d\n", stLocal.wDay, stLocal.wMonth, stLocal.wYear, stLocal.wHour, stLocal.wMinute); //close the file's handle and itself CloseHandle(hFile1); return 0; } --------------------------------------------------------------------------------------------------------- #include #include int main() { //handle for file HANDLE hFile; DWORD dwCurrentFilePosition; //file and path char fname[20] = "c:\\module15.txt"; hFile = CreateFile(fname, //file to be opened GENERIC_WRITE, //open for writing FILE_SHARE_READ, //share for reading NULL, //default security OPEN_EXISTING, //open existing file FILE_ATTRIBUTE_READONLY, //the file is read only NULL); //no attribute template if(hFile == INVALID_HANDLE_VALUE) printf("Could not open %s file, error %d\n", fname, GetLastError()); printf("File's HANDLE is OK!\n"); dwCurrentFilePosition = SetFilePointer( hFile, //must have GENERIC_READ and/or GENERIC_WRITE 0, //do not move pointer NULL, //hFile is not large enough to need this pointer FILE_CURRENT); //provides offset from current position printf("Current file pointer position: %d\n", dwCurrentFilePosition); dwCurrentFilePosition = SetFilePointer( hFile, //must have GENERIC_READ and/or GENERIC_WRITE 10, //10 bytes NULL, //hFile is not large enough to need this pointer FILE_CURRENT); //provides offset from current position printf("Current file pointer position: %d\n", dwCurrentFilePosition); return 0; } --------------------------------------------------------------------------------------------------------- #include #include int main() { HANDLE hFile; HANDLE hAppend; DWORD dwBytesRead, dwBytesWritten, dwPos; char fname[30] = "c:\\testfile.txt"; char fname2[30] = "c:\\testfiletwo.txt"; char buff[4096]; //Open the existing file. hFile = CreateFile(fname, //open testfile.txt GENERIC_READ, //open for reading 0, //do not share NULL, //default security OPEN_EXISTING, //existing file only FILE_ATTRIBUTE_NORMAL, //normal file NULL); //no attribute template if(hFile == INVALID_HANDLE_VALUE) printf("Could not open %s lol!.\n", fname); else printf("%s opened successfully.\n", fname); //Open the existing file, or if the file does not exist, //create a new file. hAppend = CreateFile(fname2, //open testfiletwo.txt GENERIC_WRITE, //open for writing 0, //do not share NULL, //default security OPEN_ALWAYS, //open or create FILE_ATTRIBUTE_NORMAL, //normal file NULL); //no attribute template if(hAppend == INVALID_HANDLE_VALUE) printf("Could not open %s lol!.\n", fname2); else { printf("%s opened/created successfully.\n", fname2); printf("\nAppending %s\'s content to %s\'s content\n", fname, fname2); printf("Check the %s content lol!\n\n", fname2); } //Append the first file to the end of the second file. //Lock the second file to prevent another process from //accessing it while writing to it. Unlock the //file when writing is finished. do { if(ReadFile(hFile, buff, 4096, &dwBytesRead, NULL)) { dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END); if(LockFile(hAppend, dwPos, 0, dwBytesRead, 0) != 0) printf("Locking %s...\n", fname2); WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL); if(UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0) != 0) printf("Unlocking %s...\n", fname2); } } while (dwBytesRead == 4096); //Close both files. if(CloseHandle(hFile) != 0) printf("\nFile handle closed successfully!\n"); if(CloseHandle(hAppend) != 0) printf("File's append handle closed successfully!\n"); return 0; } --------------------------------------------------------------------------------------------------------- #include #include int main() { char szDirPath[20] = "c:\\testdir"; HANDLE hFile; char fname[50] = "c:\\testdir\\testfile.txt"; //Create a new directory. if(!CreateDirectory(szDirPath, NULL)) printf("Couldn't create %s directory.\n", szDirPath); else printf("%s directory successfully created.\n", szDirPath); //Create a file hFile = CreateFile(fname, //file to be opened GENERIC_READ, //open for writing FILE_SHARE_READ, //share for writing NULL, //default security CREATE_ALWAYS, //create new file only FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION, //archive and impersonate client NULL); //no attribute template //Check the handle, then open... if(hFile == INVALID_HANDLE_VALUE) printf("Could not open %s file (error %d)\n", fname, GetLastError()); else { printf("%s file HANDLE is OK!\n", fname); printf("%s opened successfully!\n", fname); } //Close the handle... if(CloseHandle(hFile) != 0) printf("CloseHandle() for %s file succeeded!\n", fname); if(DeleteFile(fname) != 0) printf("%s file successfully deleted!\n", fname); //Delete the directory... if(RemoveDirectory(szDirPath) != 0) printf("%s directory successfully deleted.\n", szDirPath); return 0; } --------------------------------------------------------------------------------------------------------- #include #include #define BUFFER_SIZE 200 int main() { TCHAR infoBuf[BUFFER_SIZE]; TCHAR lpPathName[200] = "D:\\Program Files\\Microsoft sql server"; //Get the current working directory if(!GetCurrentDirectory(BUFFER_SIZE, infoBuf)) printf("GetCurrentDirectory() failed!\n"); printf("Your current directory is: %s\n", infoBuf); printf("Changing directory...\n"); //Set to current working directory if(!SetCurrentDirectory(lpPathName)) printf("SetCurrentDirectory() failed!\n"); //Do some verification... if(!GetCurrentDirectory(BUFFER_SIZE, infoBuf)) printf("GetCurrentDirectory() failed!\n"); printf("Your current directory is: %s\n", infoBuf); //Get and display the Windows directory. if(!GetWindowsDirectory(infoBuf, BUFFER_SIZE)) printf("GetWindowsDirectory() failed!\n"); printf("Your Windows directory is: %s\n", infoBuf); //Get and display the system directory. if(!GetSystemDirectory(infoBuf, BUFFER_SIZE)) printf("GetSystemDirectory() failed!\n"); printf("Your system directory is: %s\n", infoBuf); return 0; } ===============================================================================================================