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

📄 shiftdevicedlg.cpp

📁 在VC下能够发送任意自定义的SCSI指令的程序
💻 CPP
字号:
// ShiftDeviceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ShiftDevice.h"
#include "ShiftDeviceDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CShiftDeviceDlg dialog

CShiftDeviceDlg::CShiftDeviceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CShiftDeviceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CShiftDeviceDlg)
	m_Info = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CShiftDeviceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShiftDeviceDlg)
	DDX_Text(pDX, IDC_EDIT_INFO, m_Info);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CShiftDeviceDlg, CDialog)
	//{{AFX_MSG_MAP(CShiftDeviceDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShiftDeviceDlg message handlers

BOOL CShiftDeviceDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_Info = "";
	UpdateData(FALSE);
	SetTimer(2, 500, NULL);	

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CShiftDeviceDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CShiftDeviceDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CShiftDeviceDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == 2)
	{
		int result = 0;
		KillTimer(nIDEvent);
		m_Info += "start\r\n";
		UpdateData(FALSE);

		// 查找设备
		// 发送命令
		DoShiftDevice();

		m_Info += "end\r\n";
		UpdateData(FALSE);
		SetTimer(3, 1000, NULL);
	}
	else if (nIDEvent == 3)
	{
		KillTimer(nIDEvent);
		//PostQuitMessage(WM_CLOSE);
	}

	CDialog::OnTimer(nIDEvent);
}

//DEL int CShiftDeviceDlg::tryToSendSCSIcmd()
//DEL {
//DEL 	int k = 0;
//DEL 	TCHAR			szMoveDiskName[33];
//DEL 	DWORD			MaxDriveSet, CurDriveSet;
//DEL 	DWORD			drive;
//DEL 	TCHAR			szBuf[300];
//DEL 	HANDLE			hDevice;
//DEL 	PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
//DEL 	
//DEL 	char* p;
//DEL 	
//DEL 	CString str;
//DEL 	for(k=0; k<26; k++)
//DEL 		szMoveDiskName[k] = '\0';	
//DEL 	k = 1;		
//DEL 	MaxDriveSet = CurDriveSet = 0;
//DEL 	
//DEL 	MaxDriveSet = GetLogicalDrives();
//DEL 	CurDriveSet = MaxDriveSet;
//DEL 	for ( drive = 0; drive < 32; ++drive )  
//DEL 	{
//DEL 		if ( MaxDriveSet & (1 << drive) )  
//DEL 		{
//DEL 			DWORD temp = 1<<drive;
//DEL 			TCHAR			szDrvName[33];
//DEL 			int driveType = GetDriveType( szDrvName );
//DEL 			_stprintf( szDrvName, _T("%c:\\"), 'A'+drive );
//DEL 							
//DEL 			sprintf(szBuf, "\\\\?\\%c:", 'A'+drive);
//DEL 			hDevice = CreateFile(szBuf, GENERIC_READ,
//DEL 					FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
//DEL 				
//DEL 			if (hDevice != INVALID_HANDLE_VALUE)
//DEL 			{
//DEL 				pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
//DEL 					
//DEL 				pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
//DEL 					
//DEL 				if(GetDisksProperty(hDevice, pDevDesc))
//DEL 				{
//DEL 					CString InfoStr = "";
//DEL 					szMoveDiskName[k] = chFirstDriveFromMask(temp);
//DEL 					szMoveDiskName[0]=k;
//DEL 					k++;
//DEL 					p= (char*)pDevDesc;
//DEL 					str = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset]:"(NULL)");
//DEL 					InfoStr += " Vid:" + str + "\r\n";
//DEL 											
//DEL 					str=(pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset]:"(NULL)");
//DEL 					InfoStr += " Pid:" + str + "\r\n";
//DEL 					
//DEL 					str = (pDevDesc->ProductRevisionOffset ? &p[pDevDesc->ProductRevisionOffset] : "(NULL)");
//DEL 					InfoStr += " Ver:" + str + "\r\n";
//DEL 					
//DEL 					str = (pDevDesc->SerialNumberOffset ? &p[pDevDesc->SerialNumberOffset] : "(NULL)");
//DEL 					InfoStr += " sn:" + str + "\r\n";
//DEL 					
//DEL 					CString scrVidBuf = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset]:"(NULL)");
//DEL 					CString scrPidBuf = (pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset]:"(NULL)");
//DEL 					CString tagVidBuf = "SmartCom";
//DEL 					CString tagPidBuf = "SeamlessKey     ";
//DEL 					if (scrVidBuf == tagVidBuf & scrPidBuf == tagPidBuf)
//DEL 					{
//DEL 						str.Format("%s\r\n",szDrvName);
//DEL 						m_Info += str + InfoStr;
//DEL 						UpdateData(FALSE);
//DEL 						sendToDevice(hDevice);
//DEL 					}
//DEL 				}
//DEL 					
//DEL 				delete pDevDesc;
//DEL 				CloseHandle(hDevice);
//DEL 			}			
//DEL 		}
//DEL 	}
//DEL 	
//DEL 	if(szMoveDiskName[0]==0)
//DEL 	{
//DEL 		m_Info += "\r\nNo Disk!\r\n";
//DEL 		UpdateData(FALSE);
//DEL 	}
//DEL 	
//DEL 	return 0;
//DEL }

BOOL CShiftDeviceDlg::GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
{
	STORAGE_PROPERTY_QUERY	Query;	
	DWORD dwOutBytes;				
	BOOL bResult;					
	
	Query.PropertyId = StorageDeviceProperty;
	Query.QueryType = PropertyStandardQuery;
	
	bResult = ::DeviceIoControl(hDevice,			
		IOCTL_STORAGE_QUERY_PROPERTY,			
		&Query, sizeof(STORAGE_PROPERTY_QUERY),	
		pDevDesc, pDevDesc->Size,				
		&dwOutBytes,							
		(LPOVERLAPPED)NULL);					
	
	return bResult;
}

char CShiftDeviceDlg::chFirstDriveFromMask(ULONG unitmask)
{
	char i;
	for (i = 0; i < 26; ++i)  
	{
		if (unitmask & 0x1) 
			break;
		unitmask = unitmask >> 1;
	}
    return (i + 'A');
}


//DEL int CShiftDeviceDlg::sendToDevice(HANDLE fileHandle)
//DEL {
//DEL 	BOOL status = 0;
//DEL 	UCHAR dataBuffer[] = {0x06, 0xf5, 0x02, 0x00, 0x50, 0x68};
//DEL 	SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER sptdwb;
//DEL 	ULONG length = 0;
//DEL 	ULONG returned = 0;
//DEL 		
//DEL 	ZeroMemory(&sptdwb, sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER));
//DEL     // ZeroMemory(dataBuffer,sectorSize);
//DEL 	
//DEL     sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
//DEL     
//DEL     sptdwb.sptd.PathId = 0;
//DEL     sptdwb.sptd.TargetId = 1;
//DEL     sptdwb.sptd.Lun = 0;
//DEL     sptdwb.sptd.CdbLength = CDB6GENERIC_LENGTH;
//DEL     sptdwb.sptd.DataIn = SCSI_IOCTL_DATA_IN;
//DEL     sptdwb.sptd.SenseInfoLength = 24;
//DEL     sptdwb.sptd.DataTransferLength = 0;//sizeof(dataBuffer);
//DEL     sptdwb.sptd.TimeOutValue = 100;
//DEL     sptdwb.sptd.DataBuffer = NULL;//dataBuffer;
//DEL     
//DEL     sptdwb.sptd.SenseInfoOffset =
//DEL 		offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER,ucSenseBuf);
//DEL 	sptdwb.sptd.Cdb[0] = 0x06;
//DEL 	sptdwb.sptd.Cdb[1] = 0xf5;
//DEL 	sptdwb.sptd.Cdb[2] = 0x02;
//DEL 	sptdwb.sptd.Cdb[3] = 0x00;
//DEL 	sptdwb.sptd.Cdb[4] = 0x50;
//DEL 	sptdwb.sptd.Cdb[5] = 0x68;
//DEL 	
//DEL 	
//DEL     length = sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER);
//DEL 	
//DEL     DeviceIoControl(fileHandle,
//DEL 		IOCTL_SCSI_PASS_THROUGH_DIRECT,
//DEL 		&sptdwb,
//DEL 		length,
//DEL 		&sptdwb,
//DEL 		length,
//DEL 		&returned,
//DEL 		FALSE);
//DEL 	
//DEL 	return 0;
//DEL }

void CShiftDeviceDlg::DoShiftDevice()
{
	bool status = false;
    DWORD accessMode = 0;
	DWORD shareMode = 0;
    HANDLE fileHandle = NULL;
    unsigned char dataBuffer[] = {0x06, 0xf5, 0x02, 0x00, 0x50, 0x68};
    SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER sptdwb;
    char string[25];
    unsigned long length = 0;
	unsigned long errorCode = 0;
	unsigned long returned = 0;
	unsigned long sectorSize = 512;
	
	
    strcpy(string,"\\\\.\\G:"); 
	
    shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;  // default
    accessMode = GENERIC_WRITE | GENERIC_READ;       // default
	
    fileHandle = CreateFile(string,
		accessMode,
		shareMode,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);
	
    if (fileHandle == INVALID_HANDLE_VALUE) {
		printf("Error opening %s. Error: %d\n",
			string, errorCode = GetLastError());
		//PrintError(errorCode);
		return;
    }
	
    ZeroMemory(&sptdwb, sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER));
    // ZeroMemory(dataBuffer,sectorSize);
	
    sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
    
    sptdwb.sptd.PathId = 0;
    sptdwb.sptd.TargetId = 1;
    sptdwb.sptd.Lun = 0;
    sptdwb.sptd.CdbLength = CDB6GENERIC_LENGTH;
    sptdwb.sptd.DataIn = SCSI_IOCTL_DATA_IN;
    sptdwb.sptd.SenseInfoLength = 24;
    sptdwb.sptd.DataTransferLength = 0;//sizeof(dataBuffer);
    sptdwb.sptd.TimeOutValue = 10;
    sptdwb.sptd.DataBuffer = NULL;//dataBuffer;
    
    sptdwb.sptd.SenseInfoOffset =
		offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER,ucSenseBuf);
	sptdwb.sptd.Cdb[0] = 0x06;
	sptdwb.sptd.Cdb[1] = 0xf5;
	sptdwb.sptd.Cdb[2] = 0x02;
	sptdwb.sptd.Cdb[3] = 0x00;
	sptdwb.sptd.Cdb[4] = 0x50;
	sptdwb.sptd.Cdb[5] = 0x68;
	
	
    length = sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER);
	
    status = DeviceIoControl(fileHandle,
		IOCTL_SCSI_PASS_THROUGH_DIRECT,
		&sptdwb,
		length,
		&sptdwb,
		length,
		&returned,
		false);	
}

⌨️ 快捷键说明

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