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

📄 savedisk.cpp

📁 保存软盘内容到一个指定的文件内.vc++6.0
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	p = strstr(DiskID,"可移动盘");
	if(p==NULL)
		return 0xFF;
	return(*(p + strlen("可移动盘"))-'0');
}
VOID UpdataSetting()//根据磁盘0扇区更新界面设置
{
	CHAR SectorData[512],tem[100],logicname[50];
	ULONG ItemSel;
	HANDLE mDevice;

	ItemSel = SendDlgItemMessage(AfxMainHwnd,IDC_UList,CB_GETCURSEL,0,0);
	if(ItemSel == CB_ERR)
	{
		MessageBox(AfxMainHwnd,"请选择磁盘","UpdataSetting",0);
		return;
	}	
	sprintf(&logicname[0],"\\\\.\\PhysicalDrive%d\0",GetDiskNumFromSel());
	mDevice = CreateFile( &logicname[0], GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );	
	DebugPrint("CreateFile error:%x",GetLastError());
	if(mDevice==INVALID_HANDLE_VALUE){
		MessageBox(AfxMainHwnd,"打开磁盘错误","UpdataSetting",0);
		return;
	}
	if(ReadSector(mDevice,1,0,SectorData)){
		MessageBox(AfxMainHwnd,"读取磁盘错误","UpdataSetting",0);
		CloseHandle(mDevice);
		return;
	}
	sprintf(tem,"%01x%08x",SectorData[4],((PULONG)SectorData)[0]);
	SetWindowText(GetDlgItem(AfxMainHwnd,IDC_SaveSectorStart),tem);
	sprintf(&tem[0],"%08x",*(PULONG)&SectorData[5]);
	SetWindowText(GetDlgItem(AfxMainHwnd,IDC_SaveSectors),tem);
	CloseHandle(mDevice);
	return;
}

