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

📄 dlgnew.cpp

📁 一个虚拟光驱程序(驱动程序和exe)-VaporCDSource141
💻 CPP
字号:
/*
    VaporCD CD-ROM Volume Emulation for Windows NT/2000
    Copyright (C) 2000  Brad Johnson

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    http://vaporcd.sourceforge.net/

    Brad Johnson
    3305 2nd PL #222
    Lubbock, TX 79415
*/

// DlgNew.cpp : implementation file
//

#include "stdafx.h"
#include "VaporCD.h"
#include "DlgNew.h"

#include "DlgProgress.h"


#include <winioctl.h>
#include <ntddcdrm.h>
#include <winsvc.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// DlgNew dialog


DlgNew::DlgNew()
	: CPropertyPage(DlgNew::IDD)
{
	//{{AFX_DATA_INIT(DlgNew)
	m_bIgnoreErrors = FALSE;
	//}}AFX_DATA_INIT
	m_psp.dwFlags &= ~PSP_HASHELP;
}


void DlgNew::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DlgNew)
	DDX_Control(pDX, IDC_NEW_PATH, m_cNewPath);
	DDX_Control(pDX, IDC_NEW_LETTER, m_cNewLetter);
	DDX_CBString(pDX, IDC_NEW_PATH, m_sNewPath);
	DDX_CBString(pDX, IDC_NEW_LETTER, m_sNewLetter);
	DDX_Check(pDX, IDC_IGNORE_ERRORS, m_bIgnoreErrors);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DlgNew, CDialog)
	//{{AFX_MSG_MAP(DlgNew)
	ON_BN_CLICKED(IDC_NEW, OnNew)
	ON_BN_CLICKED(IDC_NEW_BROWSE, OnNewBrowse)
	ON_CBN_DROPDOWN(IDC_NEW_PATH, OnDropdownNewPath)
	ON_CBN_DROPDOWN(IDC_NEW_LETTER, OnDropdownNewLetter)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DlgNew message handlers

UINT MyControllingFunction( LPVOID pParam )
{
	return ((DlgNew *)pParam)->StartCopyThread();
}
void DlgNew::OnNew() 
{
	UpdateData(TRUE);
	// start the copy thread
	AfxBeginThread( MyControllingFunction, this,THREAD_PRIORITY_BELOW_NORMAL);
	// start the modal
	m_dlgProgress.m_bCancel = FALSE;
	m_dlgProgress.m_sMessage = "Starting... ";

	m_dlgProgress.DoModal();
//	m_dlgProgress->m_sMessage = "Done";

}

BOOL DlgNew::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void DlgNew::OnNewBrowse() 
{

	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = 
		"VaporCD Image Files (*.VaporCD)|*.VaporCD|"
        "ISO CD Image Files(*.ISO)|*.ISO|"
		"All Files (*.*)|*.*||";

	CFileDialog d(FALSE,NULL,m_sNewPath,OFN_HIDEREADONLY,szFilter);
	UpdateData(TRUE);
	if (d.DoModal() == IDOK)
	{
		m_sNewPath = d.GetPathName();
		CString s = m_sNewPath;
		s.MakeUpper();
		if ( s.Right(8) != ".VAPORCD" && s.Right(4) != ".ISO")
		{
			m_sNewPath =m_sNewPath+".VaporCD";
		}

		UpdateData(FALSE);
	}	
}


void DlgNew::OnDropdownNewPath() 
{
	// Fill the list box with strings from the registry
	m_cNewPath.ResetContent();
	for (int i=0; i < 10; i++)
	{
		CString s;
		CString val;
		val.Format("hist%i",i);
		s = ((CVaporCDApp *)AfxGetApp())->GetProfileString("New",val,"");
		if (s != "")
			m_cNewPath.AddString(s);
	}
}

