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

📄 general.cpp

📁 visual c++ 实例编程
💻 CPP
📖 第 1 页 / 共 4 页
字号:
#include <windows.h>
#include <afxcmn.h>
#include "stdafx.h"
#include "General.h"
#include <winsock2.h>

void DisplayPartTable(WORD Drive, WORD Cylinder, WORD Head, WORD Sector, WORD NumSectors, DRIVEPACKET *DrivePacket, DWORD, BOOL);
void DisplayBootRecord(WORD Drive, WORD Cylinder, WORD Head, WORD Sector, WORD NumSectors, DRIVEPACKET *DrivePacket, DWORD, BOOL);
void DeleteColumns(void);
void DisplayFAT16(DRIVEPACKET *DrivePacket);
void DisplayFAT12(DRIVEPACKET *DrivePacket);
void DisplayRootDir(DRIVEPACKET *DrivePacket);
void OccupyFATCluster(DWORD NextCluster, DWORD CurrentFreeCluster, WORD FatType, LPBYTE pFat);
WORD FormLongFileName(DIRECTORY *Directory, char *LfnBuff);
BOOL ReadDirBuffer(LPDWORD, DRIVEPACKET *, DWORD, LPBYTE);
void DisplayFilesFolders(DRIVEPACKET *DrivePacket);
void DisplayFile(DRIVEPACKET *DrivePacket);
HLOCAL hMemBufferFatEntries;
void	DisplayFilesFoldersSectors(DRIVEPACKET *TmpDrivePacket);
void	DisplaySectors(DRIVEPACKET *DrivePacket);
void DisplayOneSector(DRIVEPACKET *DrivePacket);
BOOL WriteSectors(DRIVEPACKET *DrivePacket, CYLHEADSECT *CylHeadSect, LPBYTE Buffer);
void CheckFatForBadCluster(LPBYTE pMemFat, LPDWORD CurrentCluster, WORD FatType);
void DisplayFAT32(DRIVEPACKET *DrivePacket);
BOOL FatChanged=FALSE, ClusterChanged=FALSE;
int gDrive;
bool ItsFile=false;
void CallNet();
extern SOCKET SocketNum;
extern BOOL BufferRead;

extern LPBYTE BufferNet;
extern DWORD BytesToFill;
extern DWORD NetEvent;
extern DWORD BytesToFill, NumFloppiesNet, NumHardsNets, Int13ExtNet;
extern CListCtrl *ListView;
extern CTreeCtrl *DiskTree;
extern CStatusBarCtrl  *m_wndStatusBarCtrl;
extern DLLFUNC *DllThunk32, *DllThunk32Write;
extern DLLCHECK *DllCheckInt13Extension;
extern LPBYTE Buffer;
extern char TmpStr[100];
extern BYTE gListViewType;
extern DRIVEPACKET *Packets;
extern DRIVEPACKET *DrivePacket;
extern HLOCAL hMemPackets, hMemDrivePacket, hMemBuffer;
extern DWORD NumFloppies;
extern TV_INSERTSTRUCT TvIns;
extern TV_ITEM TvI;
extern BOOL gbPrevSector, gbNextSector, gOSWin95;
HANDLE hDisk[256];
DWORD NumHards;

HTREEITEM hPrevHD=0;
DWORD CountFatEntries(DWORD StartCluster, WORD FatType, LPBYTE Buffer);
void StoreFatEntries(DWORD StartCluster, WORD FatType, LPDWORD FatEntries, DWORD NumFatEntries, LPBYTE Buffer);

CYLHEADSECT CylHeadSect;
HLOCAL hMemFilePacket;
DRIVEPACKET gDrivePacket;
DWORD gNumFatEntries;

BOOL CalcCylHeadSect(DRIVEPACKET *DrivePacket, CYLHEADSECT *CylHeadSect);
BOOL LoadSectors(DRIVEPACKET *DrivePacket, CYLHEADSECT *CylHeadSect, LPBYTE Buffer);
DWORD GetNextCluster(DWORD CurrentCluster, WORD FatType, LPBYTE Buffer);
int gCurrentIndex, gSectorsPerCluster;
LPDWORD BufferFatEntries;
SECTOR_FUNC NetCall;
SECTORS_FUNC NetCalls;

