|
#include "StdAfx.h"
#include "Function.h"
CString GetDiskNumber(CString name)
{
HKEY hkey;
char sz[256];
DWORD dwtype,sl = 256;
int number=0;
// 确定选择的磁盘
for(int i=1;i<8;i++)
{
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM//CurrentControlSet//Services//Disk//Enum",/
NULL,KEY_ALL_ACCESS,&hkey)==ERROR_SUCCESS)
{
CString id;
id.Format("%d",i);
if(RegQueryValueEx(hkey,id,NULL,&dwtype,(LPBYTE)sz,&sl)==ERROR_SUCCESS)
{
CString str=(CString)sz;
if(str.Compare(name)==0)
{
number=i;
break;
}
}
}
}
CString driver=".//PHYSICALDRIVE";
CString num;
num.Format("%d",number);
driver+=num;
return driver;
}
int ReadDisk(CString driver,unsigned char *Buf,long addr)
{
HANDLE hDevice;
BOOL bResult;
DWORD bytesread;
hDevice=CreateFile(driver,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if(hDevice==INVALID_HANDLE_VALUE)
{
AfxMessageBox("Error!");
return 0;
}
if(addr!=0)
{
SetFilePointer(hDevice,512*addr,NULL,NULL);
}
bResult=ReadFile(hDevice,Buf,512,&bytesread,NULL);
if((bResult==FALSE)||(bytesread<512))
{
AfxMessageBox("Error!");
return 0;
}
CloseHandle(hDevice);
return 1;
}
int WriteDisk(CString driver,unsigned char *Buf,long addr)
{
HANDLE hDevice;
BOOL bResult;
DWORD bytesread;
hDevice=CreateFile(driver,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if(hDevice==INVALID_HANDLE_VALUE)
{
AfxMessageBox("Error!");
return 0;
}
if(addr!=0)
{
SetFilePointer(hDevice,512*addr,NULL,NULL);
}
bResult=WriteFile(hDevice,Buf,512,&bytesread,NULL);
if((bResult==FALSE)||(bytesread<512))
{
AfxMessageBox("Error!");
return 0;
}
CloseHandle(hDevice);
return 1;
}
void PopupUSBDevice()
{
char strSystemDirectory[256];
GetSystemDirectory( strSystemDirectory, 256 );
CString strTemp = strSystemDirectory;
strTemp += "//rundll32.exe shell32.dll,Control_RunDLL hotplug.dll";
WinExec( strTemp, SW_SHOW );
} |
|