BOOL DlgNew::SaveHistory()
{
	// Add to the front of the list, remove from other if already in list
	if (m_sNewPath == "")
		return TRUE;
	int ind = m_cNewPath.FindStringExact(-1,m_sNewPath);
	if (ind == CB_ERR)
	{
		m_cNewPath.InsertString(0,m_sNewPath);
	}else
	{
		m_cNewPath.DeleteString(ind);
		m_cNewPath.InsertString(0,m_sNewPath);
	}

	// Save the contents of the listbox
	for (int i=0; i < 10; i++)
	{
		CString s;
		CString val,value;
		val.Format("hist%i",i);

		if (i < m_cNewPath.GetCount())
			m_cNewPath.GetLBText(i, value);
		else
			value = "";

		s = ((CVaporCDApp *)AfxGetApp())-> WriteProfileString("New",val, value);
	}
	return TRUE;
}

void DlgNew::OnDropdownNewLetter() 
{
	m_cNewLetter.ResetContent();
	for (char c= 'A'; c<= 'Z' ;c++)
	{
		CString s,s1;
		s.Format("%c",c);
		s1.Format("%c:\\",c);
		int iType = GetDriveType(s1);

		if (iType == DRIVE_CDROM){

			m_cNewLetter.AddString(s);
		}
	}
}


int DlgNew::StartCopyThread()
{

// this must be larger than 64*1024!!!
#define BLOCK_SIZE (512*1024)

// must be less or equal to BLOCK_SIZE
#define READ_CHUNK_SIZE (512*1024)


	// send status messages
	//
	// The user wants to create a new cdrom image
	// We need to show a progress indicator and progress stats
	//



	// There is no need to talk to the kernel mode driver for this operation

	//TODO TODO

	// Get the freespace on the volume that the image is on
	// If there is not enough free space then show a warning
		// Give chance to abort

	// Open the cdrom drive


	if (m_sNewLetter.GetLength() != 1)
	{
		AfxMessageBox("Please select a CD drive to copy from.");
		m_dlgProgress.SendMessage(WM_COMMAND,IDCANCEL);
		return 1;
	}

	// Save the string to the registry so that we can put it in the list box next time
	SaveHistory();
//	return 2;

	CString s;
	s.Format("\\\\.\\%c:",m_sNewLetter[0]);
	s.MakeUpper();
	HANDLE h;
	h = CreateFile(
		s,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);

	if (h == NULL || h == INVALID_HANDLE_VALUE )
	{
		AfxMessageBox("Could not open CD drive "+s+": for read.\n");
		m_dlgProgress.SendMessage(WM_COMMAND,IDCANCEL);
		return 3;
	}

	// Open the outfile
	CFile f;
	DeleteFile(m_sNewPath); 
	if (!f.Open(m_sNewPath,CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive))
	{
		AfxMessageBox("Could not create file "+m_sNewPath+".\n"
			"You must specify a filename to create in the \"NewImage File Path\" box.");
		m_dlgProgress.SendMessage(WM_COMMAND,IDCANCEL);
		return 4;
	}

	BYTE * buf;
	buf = new BYTE[BLOCK_SIZE];
	if (!buf)
	{
		AfxMessageBox("Could not create copy buffer");
		m_dlgProgress.SendMessage(WM_COMMAND,IDCANCEL);
		return 4;
	}
	
	// blank the memory
	ZeroMemory(buf,BLOCK_SIZE);

	DWORD dwBytes;
	strcpy((char *)(&(buf[0])),"VaporCD image file v1.0 Brad Johnson");

	// save goemetry
	DISK_GEOMETRY *dg;
	DWORD dwIoCtl = IOCTL_CDROM_GET_DRIVE_GEOMETRY;
	dwBytes = 1024;
	dg = (DISK_GEOMETRY *)&(buf[1*1024]);
	DeviceIoControl(h,dwIoCtl,NULL,0,&(buf[1*1024]),dwBytes,&dwBytes,FALSE);

	// save TOC
	CDROM_TOC *toc;
	dwIoCtl = IOCTL_CDROM_READ_TOC;
	dwBytes = 1024;
	toc = (CDROM_TOC*)&(buf[2*1024]);
	DeviceIoControl(h,dwIoCtl,NULL,0,&(buf[2*1024]),dwBytes,&dwBytes,FALSE);

	// save all 64k of data to file
    CString s1;
    s1 = m_sNewPath;
    s1.MakeUpper();
    // we skip this for making the ISO images.
    if (s1.Right(4) != ".ISO")
	    f.Write(buf,64*1024);

	// read from the cdrom and write to the file until the end of the cd!!!

	int count = 0;
	int iTotalSize = 1000;
	CString sPath;
	sPath.Format("%c:\\",m_sNewLetter[0]);
	ULARGE_INTEGER ulFreeBytesAvailableToCaller;
	ULARGE_INTEGER ulTotalNumberOfBytes;
	ULARGE_INTEGER ulTotalNumberOfFreeBytes;
	LONGLONG ll =0;
	if (GetDiskFreeSpaceEx(
		  sPath,                 // pointer to the directory name
		  &ulFreeBytesAvailableToCaller, // receives the number of bytes on
														// disk available to the caller
		  &ulTotalNumberOfBytes,    // receives the number of bytes on disk
		  &ulTotalNumberOfFreeBytes // receives the free bytes on disk
		  ))
	{
		ll = ulTotalNumberOfBytes.QuadPart;
		iTotalSize = (int)(ll >> 10);
	} else 
		iTotalSize = 0;

	CTime tStart = CTime::GetCurrentTime();
	LONGLONG lBytesRead = 0;

	//Loop
	int iRetryCount;
	int iBadReads = 0;
	while(TRUE)
	{
		iRetryCount = 0;
		CTimeSpan  tsLaps = CTime::GetCurrentTime()-tStart;
		// Read, Write
		count++;
		if (m_dlgProgress.m_bCancel)
		{
			// abort!
			f.Close();
			// Close CD
			CloseHandle(h);
			delete []buf;
			return 7;
		}
		if (tsLaps.GetTotalSeconds() < 1)
		{
			m_dlgProgress.m_sMessage = "";
            m_dlgProgress.m_cProgress.SetPos(0);
		}else
		{
            m_dlgProgress.m_cProgress.SetPos(
                (count*100*(READ_CHUNK_SIZE/1024))/iTotalSize);
			m_dlgProgress.m_sMessage.Format(
//                "Count %i\n"
                "Currently reading %iMB of total %iMB.\n"
				"Seconds laps %is    Transfer rate %3.3fMB/s\n"
				"Estimated total %is    %i%% complete\n"
				"%i Read errors",
//                count,
				count*READ_CHUNK_SIZE/(1024*1024),iTotalSize/1024, 

				tsLaps.GetTotalSeconds(), 
				(double)count*READ_CHUNK_SIZE / 
                     ((double)tsLaps.GetTotalSeconds()*(1024*1024)),

				(int)((iTotalSize/(READ_CHUNK_SIZE/1024))/ 
				((double)count / (double)tsLaps.GetTotalSeconds() )),
				(count*100*(READ_CHUNK_SIZE/1024))/iTotalSize,
				iBadReads
				);
		}
//		m_dlgProgress->UpdateData();
//		m_dlgProgress->UpdateWindow();


		DWORD dwBytes = READ_CHUNK_SIZE;
		// read one sector at a time!!!
		BOOL r = ReadFile(h,buf,dwBytes,&dwBytes,NULL);
		if (!r || dwBytes==0) 
		{
//			int error = GetLastError();

//			r = ReadFile(h,buf,dwBytes,&dwBytes,NULL);
//			if (!r || dwBytes==0)
//			{
				iBadReads++;
				if (!m_bIgnoreErrors)
				{
    				int error1 = GetLastError();
					CString sError;
					sError.Format("Error #%i while reading from CD.\n"
						"Ignore this error and continue?",error1);
					int r = AfxMessageBox(sError, MB_OKCANCEL   );
					if (r != IDOK)
					{
						break;
					}
				}
                dwBytes = READ_CHUNK_SIZE;
//			}
		}
		lBytesRead += dwBytes;

		f.Write(buf,dwBytes);

		if (lBytesRead >= ll)
			break;
		// Update Progress
	}

	// Close Outfile
	f.Close();
	// Close CD
	CloseHandle(h);

	// send message to enable the end of the modal
	m_dlgProgress.SendMessage(WM_COMMAND,IDCANCEL);
	delete []buf;
	return 0;
}



⌨️ 快捷键说明

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