⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowsdiskaccess.cpp

📁 一个可以在开发板上运行的fat文件系统,是在windows平台下开发的,是学习文件系统的好帮手
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>

// File Functions
static __int64 DiskFileSeek(HANDLE hf, __int64 distance, DWORD MoveMethod);

//---------------------------------------------------------------------
// ReadSector: Read disk sector (512 bytes) in Windows
//---------------------------------------------------------------------
extern "C" BOOL ReadDiskSector(int drive, DWORD startinglogicalsector, int numberofsectors, BYTE *buffer)
{
	HANDLE hDevice; 
	DWORD bytesread;
    
	// Creating a handle to drive a: using CreateFile () function ..
	char _devicename[] = "\\\\.\\PhysicalDrive0";
	_devicename[17] += drive;
	hDevice = CreateFile(_devicename,  // drive to open
						GENERIC_READ,     // access type
						FILE_SHARE_READ | // share mode
						FILE_SHARE_WRITE, 
						NULL,             // default security attributes
						OPEN_EXISTING,    // disposition
						0,                // file attributes
						NULL);            // do not copy file attributes

 
    if (hDevice == INVALID_HANDLE_VALUE) 
        return FALSE;

	DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

	if (!ReadFile (hDevice, buffer, 512*numberofsectors, &bytesread, NULL) )
		 return FALSE;

	CloseHandle(hDevice); 
	return TRUE;
}
//---------------------------------------------------------------------
// WriteDiskSector: Write disk sector (512 bytes) in Windows
//---------------------------------------------------------------------
extern "C" BOOL WriteDiskSector(int drive, DWORD startinglogicalsector, int numberofsectors, BYTE *buffer)
{
	HANDLE hDevice; 
	DWORD byteswritten;
    
	// Creating a handle to drive a: using CreateFile () function ..
	char _devicename[] = "\\\\.\\PhysicalDrive0";
	_devicename[17] += drive;
	hDevice = CreateFile(_devicename,  // drive to open
						GENERIC_READ|GENERIC_WRITE,     // access type
						FILE_SHARE_READ | // share mode
						FILE_SHARE_WRITE, 
						NULL,             // default security attributes
						OPEN_EXISTING,    // disposition
						0,                // file attributes
						NULL);            // do not copy file attributes

 
    if (hDevice == INVALID_HANDLE_VALUE) 
        return FALSE;

	DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

	if (!WriteFile (hDevice, buffer, 512*numberofsectors, &byteswritten, NULL) )
		 return FALSE;

	CloseHandle(hDevice); 
	return TRUE;
}
//---------------------------------------------------------------------
// DiskFileSeek: Allow seeking through large files (a disk opened as a
// file).
//---------------------------------------------------------------------
static __int64 DiskFileSeek(HANDLE hf,__int64 distance, DWORD MoveMethod)
{
	__int64 seekDistance=0;
	seekDistance = distance*512;
	LARGE_INTEGER li;
	li.QuadPart = seekDistance;
	li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
	return li.QuadPart;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -