windowsdiskaccess.cpp

来自「一个可以在开发板上运行的fat文件系统,是在windows平台下开发的,是学习文」· C++ 代码 · 共 87 行

CPP
87
字号
#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 + =
减小字号Ctrl + -
显示快捷键?