//保存数据到文件里
BOOL SaveToFile(PCHAR FileName,//打印信息
				 PUCHAR Buffer,
				 ULONG OLen,
				 BOOL IsNew //是否创建新的文件
				 )
{
	CHAR tem[10]="";
	HANDLE DFileHandle;
	DWORD nNumberOfBytesToWrite;
	if(IsNew)
		DFileHandle = CreateFile(FileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL);
	else
		DFileHandle = CreateFile(FileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL);
	if(DFileHandle==INVALID_HANDLE_VALUE)
		return FALSE;
	nNumberOfBytesToWrite=OLen;
	SetFilePointer(DFileHandle,0,NULL,FILE_END);
	if(!WriteFile(DFileHandle,Buffer,nNumberOfBytesToWrite,&nNumberOfBytesToWrite,NULL))
	{
		MessageBox(AfxMainHwnd,"写文件出错","SaveToFile",0);
		DebugPrint("WriteFile %s %dB failure.",FileName,nNumberOfBytesToWrite);
		CloseHandle(DFileHandle);
		return FALSE;
	}
	CloseHandle(DFileHandle);
	return FALSE;
}
//保存数据\到文件线程
DWORD WINAPI SaveDataProc(LPVOID lpParameter)
{

	LONGLONG SectorStart,FileStartSect;
	HANDLE mDevice;
	CHAR logicname[100]="",FileName[200]="",tem[100];
	UCHAR SectorData[512*1024+2];
	ULONG i,j,SingStep;
	ULONG FileCount,SectorCount,MinSectsPerFile,FileSects;
	ULONG ReadSects,MinSectsPerRead,ReadStartSect,ReadCount;

	EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),TRUE);
	//打开磁盘
	sprintf(logicname,"\\\\.\\PhysicalDrive%x",GetDiskNumFromSel());
	mDevice = CreateFile( &logicname[0], GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );	
	if(mDevice==INVALID_HANDLE_VALUE){
		MessageBox(AfxMainHwnd,"打开磁盘错误","SaveDataProc",0);
		EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),FALSE);
		return 0;
	}
	if(ReadSector(mDevice,1,0,&SectorData[0])){
		MessageBox(AfxMainHwnd,"打开磁盘错误","SaveDataProc",0);
		CloseHandle(mDevice);
		EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),FALSE);
		return 0;
	}
	MinSectsPerFile = GetDlgItemInt(AfxMainHwnd,IDC_MinSaveUnit,NULL,FALSE); //以M为单位
	MinSectsPerFile *=(1024*2); //每个文件的最小扇区数(1024*1024/512)
	//从磁盘0扇区前5个字节获取到要保存的起始扇区号
	SectorStart = *(PULONG)(&SectorData[0])+SectorData[4]*0x100000000;
	//从磁盘0扇区第5,6,7,8字节获取到要保存的扇区数
	SectorCount = *(PULONG)(&SectorData[5]);
	//要生成的文件数
	FileCount = (SectorCount+MinSectsPerFile-1)/MinSectsPerFile;

	for(i=0;i<FileCount;i++)
	{
		CHAR Path[200]="",FullFileName[200]="";
		GetDlgItemText(AfxMainHwnd,IDC_SavePath,Path,200);
		if(strlen(Path)<1)//如果没输入,取默认文件名
		{
			MessageBox(AfxMainHwnd,"请选择保存路径","SaveDataProc",0);
			CloseHandle(mDevice);
			EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),FALSE);
			return 0;
		}
		GetDlgItemText(AfxMainHwnd,IDC_FileName,FileName,200);
		if(strlen(FileName)<1)//如果没输入,取默认文件名
		{
			MessageBox(AfxMainHwnd,"请输入文件名","SaveDataProc",0);
			CloseHandle(mDevice);
			EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),FALSE);
			return 0;
		}
		sprintf(FullFileName,"%s//%s%x.log\0",Path,FileName,i);

		FileStartSect = (LONGLONG)(SectorStart + i*MinSectsPerFile); //文件的保存的起始扇区数
		//文件保存的扇区数
		if((i+1)>=FileCount)//最后一个文件要保存的剩余扇区数
			FileSects = SectorCount - i*MinSectsPerFile ;
		else
			FileSects = MinSectsPerFile;

		//每次从磁盘中读1024扇区(受内存限制)
		MinSectsPerRead = 1024;
		ReadCount = (FileSects+MinSectsPerRead-1)/MinSectsPerRead;//文件要分批读的次数
		for( j=0;j<ReadCount;j++ )
		{
			ReadStartSect = (ULONG)(FileStartSect+j*MinSectsPerRead);//分批读的起始扇区数
			if( (j+1)>=ReadCount ) //文件的最后一批扇区读
				ReadSects = FileSects - j*MinSectsPerRead;
			else
				ReadSects = MinSectsPerRead;			
			ReadSector(mDevice,ReadSects,ReadStartSect,&SectorData[0]);
			
			SaveToFile(FullFileName,SectorData,ReadSects*512,j==0);			
			SingStep = MinSectsPerFile*i + (MinSectsPerRead*j+ReadSects); //已完成扇区数
			sprintf(tem,"已保存扇区数%ld-%d%%",SingStep,SingStep*100/SectorCount);
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_Proc),tem);
			SendDlgItemMessage(AfxMainHwnd,IDC_PROGRESS1,PBM_SETPOS ,SingStep*100/SectorCount,0);
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_PROGRESS1),tem);
			
			if(CancelSave)
			{
				CloseHandle(mDevice);
				EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),FALSE);
				return 0;
			}
		}		
	}
	CloseHandle(mDevice);
	EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),FALSE);
	return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow){
	AFXInstance = hInstance;
	return 	DialogBox(hInstance, (LPCTSTR)IDD_MainWnd, 0, (DLGPROC)WndProc);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	int wmId, wmEvent;
	DWORD ThreadID;
	AfxMainHwnd=hWnd;
	switch (message) {
	case WM_INITDIALOG:
		{
			INITCOMMONCONTROLSEX CommStruc={0};
			CommStruc.dwSize = sizeof(CommStruc);
			CommStruc.dwICC = ICC_PROGRESS_CLASS;
			InitCommonControlsEx(&CommStruc);

			HICON  hIcon = LoadIcon(AFXInstance, MAKEINTRESOURCE(IDI_SAVEDISK));
			SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon); 
			SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);

			SearchUDisk();
			SendDlgItemMessage(AfxMainHwnd,IDC_UList,CB_SETCURSEL,0,0);
			UpdataSetting();
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_MinSaveUnit),"100");
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_FileName),"UDiskPart");
			EnableWindow(GetDlgItem(AfxMainHwnd,IDC_CancelSave),FALSE);
			SendDlgItemMessage(AfxMainHwnd,IDC_PROGRESS1,PBM_SETRANGE32,0,100);
			SendDlgItemMessage(AfxMainHwnd,IDC_PROGRESS1,PBM_SETSTEP,1,0);			
			SendDlgItemMessage(AfxMainHwnd,IDC_PROGRESS1,PBM_SETPOS ,0,0);

			CHAR FilePath[200];
			GetCurrentDirectory(200,FilePath);
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_SavePath),FilePath);
		}
		break; 
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{
		case IDC_SearchDisk:			
			SendDlgItemMessage(AfxMainHwnd,IDC_UList,CB_RESETCONTENT,0,0);
			SearchUDisk();
			SendDlgItemMessage(AfxMainHwnd,IDC_UList,CB_SETCURSEL,0,0);
			UpdataSetting();
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_MinSaveUnit),"100");
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_FileName),"UDiskPart");
			break;
		case IDC_SAVEDISK:
			CancelSave = FALSE;
			SetWindowText(GetDlgItem(AfxMainHwnd,IDC_Proc),"已保存扇区数0-0%");
			SaveThread = CreateThread(NULL,0,SaveDataProc,NULL,0,&ThreadID);
			if(SaveThread==INVALID_HANDLE_VALUE)
			{
				MessageBox(AfxMainHwnd,"创建线程失败","WndProc",0);
				break;
			}
			break;
		case IDC_CancelSave:
			CancelSave = TRUE;
			break;
		case IDC_SaveDir:
			{
				CHAR FileName[MAX_PATH];
				BROWSEINFO BrowseInfo={0};
				LPITEMIDLIST pidl;
				BrowseInfo.hwndOwner = hWnd;
				BrowseInfo.pidlRoot = NULL;
				BrowseInfo.pszDisplayName = FileName;
				BrowseInfo.lpszTitle = "Select directory to save";
				BrowseInfo.lpfn = NULL;
				BrowseInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_EDITBOX;
				pidl = SHBrowseForFolder(&BrowseInfo);
				if(pidl==NULL)
					break;
				SHGetPathFromIDList (pidl, FileName);
				SetWindowText(GetDlgItem(hWnd,IDC_SavePath),FileName);
			}
			break;
		case WM_DESTROY:
			CancelSave = TRUE;
			Sleep(10);
			CloseHandle(SaveThread);
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;		
		case WM_DESTROY:
			PostQuitMessage(0);
			break;		
	}
	return 0;
}

⌨️ 快捷键说明

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