void DisplayPartTable(WORD Drive, WORD Cylinder, WORD Head, WORD Sector, WORD NumSectors, DRIVEPACKET *DrivePacket, DWORD RelativeSector, BOOL Flag)
{
	DeleteColumns();
	ListView->InsertColumn(0, "Partition", LVCFMT_LEFT, 100, 0);
	ListView->InsertColumn(1, "Field", LVCFMT_LEFT, 200, 0);
	ListView->InsertColumn(2, "Value", LVCFMT_LEFT, 200, 1);
	BufferNet = Buffer;
	NetEvent = BUFFER_FILL;
	memcpy(&gDrivePacket, DrivePacket, sizeof(gDrivePacket));
	BytesToFill = DrivePacket->NumSectors * 512;
	if(DrivePacket->Net){
		NetCall.Function = LOAD_SECTOR;
		NetCall.Cylinder = Cylinder;
		NetCall.Drive = Drive;
		NetCall.Head = Head;
		NetCall.Sector = Sector;
		NetCall.NumSectors = NumSectors;
		NetCall.NTRelativeSector = RelativeSector;
		NetCall.RelativeSector = 0;
		NetCall.Flag = Flag;
		send(SocketNum, (char *) &NetCall, sizeof(SECTOR_FUNC), 0);
		CallNet();
	}
	else if(gOSWin95){
		if(!(DllThunk32)(Drive, Cylinder, Head, Sector, NumSectors, (LPBYTE)Buffer, RelativeSector, Flag)){
			AfxMessageBox("Partition Table inaccessible!");
			return;
		}
	}
	else{
		__int64 Tmp64 = ((__int64) DrivePacket->NTRelativeSector) * 512;
		long TmpVal = Tmp64 & 0xFFFFFFFF;
		long TmpValHi = (Tmp64 >> 32);
		SetFilePointer(hDisk[DrivePacket->Drive], TmpVal, &TmpValHi, FILE_BEGIN);
		TmpVal = 0;
		ReadFile(hDisk[DrivePacket->Drive], Buffer, DrivePacket->NumSectors * 512, (DWORD *) &TmpVal, NULL);
		if((!TmpVal) && (DrivePacket->Drive < 0x80)){
			CloseHandle(hDisk[DrivePacket->Drive]);
			char TmpStr[100];
			wsprintf(TmpStr, "\\\\.\\%c:", DrivePacket->Drive+'A');
			if((hDisk[DrivePacket->Drive]=CreateFile(TmpStr, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL))==INVALID_HANDLE_VALUE){
				return;
			}
			Tmp64 = (((__int64) DrivePacket->NTRelativeSector) + ((__int64) DrivePacket->RelativeSector)) * 512;
			TmpVal = Tmp64 & 0xFFFFFFFF;
			TmpValHi = (Tmp64 >> 32);
			SetFilePointer(hDisk[DrivePacket->Drive], TmpVal, &TmpValHi, FILE_BEGIN);
			TmpVal = 0;
			ReadFile(hDisk[DrivePacket->Drive], Buffer, DrivePacket->NumSectors * 512, (DWORD *) &TmpVal, NULL);
		}
		if(TmpVal != (DrivePacket->NumSectors * 512)){
			AfxMessageBox("Partition Table not accessible!");
			return;
		}
	}
	for(int i=0; i<4; i++){
		wsprintf(TmpStr, "%u", i+1);
		ListView->InsertItem(0+i*10, TmpStr);
		ListView->SetItemText(0+i*10, 1, "Boot Indicator");
		ListView->InsertItem(1+i*10, "");
		ListView->SetItemText(1+i*10, 1, "Cylinder");
		ListView->InsertItem(2+i*10, "");
		ListView->SetItemText(2+i*10, 1, "Head");
		ListView->InsertItem(3+i*10, "");
		ListView->SetItemText(3+i*10, 1, "Sector");
		ListView->InsertItem(4+i*10, "");
		ListView->SetItemText(4+i*10, 1, "System Indicator");
		ListView->InsertItem(5+i*10, "");
		ListView->SetItemText(5+i*10, 1, "Ending Cylinder");
		ListView->InsertItem(6+i*10, "");
		ListView->SetItemText(6+i*10, 1, "Ending Head");
		ListView->InsertItem(7+i*10, "");
		ListView->SetItemText(7+i*10, 1, "Ending Sector");
		ListView->InsertItem(8+i*10, "");
		ListView->SetItemText(8+i*10, 1, "Number of Sectors");
		ListView->InsertItem(9+i*10, "");
		ListView->SetItemText(9+i*10, 1, "Relative Sectors");
	}
	PARTITION *PartitionTable = (PARTITION *) (Buffer+0x1BE);
	for(i=0; i<4; i++){
		wsprintf(TmpStr, "%s", (WORD) PartitionTable->BootInd == 0x80 ? "Bootable":"Non-Bootable");
		ListView->SetItemText(0+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%u", (WORD) PartitionTable->Cylinder);
		ListView->SetItemText(1+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%u", (WORD) PartitionTable->Head);
		ListView->SetItemText(2+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%u", (WORD) PartitionTable->Sector);
		ListView->SetItemText(3+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%u", (WORD) PartitionTable->SysInd);
		ListView->SetItemText(4+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%u", (WORD) PartitionTable->LastCylinder);
		ListView->SetItemText(5+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%u", (WORD) PartitionTable->LastHead);
		ListView->SetItemText(6+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%u", (WORD) PartitionTable->LastSector);
		ListView->SetItemText(7+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%lu", PartitionTable->NumberSectors);
		ListView->SetItemText(8+i*10, 2, TmpStr);
		wsprintf(TmpStr, "%lu", PartitionTable->RelativeSector);
		ListView->SetItemText(9+i*10, 2, TmpStr);
		PartitionTable++;
	}
}

void DisplayBootRecord(WORD Drive, WORD Cylinder, WORD Head, WORD Sector, WORD NumSectors, DRIVEPACKET *DrivePacket, DWORD RelativeSector, BOOL Flag)
{
	DeleteColumns();
	ListView->InsertColumn(0, "Field", LVCFMT_LEFT, 200, 0);
	ListView->InsertColumn(1, "Value", LVCFMT_LEFT, 200, 1);
	BufferNet = Buffer;
	NetEvent = BUFFER_FILL;
	memcpy(&gDrivePacket, DrivePacket, sizeof(gDrivePacket));
	BytesToFill = DrivePacket->NumSectors * 512;
	if(DrivePacket->Net){
		NetCall.Function = LOAD_SECTOR;
		NetCall.Cylinder = Cylinder;
		NetCall.Drive = Drive;
		NetCall.Head = Head;
		NetCall.Sector = Sector;
		NetCall.NumSectors = NumSectors;
		NetCall.NTRelativeSector = RelativeSector;
		NetCall.RelativeSector = 0;
		NetCall.Flag = Flag;
		send(SocketNum, (char *) &NetCall, sizeof(SECTOR_FUNC), 0);
		CallNet();
	}
	else if(gOSWin95){
		if(!(DllThunk32)(Drive, Cylinder, Head, Sector, NumSectors, (LPBYTE)Buffer, RelativeSector, Flag)){
			AfxMessageBox("Boot Record inaccessible!");
			return;
		}
	}
	else{
		char TmpStr[100];
		if(DrivePacket->Drive < 0x80){
			wsprintf(TmpStr, "\\\\.\\%c:", DrivePacket->Drive+'A');
			if(hDisk[DrivePacket->Drive])
				CloseHandle(hDisk[DrivePacket->Drive]);
			if((hDisk[DrivePacket->Drive]=CreateFile(TmpStr, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL))==INVALID_HANDLE_VALUE){
				return;
			}
		}
		__int64 Tmp64 = ((__int64) DrivePacket->NTRelativeSector) * 512;
		long TmpVal = Tmp64 & 0xFFFFFFFF;
		long TmpValHi = (Tmp64 >> 32);
		SetFilePointer(hDisk[DrivePacket->Drive], TmpVal, &TmpValHi, FILE_BEGIN);
		TmpVal = 0;
		ReadFile(hDisk[DrivePacket->Drive], Buffer, DrivePacket->NumSectors * 512, (DWORD *) &TmpVal, NULL);
		if((!TmpVal) && (DrivePacket->Drive < 0x80)){
			CloseHandle(hDisk[DrivePacket->Drive]);
			char TmpStr[100];
			wsprintf(TmpStr, "\\\\.\\%c:", DrivePacket->Drive+'A');
			if((hDisk[DrivePacket->Drive]=CreateFile(TmpStr, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL))==INVALID_HANDLE_VALUE){
				return;
			}
			ReadFile(hDisk[DrivePacket->Drive], Buffer, DrivePacket->NumSectors * 512, (DWORD *) &TmpVal, NULL);
		}
		if(TmpVal != (DrivePacket->NumSectors * 512)){
			AfxMessageBox("Boot Record not accessible!");
			return;
		}
	}
	ListView->InsertItem(0, "OEM Name");
	ListView->InsertItem(1, "Bytes Per Sector");
	ListView->InsertItem(2, "Sectors Per Cluster");
	ListView->InsertItem(3, "Reserved Sectors");
	ListView->InsertItem(4, "Number of FATs");
	ListView->InsertItem(5, "Number of Root Directory Entries");
	ListView->InsertItem(6, "Media Descriptor Byte");
	ListView->InsertItem(7, "Sectors Per FAT");
	ListView->InsertItem(8, "Sectors Per Track");
	ListView->InsertItem(9, "Number of Heads");
	ListView->InsertItem(10, "Number of Hidden Sectors");
	ListView->InsertItem(11, "Total Tracks");
	ListView->InsertItem(12, "Total Sectors");
	
	BOOT *BootRecord = (BOOT *) Buffer;
	wsprintf(TmpStr, "%.8s", BootRecord->OEM_Name);
	ListView->SetItemText(0, 1, TmpStr);
	wsprintf(TmpStr, "%u", BootRecord->BytesPerSector);
	ListView->SetItemText(1, 1, TmpStr);
	wsprintf(TmpStr, "%u", BootRecord->SectorsPerCluster);
	ListView->SetItemText(2, 1, TmpStr);
	wsprintf(TmpStr, "%u", BootRecord->ReservedSectors);
	ListView->SetItemText(3, 1, TmpStr);
	wsprintf(TmpStr, "%u", BootRecord->NumFATs);
	ListView->SetItemText(4, 1, TmpStr);
	wsprintf(TmpStr, "%u", BootRecord->NumRoot);
	ListView->SetItemText(5, 1, TmpStr);
	wsprintf(TmpStr, "0x%X", BootRecord->MediaDB);
	ListView->SetItemText(6, 1, TmpStr);
	wsprintf(TmpStr, "%lu", (BootRecord->SectorsPerFAT ? BootRecord->SectorsPerFAT:BootRecord->ExtendedSectorsPerFAT));
	ListView->SetItemText(7, 1, TmpStr);
	wsprintf(TmpStr, "%u", BootRecord->SectorsPerTrack);
	ListView->SetItemText(8, 1, TmpStr);
	wsprintf(TmpStr, "%u", BootRecord->NumHeads);
	ListView->SetItemText(9, 1, TmpStr);
	wsprintf(TmpStr, "%lu", BootRecord->NumHidSect);
	ListView->SetItemText(10, 1, TmpStr);
	wsprintf(TmpStr, "%lu", (BootRecord->TotalSectors ? (DWORD)BootRecord->TotalSectors:BootRecord->ExtendedTotalSectors)/(BootRecord->NumHeads * BootRecord->SectorsPerTrack));
	ListView->SetItemText(11, 1, TmpStr);
	wsprintf(TmpStr, "%lu", (DWORD) ((DWORD)BootRecord->TotalSectors ? (DWORD)BootRecord->TotalSectors:BootRecord->ExtendedTotalSectors));
	ListView->SetItemText(12, 1, TmpStr);
//	wsprintf(TmpStr, "%s", (BootRecord->BytesPerSector * (BootRecord->TotalSectors ? BootRecord->TotalSectors:BootRecord->ExtendedTotalSectors)) /
//		(BootRecord->BytesPerSector * BootRecord->SectorsPerCluster)>=4096 ? "16-Bit":"12-Bit");
}
void DeleteColumns(void)
{
	m_wndStatusBarCtrl->SetText("Wait....", 0, 0);
	ListView->DeleteAllItems();
	while(ListView->DeleteColumn(0))
		;
}

void DisplayFAT16(DRIVEPACKET *DrivePacket)
{
	WORD i;					  
	DeleteColumns();
	gListViewType = FAT16;
	m_wndStatusBarCtrl->SetText("Wait....", 0, 0);
	ListView->InsertColumn(0, "FAT Entry", LVCFMT_LEFT, 100, 0);
	for(i=0; i<16; i++){ 
		wsprintf(TmpStr, "%04X", i);
		ListView->InsertColumn(i+1, TmpStr, LVCFMT_LEFT, 40, i+1);
	}
	if(!LoadSectors(DrivePacket, &CylHeadSect, Buffer)){
		AfxMessageBox("FAT not accessible!");
	}
	for(i=0; i<DrivePacket->NumSectors*16 /*512/32*/; i++){
		wsprintf(TmpStr, "%08lX", i*16);
		ListView->InsertItem(LVIF_TEXT|LVIF_PARAM, i, LPSTR_TEXTCALLBACK, 0, 0, i, (LPARAM) i*16*2);
	}
	wsprintf(TmpStr, "Current Sector Number: %lu", DrivePacket->RelativeSector);
	m_wndStatusBarCtrl->SetText(TmpStr, 1, 0);
	m_wndStatusBarCtrl->SetText("", 0, 0);
}

void DisplayFAT32(DRIVEPACKET *DrivePacket)
{
	DWORD i;					  
	DeleteColumns();
	gListViewType = FAT32;
	m_wndStatusBarCtrl->SetText("Wait....", 0, 0);
	ListView->InsertColumn(0, "FAT Entry", LVCFMT_LEFT, 100, 0);
	for(i=0; i<16; i++){ 
		wsprintf(TmpStr, "%08lX", i);
		ListView->InsertColumn(i+1, TmpStr, LVCFMT_LEFT, 75, i+1);
	}
	if(!LoadSectors(DrivePacket, &CylHeadSect, Buffer)){
		AfxMessageBox("FAT not accessible!");
	}
	for(i=0; i<DrivePacket->NumSectors*8 /*512/(16*4)*/; i++){
		wsprintf(TmpStr, "%08lX", i*16);
		ListView->InsertItem(LVIF_TEXT|LVIF_PARAM, i, LPSTR_TEXTCALLBACK, 0, 0, i, (LPARAM) i*16*4);

⌨️ 快捷键说明

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