📄 usbdisk_ctrl.cpp
字号:
//sptdwb.sptd.TimeOutValue= 2;
sptdwb.sptd.DataBuffer= lpOutBuffer;
sptdwb.sptd.SenseInfoOffset= offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, ucSenseBuf);
sptdwb.sptd.Cdb[0]= UFI_READCAPACITY;
/*
sptdwb.sptd.Cdb[7]= *(UCHAR*)lpCDBBuffer;
sptdwb.sptd.Cdb[8]= *((UCHAR*)lpCDBBuffer+1);
sptdwb.sptd.Cdb[9]= *((UCHAR*)lpCDBBuffer+2);
sptdwb.sptd.Cdb[10]= *((UCHAR*)lpCDBBuffer+3);
sptdwb.sptd.Cdb[11]= *((UCHAR*)lpCDBBuffer+4);
sptdwb.sptd.Cdb[12]= *((UCHAR*)lpCDBBuffer+5);
sptdwb.sptd.Cdb[13]= *((UCHAR*)lpCDBBuffer+6);
sptdwb.sptd.Cdb[14]= *((UCHAR*)lpCDBBuffer+7);
*/
sptdwb.sptd.Cdb[15]= 0xF5;
break;
}
case SD_SET_DESKEY:
{
sptdwb.sptd.CdbLength= CDB10GENERIC_LENGTH+6;
sptdwb.sptd.DataIn= SCSI_IOCTL_DATA_IN;
sptdwb.sptd.DataTransferLength= nOutBufferSize;
//sptdwb.sptd.TimeOutValue= 2;
sptdwb.sptd.DataBuffer= lpOutBuffer;
sptdwb.sptd.SenseInfoOffset= offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, ucSenseBuf);
sptdwb.sptd.Cdb[0]= UFI_READCAPACITY;
sptdwb.sptd.Cdb[7]= *(UCHAR*)lpCDBBuffer;
sptdwb.sptd.Cdb[8]= *((UCHAR*)lpCDBBuffer+1);
sptdwb.sptd.Cdb[9]= *((UCHAR*)lpCDBBuffer+2);
sptdwb.sptd.Cdb[10]= *((UCHAR*)lpCDBBuffer+3);
sptdwb.sptd.Cdb[11]= *((UCHAR*)lpCDBBuffer+4);
sptdwb.sptd.Cdb[12]= *((UCHAR*)lpCDBBuffer+5);
sptdwb.sptd.Cdb[13]= *((UCHAR*)lpCDBBuffer+6);
sptdwb.sptd.Cdb[14]= *((UCHAR*)lpCDBBuffer+7);
sptdwb.sptd.Cdb[15]= 0xF6;
break;
}
default:
break;
}
int iLength = sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER);
(DWORD)*lpBytesReturned = 0;
bResult = DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT,
&sptdwb, iLength, &sptdwb, iLength, lpBytesReturned, FALSE);
for (i=0;i<2 && !bResult;i++)
{
Sleep(200);
bResult = DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT,
&sptdwb, iLength, &sptdwb, iLength, lpBytesReturned, FALSE);
}
CloseHandle(hDevice);
return bResult;
}
BOOL GetDeviceName(char *psyslinkname)
{
HANDLE rv;
for (int i=0;i<100;i++)
if ((rv=GetDeviceViaInterface((LPGUID)&DISK_GUID,i,psyslinkname))!=NULL)
{
if (NULL!=strstr(psyslinkname,DISKSYMBOL))
{
CloseHandle(rv);
return true;
}
else
psyslinkname[0]=0;
CloseHandle(rv);
}
return false;
}
int DisconnectDevice( GUID* pGuid)
{
HDEVINFO hDevInfo;
int RetryTimes =0;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
//--------------------------------------------------------------------------
// 获取设备信息
hDevInfo = SetupDiGetClassDevs(pGuid,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE );
if (hDevInfo == INVALID_HANDLE_VALUE) {
// 查询信息失败
printf("ERROR - SetupDiGetClassDevs()");
return 1;
}
//--------------------------------------------------------------------------
// 枚举每个USB设备
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0; i<32 ;i++)
{
LPTSTR buffer = NULL;
PVOID buffer2 = NULL;
DWORD buffersize = 0;
ULONG len;
CONFIGRET cr;
PNP_VETO_TYPE pnpvietotype;
CHAR vetoname[MAX_PATH];
ULONG ulStatus;
ULONG ulProblemNumber;
if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData))
continue;
cr = CM_Get_DevNode_Status( &ulStatus,
&ulProblemNumber,
DeviceInfoData.DevInst,
0);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Get_DevNode_Status()[%d]\n", cr);
printf("OK - CM_Get_DevNode_Status() sts [%x]\n", ulStatus);
printf("OK - CM_Get_DevNode_Status() pro [%x]\n", ulProblemNumber);
} else {
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", cr);
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", GetLastError());
}
// DN_DISABLEABLE or DN_REMOVABLE
if ((DN_DISABLEABLE & ulStatus ) != 0 ) {
printf("HAS - DN_DISABLEABLE()[%x]\n", DN_DISABLEABLE & ulStatus);
} else {
continue;
}
if ((DN_REMOVABLE & ulStatus ) != 0 ) {
TRACE("HAS - DN_REMOVABLE()[%x]\n", DN_REMOVABLE & ulStatus);
} else {
continue;
}
if (DeviceInfoData.ClassGuid != USBCLASS_GUID)
continue;
len = MAX_PATH;
// pnpvietotype = PNP_VetoDevice;
Sleep(100);
cr = CM_Request_Device_Eject(
DeviceInfoData.DevInst,
&pnpvietotype,
vetoname,
len,
0
);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Request_Device_Eject()[%d]\n", cr);
} else
{
printf("ERROR - CM_Request_Device_Eject()[%d]\n", cr);
int j = GetLastError();
printf("ERROR - CM_Request_Device_Eject()[%d]\n", GetLastError());
for (RetryTimes = 0; CR_SUCCESS != cr && RetryTimes<DISCONNECTTIMES; RetryTimes++)
{
Sleep(10);
cr = CM_Request_Device_Eject(
DeviceInfoData.DevInst,
&pnpvietotype,
vetoname,
len,
0
);
}
}
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
if (RetryTimes >= DISCONNECTTIMES)
return 2;
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
BOOL StateChange(DWORD NewState)
{
BOOL bFlag = FALSE;
SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
HDEVINFO hDevInfo = 0;
OSVERSIONINFO osvi;
DWORD i = 0;
BOOL bError = FALSE;
/* //
// This may take a while :^(
//
hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));*/
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if ((osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) && (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion == 0))
{
hDevInfo = SetupDiGetClassDevs(&USBSTORAGE_GUID,NULL,NULL,
DIGCF_PRESENT|DIGCF_ALLCLASSES);
// Get a handle to the Selected Item.
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData); i++)
{
DWORD DataT = 0;
LPTSTR p = NULL,pbuffer = NULL;
DWORD buffersize = 0;
// We won't know the size of the HardwareID buffer until we call
// this function. So call it with a null to begin with, and then
// use the required buffer size to Alloc the nessicary space.
// Keep calling we have success or an unknown failure.
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)pbuffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INVALID_DATA)
{
// May be a Legacy Device with no HardwareID. Continue.
break;
}
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
// We need to change the buffer size.
if (pbuffer)
{
LocalFree(pbuffer);
pbuffer = NULL;
}
pbuffer = (CHAR*)LocalAlloc(LPTR,buffersize);
}
else
{
// Unknown Failure.
bError = TRUE;
}
}
if (bError)
{
break;
}
if (GetLastError() == ERROR_INVALID_DATA)
continue;
// Compare each entry in the buffer multi-sz list with our HardwareID.
for (p=pbuffer;*p&&(p<&pbuffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
{
CString strBuf=p;
INT nPosition = -1;
nPosition = strBuf.Find("&");
if ( nPosition > 0 )
{
nPosition = strBuf.Find("&", nPosition+1);
if (nPosition > 0)
{
strBuf = strBuf.Left(nPosition);
}
}
// usbPIDVID is your device PID&VID
char USBPIDVID[64] = "Vid_0547&Pid_1002";
//if( strBuf.CompareNoCase(USBPIDVID)==0)
if( strBuf.Find(USBPIDVID)>0)
{
// Worker function to remove device.
PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChange = NewState;
if (!SetupDiSetClassInstallParams(hDevInfo,
&DeviceInfoData,
(SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)))
{
int iError = GetLastError();
return FALSE;
}
// Call the ClassInstaller and perform the change.
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
hDevInfo,
&DeviceInfoData))
{
return TRUE;
}
bFlag = TRUE;
break;
}
if (bFlag == TRUE)
{
break;
}
}
if (pbuffer)
{
LocalFree(pbuffer);
pbuffer = NULL;
}
}
}
return TRUE;
}
BOOL RegisterDevice(HANDLE hRecipient, GUID* pGuid)
{
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(NotificationFilter);
NotificationFilter.dbcc_devicetype=DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid= (GUID)*pGuid;
/*
// 針對所有裝置的改變訊息
hDevNotify=RegisterDeviceNotification(this->m_hWnd,&NotificationFilter,\
DEVICE_NOTIFY_WINDOW_HANDLE|DEVICE_NOTIFY_SERVICE_HANDLE );
*/
// 只有針對指定的裝置
//DEVICE_NOTIFY_ALL_INTERFACE_CLASSES
HANDLE hDevNotify=RegisterDeviceNotification(hRecipient,&NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
if(hDevNotify==NULL)
return false;
return true;
}
BOOL ProcessKey(char * strDeviceName,UCHAR *KeyOut)
{
BOOL bResult=false;
UCHAR bMassBuf[MAX_INQUIRY_BUFFER_LENGTH + 32];
DWORD nLen;
PUCHAR pBuf;
pBuf = bMassBuf;
int i =0;
int iRand =0;
//BYTE bRandKey[8];
BOOL bInitKey = true;
bResult = MassStorageIO(strDeviceName,
SD_GET_DESKEY,
NULL,//bRandKey,
pBuf,
SCSITRANSLEN8,
&nLen,
NULL,0);
if (bResult)
{
for (i=0;i<8;i++)
if (pBuf[i] != INITKEY[i])
bInitKey = false;
if (bInitKey)
{
srand( (unsigned)time(NULL));
for (i=0;i<8;i++)
KeyOut[i] = rand();
bResult = MassStorageIO(strDeviceName,
SD_SET_DESKEY,
KeyOut,//bRandKey,
pBuf,
SCSITRANSLEN8,
&nLen,
NULL,0);
return bResult;
}
else
{
for (i=0;i<8;i++)
KeyOut[i] = pBuf[i];
KeyOut[0] = KeyOut[0] ^0x5a;
KeyOut[1] = KeyOut[1] ^0xa5;
KeyOut[2] = KeyOut[2] ^0xa5;
KeyOut[3] = KeyOut[3] ^0x5a;
KeyOut[4] = KeyOut[4] ^0x5a;
KeyOut[5] = KeyOut[5] ^0xa5;
KeyOut[6] = KeyOut[6] ^0xa5;
KeyOut[7] = KeyOut[7] ^0x5a;
return true;
}
}
else
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -