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

📄 portdll.cpp

📁 一个可以拦截DeviceIoControl的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
int WinNTHDSerialNumAsPhysicalRead (DWORD * buffer)
{
	#define  DFP_GET_VERSION          0x00074080
	BYTE IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];

	int done = FALSE;
	int drive = 0, maxDriver = 4;

	//先获得系统盘符
	TCHAR sysPath[100];
	GetWindowsDirectory(sysPath, 100);

	DWORD diskNumber, partitionNumber;

	if( GetDiskAndPartitionNumbers( sysPath[0], &diskNumber, &partitionNumber ) )
	{
		drive = diskNumber;//物理硬盘号
		maxDriver = drive + 1;
	}

	for ( drive; drive < maxDriver; drive++)
	{
		HANDLE hPhysicalDriveIOCTL = 0;

		 //  Try to get a handle to PhysicalDrive IOCTL, report failure
		 //  and exit if can't.
		char driveName [256];

		sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive);

		 //  Windows NT, Windows 2000, must have admin rights
		hPhysicalDriveIOCTL = CreateFileA (driveName,
							   GENERIC_READ | GENERIC_WRITE, 
							   FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
							   OPEN_EXISTING, 0, NULL);
		//if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
		//    printf ("Unable to open physical drive %d, error code: 0x%lX\n",
		//            drive, GetLastError ());

		if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
		{
			GETVERSIONOUTPARAMS VersionParams;
			DWORD               cbBytesReturned = 0;

				// Get the version, etc of PhysicalDrive IOCTL
			DISK_GEOMETRY g;
			memset(&g,0,sizeof(g));
			if ( ! DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_DISK_GET_DRIVE_GEOMETRY,
					   NULL, 
					   0,
					   &g,
					   sizeof(g),
					   &cbBytesReturned, NULL) ){
				CloseHandle(hPhysicalDriveIOCTL);
				continue;
			}

			DRIVE_LAYOUT_INFORMATION layout[10];
			memset(&layout, 0, sizeof(DRIVE_LAYOUT_INFORMATION)*10 );


			if ( DeviceIoControl(hPhysicalDriveIOCTL, IOCTL_DISK_GET_DRIVE_LAYOUT,
				NULL,                        // lpInBuffer
				0,                           // nInBufferSize
				layout,        // output buffer
				sizeof(DRIVE_LAYOUT_INFORMATION)*10,      // size of output buffer
				&cbBytesReturned,   // number of bytes returned
				NULL)
				)
			{
				int count = cbBytesReturned / sizeof( DRIVE_LAYOUT_INFORMATION );
			}
			
			//判断是否为固定硬盘Fixed or Removable
			if(g.MediaType != FixedMedia)
			{
				CloseHandle(hPhysicalDriveIOCTL);
				continue;
			}

			memset ((void*) &VersionParams, 0, sizeof(VersionParams));

			if ( ! DeviceIoControl (hPhysicalDriveIOCTL, DFP_GET_VERSION,
					   NULL, 
					   0,
					   &VersionParams,
					   sizeof(VersionParams),
					   &cbBytesReturned, NULL) )
			{         
				// printf ("DFP_GET_VERSION failed for drive %d\n", i);
				// continue;
			}

				// If there is a IDE device at number "i" issue commands
				// to the device
			if (VersionParams.bIDEDeviceMap > 0)
			{
				BYTE             bIDCmd = 0;   // IDE or ATAPI IDENTIFY cmd
				SENDCMDINPARAMS  scip;
				//SENDCMDOUTPARAMS OutCmd;

				// Now, get the ID sector for all IDE devices in the system.
				   // If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,
				   // otherwise use the IDE_ATA_IDENTIFY command
				bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ? \
						  IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;

				memset (&scip, 0, sizeof(scip));
				memset (IdOutCmd, 0, sizeof(IdOutCmd));

				if ( DoIDENTIFY (hPhysicalDriveIOCTL, 
						   &scip, 
						   (PSENDCMDOUTPARAMS)&IdOutCmd, 
						   (BYTE) bIDCmd,
						   (BYTE) drive,
						   &cbBytesReturned))
				{
					PSENDCMDOUTPARAMS  p = (PSENDCMDOUTPARAMS)IdOutCmd;
					int ijk = 0;
					USHORT *pIdSector = (USHORT *)
								 ((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;

					for (ijk = 0; ijk < 256; ijk++)
						buffer[ijk] = pIdSector [ijk];
#if _DEBUG
					PrintIdeInfo (drive, buffer);
#endif
					CloseHandle (hPhysicalDriveIOCTL);
					return TRUE;
				}
			}

			CloseHandle (hPhysicalDriveIOCTL);
		}
	}

	return FALSE;
}

int WinNTHDSerialNumAsPhysicalRead2(DWORD * buffer)
{
	#define  DFP_GET_VERSION          0x00074080
	BYTE IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];

	int done = FALSE;
	int drive = 0, maxDriver = 4;

	//先获得系统盘符
	TCHAR sysPath[100];
	GetWindowsDirectory(sysPath, 100);

	DWORD diskNumber, partitionNumber;

	if( GetDiskAndPartitionNumbers( sysPath[0], &diskNumber, &partitionNumber ) )
	{
		drive = diskNumber;//物理硬盘号
		maxDriver = drive + 1;
	}

	for ( drive; drive < maxDriver; drive++)
	{
		HANDLE hPhysicalDriveIOCTL = 0;

		 //  Try to get a handle to PhysicalDrive IOCTL, report failure
		 //  and exit if can't.
		char driveName [256];

		sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive);

		 //  Windows NT, Windows 2000, must have admin rights
		hPhysicalDriveIOCTL = CreateFileA (driveName,
							   GENERIC_READ | GENERIC_WRITE, 
							   FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
							   OPEN_EXISTING, 0, NULL);
		//if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
		//    printf ("Unable to open physical drive %d, error code: 0x%lX\n",
		//            drive, GetLastError ());

		if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
		{
			GETVERSIONOUTPARAMS VersionParams;
			DWORD               cbBytesReturned = 0;

				// Get the version, etc of PhysicalDrive IOCTL
			DISK_GEOMETRY g;
			memset(&g,0,sizeof(g));
			if ( ! DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_DISK_GET_DRIVE_GEOMETRY,
					   NULL, 
					   0,
					   &g,
					   sizeof(g),
					   &cbBytesReturned, NULL) ){
				CloseHandle(hPhysicalDriveIOCTL);
				continue;
			}

			DRIVE_LAYOUT_INFORMATION layout[10];
			memset(&layout, 0, sizeof(DRIVE_LAYOUT_INFORMATION)*10 );


			if ( DeviceIoControl(hPhysicalDriveIOCTL, IOCTL_DISK_GET_DRIVE_LAYOUT,
				NULL,                        // lpInBuffer
				0,                           // nInBufferSize
				layout,        // output buffer
				sizeof(DRIVE_LAYOUT_INFORMATION)*10,      // size of output buffer
				&cbBytesReturned,   // number of bytes returned
				NULL)
				)
			{
				int count = cbBytesReturned / sizeof( DRIVE_LAYOUT_INFORMATION );
			}
			
			//判断是否为固定硬盘Fixed or Removable
			if(g.MediaType != FixedMedia)
			{
				CloseHandle(hPhysicalDriveIOCTL);
				continue;
			}

			memset ((void*) &VersionParams, 0, sizeof(VersionParams));

			if ( ! DeviceIoControl (hPhysicalDriveIOCTL, DFP_GET_VERSION,
					   NULL, 
					   0,
					   &VersionParams,
					   sizeof(VersionParams),
					   &cbBytesReturned, NULL) )
			{         
				// printf ("DFP_GET_VERSION failed for drive %d\n", i);
				// continue;
			}

				// If there is a IDE device at number "i" issue commands
				// to the device
			if (VersionParams.bIDEDeviceMap > 0)
			{
				BYTE             bIDCmd = 0;   // IDE or ATAPI IDENTIFY cmd
				SENDCMDINPARAMS  scip;
				//SENDCMDOUTPARAMS OutCmd;

				// Now, get the ID sector for all IDE devices in the system.
				   // If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,
				   // otherwise use the IDE_ATA_IDENTIFY command
				bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ? \
						  IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;

				memset (&scip, 0, sizeof(scip));
				memset (IdOutCmd, 0, sizeof(IdOutCmd));

				if ( DoIDENTIFY (hPhysicalDriveIOCTL, 
						   &scip, 
						   (PSENDCMDOUTPARAMS)&IdOutCmd, 
						   (BYTE) bIDCmd,
						   (BYTE) drive,
						   &cbBytesReturned))
				{
					PSENDCMDOUTPARAMS  p = (PSENDCMDOUTPARAMS)IdOutCmd;
					int ijk = 0;
					USHORT *pIdSector = (USHORT *)
								 ((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;

					for (ijk = 0; ijk < 256; ijk++)
						buffer[ijk] = pIdSector [ijk];
#if _DEBUG
					PrintIdeInfo (drive, buffer);
#endif
					CloseHandle (hPhysicalDriveIOCTL);
					return TRUE;
				}
			}

			CloseHandle (hPhysicalDriveIOCTL);
		}
	}

	return FALSE;
}

LPCSTR HDSerialNumRead1()
{	
	static CHAR  buffer[256];	
	memset(buffer,0,sizeof(buffer));
	OSVERSIONINFO OSVersionInfo;
	OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx( &OSVersionInfo);
	if (OSVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
	{   
		WORD m_wSeri[256];
		Win9xHDSerialNumRead(m_wSeri);  
		if(m_wSeri[10])
			strcpy (buffer, ConvertToString (m_wSeri, 10, 19));
	}
	else{
		DWORD m_wStr[256];

		/*
		if ( WinNTHDSerialNumAsPhysicalRead(m_wStr) )
			strcpy (buffer, ConvertToString2 (m_wStr, 10, 19));
		*/
		/*
		DWORD m_wStr2[256];
		char buffer2[256];
		if( WinNTHDSerialNumAsScsiRead(m_wStr2) )
			strcpy (buffer2, ConvertToString2 (m_wStr2, 10, 19));
		*/
		if ( WinNTHDSerialNumAsPhysicalRead(m_wStr) || WinNTHDSerialNumAsScsiRead(m_wStr) )
			strcpy (buffer, ConvertToString2 (m_wStr, 10, 19));
	}	

	return (LPCSTR(buffer));
}

LPCSTR HDSerialNumRead2()
{	
	static CHAR  buffer[256];	
	memset(buffer,0,sizeof(buffer));
	OSVERSIONINFO OSVersionInfo;
	OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx( &OSVersionInfo);
	if (OSVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
	{   
		WORD m_wSeri[256];
		Win9xHDSerialNumRead(m_wSeri);  
		if(m_wSeri[10])
			strcpy (buffer, ConvertToString (m_wSeri, 10, 19));
	}
	else{
		DWORD m_wStr[256];

		/*
		if ( WinNTHDSerialNumAsPhysicalRead(m_wStr) )
			strcpy (buffer, ConvertToString2 (m_wStr, 10, 19));
		*/

		/*
		DWORD m_wStr2[256];
		char buffer2[256];
		if( WinNTHDSerialNumAsScsiRead(m_wStr2) )
			strcpy (buffer2, ConvertToString2 (m_wStr2, 10, 19));
		*/

		if ( WinNTHDSerialNumAsPhysicalRead2(m_wStr) || WinNTHDSerialNumAsScsiRead(m_wStr) )
			strcpy (buffer, ConvertToString2 (m_wStr, 10, 19));
	}	

	return (LPCSTR(buffer));
}

int WEP(int nParam)
{
	return 1;
}


void PrintIdeInfo (int drive, DWORD diskdata [256])
{
      //  copy the hard driver serial number to the buffer
   //strcpy (HardDriveSerialNumber, ConvertToString (diskdata, 10, 19));

//#ifdef PRINTING_TO_CONSOLE_ALLOWED
#ifdef _DEBUG

  /*
   switch (drive / 2)
     {
        case 0: TRACE("\nPrimary Controller - ");
                break;
        case 1: TRACE ("\nSecondary Controller - ");
                break;
        case 2: TRACE ("\nTertiary Controller - ");
                break;
        case 3: TRACE ("\nQuaternary Controller - ");
                break;
     }
  
  
   switch (drive % 2)
   {
   case 0: TRACE ("Master drive\n\n");
	   break;
   case 1: TRACE ("Slave drive\n\n");
	   break;
   }
   
   char buf[256];
 
   sprintf (buf, "Drive Model Number________________: %s\n", ConvertToString2 (diskdata, 27, 46));
    TRACE(buf);
 
    sprintf (buf, "Drive Serial Number_______________: %s\n", ConvertToString2 (diskdata, 10, 19));
    TRACE(buf);
 
    sprintf (buf, "Drive Controller Revision Number__: %s\n", ConvertToString2 (diskdata, 23, 26));
    TRACE(buf);
    
    sprintf (buf, "Controller Buffer Size on Drive___: %u bytes\n",  diskdata [21] * 512);
    TRACE(buf);
    
    TRACE ("Drive Type________________________: ");
 
   if (diskdata [0] & 0x0080)
	   TRACE ("Removable\n");
   else if (diskdata [0] & 0x0040)
	   TRACE ("Fixed\n");
   else TRACE ("Unknown\n");
   
   sprintf(buf, "Physical Geometry:     "
           "%u Cylinders     %u Heads     %u Sectors per track\n",
           diskdata [1], diskdata [3], diskdata [6]);

   TRACE(buf);
*/
#endif  // PRINTING_TO_CONSOLE_ALLOWED

}

BOOL GetDiskAndPartitionNumbers(char driveLetter, DWORD *diskNumberPtr, DWORD *partitionNumberPtr)
{
	typedef  DWORD DEVICE_TYPE ;
	typedef struct _STORAGE_DEVICE_NUMBER
	{
		DEVICE_TYPE  DeviceType;
		ULONG  DeviceNumber;
		ULONG  PartitionNumber;
	} STORAGE_DEVICE_NUMBER, *PSTORAGE_DEVICE_NUMBER;

	{
		HANDLE fileHandle;
		STORAGE_DEVICE_NUMBER deviceInfo;
		DWORD bytesReturned;
		char rawDiskName[] = "\\\\.\\C:";
		
		// Open the raw disk
		rawDiskName[4] = driveLetter;
		fileHandle = CreateFileA(rawDiskName, GENERIC_READ,
			FILE_SHARE_READ | FILE_SHARE_WRITE,
			NULL, OPEN_EXISTING,
			FILE_ATTRIBUTE_NORMAL, NULL);
		if (fileHandle == INVALID_HANDLE_VALUE) return FALSE;

		// Get disk and partition number information using a device control
		// request
		if (!DeviceIoControl(fileHandle, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL,
			0, &deviceInfo, sizeof(deviceInfo), &bytesReturned,
			NULL))
			return FALSE;

		if (bytesReturned != sizeof(deviceInfo))
			return FALSE;

		// Put values into the passed pointers
		*diskNumberPtr = deviceInfo.DeviceNumber;
		*partitionNumberPtr = deviceInfo.PartitionNumber;
/*
#ifdef _DEBUG
		char msgbuf[128];
		_stprintf(msgbuf,_T("%d %d\n"), deviceInfo.DeviceNumber, deviceInfo.PartitionNumber);
		TRACE(msgbuf);
#endif*/

		CloseHandle(fileHandle);
	}

	return TRUE;
}

#endif




⌨️ 快捷键说明

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