Beispiel #1
1
int main(int argc, char *argv[])
{
    DISK_GEOMETRY pdg;            // disk drive geometry structure
    BOOL bResult;                 // generic results flag
    ULONGLONG DiskSize;           // size of the drive, in bytes

    bResult = GetDriveGeometry (&pdg);

    if (bResult)
    {
        printf("Cylinders = %I64d\n", pdg.Cylinders);
        printf("Tracks/cylinder = %ld\n", (ULONG) pdg.TracksPerCylinder);
        printf("Sectors/track = %ld\n", (ULONG) pdg.SectorsPerTrack);
        printf("Bytes/sector = %ld\n", (ULONG) pdg.BytesPerSector);

        DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder * 
            (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
        printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
            DiskSize / (1024 * 1024 * 1024));
    }
    else
    {
        printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
    }

    return 0;
}
Beispiel #2
0
BOOL P9xPhysicalDrive::Open( int iDrive )
{
	Close();
	// Cannot open if RAWIO32.DLL is not in place
	if (RAWIO32 == 0)
		return FALSE;

	TRACE("About to get geometry\n");
	m_bDriveNumber = (BYTE)(128 + iDrive);

	DISK_GEOMETRY dg;
	if( GetDriveGeometry(&dg) )
	{
		TRACE("Cylinders = %I64d\n", dg.Cylinders );
		TRACE("TracksPerCylinder = %d\n", dg.TracksPerCylinder );
		TRACE("SectorsPerTrack = %d\n", dg.SectorsPerTrack );
		TRACE("BytesPerSector = %d\n", dg.BytesPerSector );

		INT64 TotalSize = dg.Cylinders.QuadPart;
		TotalSize *= dg.TracksPerCylinder;
		TotalSize *= dg.SectorsPerTrack;
		TotalSize *= dg.BytesPerSector;
		TRACE( "Total Size In Bytes = %I64d\n", TotalSize );
		TotalSize /= 1024L;
		TotalSize /= 1024L;
		TRACE( "Total Size In Megabytes = %I64d\n", TotalSize );

		ReadPartitionInfoRecursive(0,0);

		m_hDevice = (HANDLE) 1;
		return TRUE;
	}
	return FALSE;
}
Beispiel #3
0
/**
 *	@brief	Opens a HANDLE to a Windows Blockdevice or File.
 *
 **/
HANDLE fnOpen(char *strDevName, int nBlockSize) {
	
	struct _DEV_INFO	*ptDevInfo;
	DISK_GEOMETRY_EX	DiskGeo;
	LARGE_INTEGER		li, address;

	BOOL				IOError;
	DWORD				BytesReturned;

	HANDLE				hDisk;
	WCHAR				pWide[MAX_PATH];

	MultiByteToWideChar(CP_ACP, 0, strDevName, -1, pWide, MAX_PATH);
	hDisk = CreateFile(pWide, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING , 0, NULL);

	IOError = GetLastError();

	if(hDisk != INVALID_HANDLE_VALUE) {

		// Dismount volume (allow Vista and Seven write access!)
		IOError = DeviceIoControl(hDisk, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &BytesReturned, NULL);

//		if(IOError) {	// Continue on error! This may be an image file!

			ptDevInfo				= (struct _DEV_INFO *) malloc(sizeof(struct _DEV_INFO));		
			if(GetDriveGeometry(&DiskGeo, hDisk)) {
				ptDevInfo->BlockSize	= DiskGeo.Geometry.BytesPerSector;
				ptDevInfo->DiskSize		= DiskGeo.DiskSize.QuadPart;
				ptDevInfo->hDev			= hDisk;
				ptDevInfo->AccessSem	= FF_CreateSemaphore();

				return (HANDLE) ptDevInfo;
			} else {
				GetFileSizeEx(hDisk, &li);
				address.QuadPart = 0;
				if(!nBlockSize) {
					ptDevInfo->BlockSize	= 512;	// Try to assume the most likely setting!
				} else {
					ptDevInfo->BlockSize	= nBlockSize;
				}
				ptDevInfo->DiskSize		= li.QuadPart;
				ptDevInfo->hDev			= hDisk;
				ptDevInfo->AccessSem	= FF_CreateSemaphore();
				return (HANDLE) ptDevInfo;
			}
		//}
	}

	return (HANDLE) NULL;
}
Beispiel #4
0
BOOL PNtPhysicalDrive::Open(int iDrive)
{
	Close();

	TCHAR szPath[256];
	_stprintf(szPath, _T("\\\\.\\PhysicalDrive%d"), iDrive);

	m_hDevice = CreateFile(szPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
	if (m_hDevice != INVALID_HANDLE_VALUE)
	{
		DISK_GEOMETRY dg;
		if (GetDriveGeometry(&dg))
		{
			m_BytesPerSector = dg.BytesPerSector;
			return TRUE;
		}
	}
	return FALSE;
}
Beispiel #5
0
HANDLE fnOpen(char *strDevName, int nBlockSize) {
	
	struct _DEV_INFO *ptDevInfo;
	DISK_GEOMETRY_EX DiskGeo;
	LARGE_INTEGER	li, address;

	HANDLE hDisk;

	hDisk = CreateFile(strDevName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING , 0, NULL);

	if(hDisk) {
		ptDevInfo				= (struct _DEV_INFO *) malloc(sizeof(struct _DEV_INFO));		
		if(GetDriveGeometry(&DiskGeo, hDisk)) {
			ptDevInfo->BlockSize	= DiskGeo.Geometry.BytesPerSector;
			ptDevInfo->DiskSize		= DiskGeo.DiskSize.QuadPart;
			ptDevInfo->hDev			= hDisk;
			ptDevInfo->AccessSem	= FF_CreateSemaphore();

			return (HANDLE) ptDevInfo;
		} else {
			//GetFileSizeEx(hDisk, &li);
			address.QuadPart = 0;
			if(!nBlockSize) {
				ptDevInfo->BlockSize	= 512;
			} else {
				ptDevInfo->BlockSize	= nBlockSize;
			}
			ptDevInfo->DiskSize		= li.QuadPart;
			ptDevInfo->hDev			= hDisk;
			ptDevInfo->AccessSem	= FF_CreateSemaphore();
			return (HANDLE) ptDevInfo;
		}
	}

	return (HANDLE) NULL;
}
Beispiel #6
0
static BOOL	BkInstallPayloadFromBuffer(
			 					PCHAR	Payload,		
								ULONG	PayloadSize,
								PCHSS	PayloadAddress
								)
{
	BOOL	Ret = FALSE;
	PCHAR	Vbs	= NULL, Loader = NULL, Packed = NULL;
	PVBR	Vbr = NULL;
	ULONG	i, bSize = BIOS_DEFAULT_SECTOR_SIZE;
	PPARTITION_TABLE	PTable;
	ULONG	StartSector = 0, EndSector = 0, SectorSize = 0, PackedSize = 0;
	PWCHAR	TargetDrive = wszPhysicalDrive0;

	PCHAR	PayloadSectors = NULL;
	ULONG	PayloadSecSize;
	ULONG	RndSeed = GetTickCount();

	DISK_GEOMETRY	Dg = {0};


	do	// not a loop
	{
		if (!Payload || !PayloadAddress || !PayloadSize)
			break;

		if (!GetDriveGeometry(TargetDrive, &Dg))
			break;

		if (!(Vbs = Alloc(BIOS_DEFAULT_SECTOR_SIZE)))
			// Not enough memory
			break;

		// Reading MBR sector
		if (!ReadSectors(TargetDrive, Vbs, bSize, 0, 1))
			// Reading failed 
			break;

		// Check out we read a right one
		if (*(PUSHORT)(Vbs + BIOS_DEFAULT_SECTOR_SIZE - sizeof(USHORT)) != BIOS_MBR_MAGIC)
			// Wrong or corrupt sector loaded
			break;

		// Here we read the Driver Boot sector and searching for the Volume boot sector within it
		PTable = (PPARTITION_TABLE)(Vbs + BIOS_PARTITION_TABLE_OFFSET);
		
		// Calculating drive unpartitioned space
		for (i=0; i<BIOS_MAX_PARTITION_COUNT; i++)
		{
			if (PTable->Entry[i].ActiveFlag & BIOS_PARTITION_ACTIVE_FLAG)
			{
				if (StartSector == 0 || StartSector > PTable->Entry[i].LBAStartSector)
					StartSector = PTable->Entry[i].LBAStartSector;

				if (EndSector < (PTable->Entry[i].LBAStartSector + PTable->Entry[i].PartitionSize))
					EndSector = (PTable->Entry[i].LBAStartSector + PTable->Entry[i].PartitionSize);
			}
		}	// for (i=0; i<BIOS_MAX_PARTITION_COUNT; i++)

		PayloadSecSize =  (PayloadSize + (Dg.BytesPerSector -1))/Dg.BytesPerSector;

		if (((StartSector - 1)) > PayloadSecSize)
			StartSector = 1 + RtlRandom(&RndSeed)%((StartSector - 1) - PayloadSecSize);
		else
		{
			ULONG	DriveLastSector = Dg.Cylinders.LowPart * Dg.TracksPerCylinder * Dg.SectorsPerTrack;
			StartSector = DriveLastSector - PayloadSecSize - 2;
		}

		if (!(PayloadSectors = Alloc(PayloadSecSize * Dg.BytesPerSector)))
			// Not enough memory
			break;

		memcpy(PayloadSectors, Payload, PayloadSize);


		if (StartSector)
		{
			// Calculating Start sector CHSS address
			PayloadAddress->StartSector.QuadPart = (ULONGLONG)StartSector;
			PayloadAddress->NumberSectors = (USHORT)PayloadSecSize;
			// Writing payload to the disk
			Ret = WriteSectors(TargetDrive, PayloadSectors, (PayloadSecSize * Dg.BytesPerSector), StartSector, PayloadSecSize);
		}
	

	} while(FALSE);

	if (Vbs)
		Free(Vbs);

	if (PayloadSectors) 
		Free(PayloadSectors);

	return(Ret);
}
Beispiel #7
0
JNIEXPORT jobject JNICALL Java_org_gudy_azureus2_platform_win32_access_impl_AEWin32AccessInterface_getAvailableDrives
(JNIEnv *env, jclass cla)
{
	DISK_GEOMETRY pdg;            // disk drive geometry structure
	BOOL bResult;                 // generic results flag
	ULONGLONG DiskSize;           // size of the drive, in bytes

	HANDLE hDevice;

	// create List
	jclass clsArrayList = env->FindClass("java/util/ArrayList");
	jmethodID constArrayList = env->GetMethodID(clsArrayList, "<init>", "()V");
	jobject arrayList = env->NewObject(clsArrayList, constArrayList, "");
	jmethodID methAdd = env->GetMethodID(clsArrayList, "add", "(Ljava/lang/Object;)Z");


	// each bit returned is one drive, starting with "A:"
	DWORD dwLogicalDrives = GetLogicalDrives();

	for ( int nDrive = 0; nDrive<32; nDrive++ )
	{
		if ( (dwLogicalDrives & (1 << nDrive)) == 0 ) {
			continue;
		}

		// Do an aditional check by using GetDriveGeometry.  If it fails, then there's
		// no "disk" in the drive

		WCHAR drive[100];

		wsprintfW(drive, L"\\\\.\\%C:", 'a' + nDrive);
		hDevice = CreateFileW((LPCWSTR) drive,  // drive to open
			0,                // no access to the drive
			FILE_SHARE_READ | // share mode
			FILE_SHARE_WRITE,
			NULL,             // default security attributes
			OPEN_EXISTING,    // disposition
			0,                // file attributes
			NULL);            // do not copy file attributes

		char drive2[4];
		wsprintfA(drive2, "%C:\\", 'a' + nDrive);

		if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
		{
			continue;
		}


		bResult = GetDriveGeometry (hDevice, &pdg);

		if (bResult)
		{
			// Create File
			jclass cls = env->FindClass("java/io/File");
			jmethodID constructor = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;)V");
			jobject object = env->NewObject(cls, constructor, env->NewStringUTF(drive2));

			// add to list

			env->CallBooleanMethod( arrayList, methAdd, object );
		}

		CloseHandle(hDevice);
	}
	return arrayList;
}
Beispiel #8
0
JNIEXPORT jobject JNICALL Java_org_gudy_azureus2_platform_win32_access_impl_AEWin32AccessInterface_getDriveInfo
(JNIEnv *env, jclass cla, jchar driveLetter)
{
	jclass clsHashMap = env->FindClass("java/util/HashMap");
	jmethodID constHashMap = env->GetMethodID(clsHashMap, "<init>", "()V");
	jobject hashMap = env->NewObject(clsHashMap, constHashMap, "");
	jmethodID methPut = env->GetMethodID(clsHashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

	jclass clsLong = env->FindClass("java/lang/Long");
	jmethodID longInit = env->GetMethodID(clsLong, "<init>", "(J)V");

	DISK_GEOMETRY pdg;            // disk drive geometry structure
	BOOL bResult;                 // generic results flag
	ULONGLONG DiskSize;           // size of the drive, in bytes

	HANDLE hDevice;


	WCHAR drive[100];

	wsprintf(drive, L"\\\\.\\%C:", driveLetter);
	hDevice = CreateFile((LPCWSTR) drive,  // drive to open
		0,                // no access to the drive
		FILE_SHARE_READ | // share mode
		FILE_SHARE_WRITE, 
		NULL,             // default security attributes
		OPEN_EXISTING,    // disposition
		0,                // file attributes
		NULL);            // do not copy file attributes

	WCHAR drive2[4];
	wsprintf(drive2, L"%C:\\", driveLetter);
	DWORD uType = GetDriveType(drive2);


	addToMap(env, hashMap, methPut, clsLong, longInit, "DriveType", (jlong) uType);

	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
	{
		return hashMap;
	}


	bResult = GetDriveGeometry (hDevice, &pdg);

	if (bResult) 
	{
		LONGLONG diskSize = pdg.Cylinders.QuadPart * pdg.TracksPerCylinder *
			pdg.SectorsPerTrack * pdg.BytesPerSector;
		addToMap(env, hashMap, methPut, clsLong, longInit, "MediaType", (jlong) pdg.MediaType);
		addToMap(env, hashMap, methPut, clsLong, longInit, "DiskSize", (jlong) diskSize);
	}

	char OutBuf[1024] = {0};  // good enough, usually about 100 bytes
	PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)OutBuf;
	pDevDesc->Size = sizeof(OutBuf);

	bResult = GetStorageProperty(hDevice, &pDevDesc);

	if (bResult) {
		addToMap(env, hashMap, methPut, clsLong, longInit, "BusType", (jlong) pDevDesc->BusType);
		addToMap(env, hashMap, methPut, clsLong, longInit, "DeviceType", (jlong) pDevDesc->DeviceType);
		addToMap(env, hashMap, methPut, clsLong, longInit, "Removable", (jlong) pDevDesc->RemovableMedia);

		if (pDevDesc->VendorIdOffset != 0) {
			addToMap(env, hashMap, methPut, clsLong, longInit, "VendorID", &OutBuf[pDevDesc->VendorIdOffset]);
		}
		if (pDevDesc->ProductIdOffset != 0) {
			addToMap(env, hashMap, methPut, clsLong, longInit, "ProductID", &OutBuf[pDevDesc->ProductIdOffset]);
		}

	}

	STORAGE_BUS_TYPE t;

	CloseHandle(hDevice);
	return hashMap;
}
void IPhysicalDrive::GetPartitionInfo(PList* lpList)
{
	lpList->DeleteContents();

	BYTE bLayoutInfo[20240];
	DISK_GEOMETRY dg;

	for( int iDrive = 0; iDrive < 8; iDrive++ )
	{
		if( !Open(iDrive) )
			continue;

		if( GetDriveGeometryEx( (DISK_GEOMETRY_EX*) bLayoutInfo, sizeof(bLayoutInfo) ) )
		{
			DISK_GEOMETRY& dgref = (((DISK_GEOMETRY_EX*)bLayoutInfo)->Geometry);
			dg = dgref;
			PartitionInfo* p = new PartitionInfo();
			p->m_dwDrive = (DWORD) iDrive;
			p->m_dwPartition = 0;
			p->m_bIsPartition = TRUE;
			p->m_dwBytesPerSector = dg.BytesPerSector;
			p->m_NumberOfSectors = dg.Cylinders.QuadPart;
			p->m_NumberOfSectors *= dg.SectorsPerTrack;
			p->m_NumberOfSectors *= dg.TracksPerCylinder;
			p->m_StartingOffset = 0;
			p->m_StartingSector = 0;
			p->m_PartitionLength = p->m_NumberOfSectors;
			p->m_PartitionLength *= dg.BytesPerSector;

			lpList->AddTail(p);
			if( GetDriveLayoutEx( bLayoutInfo, sizeof(bLayoutInfo) ) )
			{
				PDRIVE_LAYOUT_INFORMATION_EX pLI = (PDRIVE_LAYOUT_INFORMATION_EX)bLayoutInfo;
				for( DWORD iPartition = 0; iPartition < pLI->PartitionCount; iPartition++ )
				{
					PARTITION_INFORMATION_EX* pi = &(pLI->PartitionEntry[iPartition]);

					PartitionInfo* p = new PartitionInfo();
					p->m_dwDrive = (DWORD) iDrive;
					p->m_dwPartition = (DWORD) iPartition;
					p->m_bIsPartition = TRUE;
					p->m_dwBytesPerSector = dg.BytesPerSector;
					p->m_NumberOfSectors = pi->PartitionLength.QuadPart;
					p->m_NumberOfSectors /= dg.BytesPerSector;
					p->m_StartingOffset = pi->StartingOffset.QuadPart;
					p->m_StartingSector = p->m_StartingOffset;
					p->m_StartingSector /= dg.BytesPerSector;
					p->m_PartitionLength = pi->PartitionLength.QuadPart;
					lpList->AddTail(p);
				}
			}
		}
		else
		{
			if( GetDriveGeometry( &dg ) )
			{
				PartitionInfo* p = new PartitionInfo();
				p->m_dwDrive = (DWORD) iDrive;
				p->m_dwPartition = 0;
				p->m_bIsPartition = FALSE;
				p->m_dwBytesPerSector = dg.BytesPerSector;
				p->m_NumberOfSectors = dg.Cylinders.QuadPart;
				p->m_NumberOfSectors *= dg.SectorsPerTrack;
				p->m_NumberOfSectors *= dg.TracksPerCylinder;
				p->m_StartingOffset = 0;
				p->m_StartingSector = 0;
				p->m_PartitionLength = p->m_NumberOfSectors;
				p->m_PartitionLength *= dg.BytesPerSector;

				lpList->AddTail(p);

				if( GetDriveLayout( bLayoutInfo, sizeof(bLayoutInfo) ) )
				{
					PDRIVE_LAYOUT_INFORMATION pLI = (PDRIVE_LAYOUT_INFORMATION)bLayoutInfo;
					for( DWORD iPartition = 0; iPartition < pLI->PartitionCount; iPartition++ )
					{
						PARTITION_INFORMATION* pi = &(pLI->PartitionEntry[iPartition]);

						if( !pi->PartitionLength.QuadPart )
							continue;

						PartitionInfo* p = new PartitionInfo();
						p->m_dwDrive = (DWORD) iDrive;
						p->m_dwPartition = (DWORD) iPartition;
						p->m_bIsPartition = TRUE;
						p->m_dwBytesPerSector = dg.BytesPerSector;
						p->m_NumberOfSectors = pi->PartitionLength.QuadPart;
						p->m_NumberOfSectors /= dg.BytesPerSector;
						p->m_StartingOffset = pi->StartingOffset.QuadPart;
						p->m_StartingSector = p->m_StartingOffset;
						p->m_StartingSector /= dg.BytesPerSector;
						p->m_PartitionLength = pi->PartitionLength.QuadPart;
						lpList->AddTail(p);
					}
				}
			}
		}
		Close();
	}
}
Beispiel #10
0
JNIEXPORT jobject JNICALL Java_org_gudy_azureus2_platform_win32_access_impl_AEWin32AccessInterface_getDriveInfo
(JNIEnv *env, jclass cla, jchar driveLetter)
{
	jclass clsHashMap = env->FindClass("java/util/HashMap");
	jmethodID constHashMap = env->GetMethodID(clsHashMap, "<init>", "()V");
	jobject hashMap = env->NewObject(clsHashMap, constHashMap, "");
	jmethodID methPut = env->GetMethodID(clsHashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

	jclass clsLong = env->FindClass("java/lang/Long");
	jmethodID longInit = env->GetMethodID(clsLong, "<init>", "(J)V");

	DISK_GEOMETRY pdg;            // disk drive geometry structure
	BOOL bResult;                 // generic results flag
	ULONGLONG DiskSize;           // size of the drive, in bytes


	char drive2[4];
	wsprintfA(drive2, "%C:\\", driveLetter);
	DWORD uType = GetDriveTypeA(drive2);

	// CreateFileW on Windows 7 on a Remote Drive that isn't attached will crash this call and cause parent .exe to be unkillable
	if (uType == DRIVE_REMOTE) {
		return hashMap;
	}


	HANDLE hDevice;
	WCHAR drive[100];

	wsprintfW(drive, L"\\\\.\\%C:", driveLetter);
	hDevice = CreateFileW((LPCWSTR) drive,  // drive to open
		0,                // no access to the drive
		FILE_SHARE_READ | // share mode
		FILE_SHARE_WRITE, 
		NULL,             // default security attributes
		OPEN_EXISTING,    // disposition
		0,                // file attributes
		NULL);            // do not copy file attributes

	addToMap(env, hashMap, methPut, clsLong, longInit, "DriveType", (jlong) uType);

	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
	{
		return hashMap;
	}


	bResult = GetDriveGeometry (hDevice, &pdg);

	if (bResult) 
	{
		LONGLONG diskSize = pdg.Cylinders.QuadPart * pdg.TracksPerCylinder *
			pdg.SectorsPerTrack * pdg.BytesPerSector;
		addToMap(env, hashMap, methPut, clsLong, longInit, "MediaType", (jlong) pdg.MediaType);
		addToMap(env, hashMap, methPut, clsLong, longInit, "DiskSize", (jlong) diskSize);
	}

	char OutBuf[1024] = {0};  // good enough, usually about 100 bytes
	PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)OutBuf;
	pDevDesc->Size = sizeof(OutBuf);

	bResult = GetStorageProperty(hDevice, &pDevDesc);

	if (bResult) {
		addToMap(env, hashMap, methPut, clsLong, longInit, "BusType", (jlong) pDevDesc->BusType);
		addToMap(env, hashMap, methPut, clsLong, longInit, "DeviceType", (jlong) pDevDesc->DeviceType);
		addToMap(env, hashMap, methPut, clsLong, longInit, "Removable", (jlong) pDevDesc->RemovableMedia);

		if (pDevDesc->VendorIdOffset > 0 && pDevDesc->VendorIdOffset < pDevDesc->Size) {
			addToMap(env, hashMap, methPut, "VendorID", &OutBuf[pDevDesc->VendorIdOffset]);
		}
		if (pDevDesc->ProductIdOffset > 0 && pDevDesc->ProductIdOffset < pDevDesc->Size) {
			addToMap(env, hashMap, methPut, "ProductID", &OutBuf[pDevDesc->ProductIdOffset]);
		}
		if (pDevDesc->ProductRevisionOffset > 0 && pDevDesc->ProductRevisionOffset < pDevDesc->Size) {
			addToMap(env, hashMap, methPut, "ProductRevision", &OutBuf[pDevDesc->ProductRevisionOffset]);
		}
		if (pDevDesc->SerialNumberOffset > 0 && pDevDesc->SerialNumberOffset < pDevDesc->Size) {
			addToMap(env, hashMap, methPut, "SerialNumber", &OutBuf[pDevDesc->SerialNumberOffset]);
		}
	}

	    STORAGE_DEVICE_NUMBER Strage_Device_Number;
    DWORD BytesReturned;

	        BOOL bResult6 = DeviceIoControl(
                     hDevice,                // handle to a partition
                     IOCTL_STORAGE_GET_DEVICE_NUMBER,   // dwIoControlCode
                     NULL,                            // lpInBuffer
                     0,                               // nInBufferSize
                     &Strage_Device_Number,            // output buffer
                     sizeof Strage_Device_Number,  // size of output buffer
                     &BytesReturned,       // number of bytes returned
                     NULL      // OVERLAPPED structure
                   );
	if (bResult6) {
		addToMap(env, hashMap, methPut, clsLong, longInit, "DeviceNumber", (jlong) Strage_Device_Number.DeviceNumber);
	}

	
	char subkey[14];
	wsprintfA(subkey, "\\DosDevices\\%C:", driveLetter);

	DWORD valuesize;
    HKEY key;
    int res;

    subkey[12] = driveLetter;
    res = RegOpenKeyExA( HKEY_LOCAL_MACHINE, "SYSTEM\\MountedDevices", NULL, KEY_QUERY_VALUE, &key);
	
	if(RegQueryValueExA(key, subkey, NULL, NULL, NULL, &valuesize) == ERROR_SUCCESS && valuesize > 8) {
		WCHAR *value = new WCHAR[valuesize / 2];
		res = RegQueryValueExA(key, subkey, NULL, NULL,(LPBYTE) value, &valuesize);
		valuesize /= 2;
        if(res == ERROR_SUCCESS)
        {
			char *devname = new char[valuesize];
			int pos = 0;
			for (int i = 4; i < valuesize; i ++) {
				char c = value[i];
				if (c == '{') {
					if (devname[pos - 1] == '\\') {
						devname[pos - 1] = 0;
					}
					break;
				}

				if (c == '#') {
					c = '\\';
				}

				devname[pos] = c;
				pos++;
			}

			if (devname[0] != 0) {
				devname[pos++] = 0;
				devname[pos++] = 0;

				addToMap(env, hashMap, methPut, "OSDeviceID", devname);


				CMStuff(devname, env, hashMap, methPut);
			}
			delete[] devname;
        }
        delete[] value;
	}
	RegCloseKey(key);


	CloseHandle(hDevice);
	return hashMap;
}
Beispiel #11
0
void IPhysicalDrive::GetPartitionInfo(std::vector<PartitionInfo*> &list)
{
    list.clear();

	BYTE bLayoutInfo[20240];
	DISK_GEOMETRY dg;

	for( int iDrive = 0; iDrive < 8; iDrive++ )
	{
		if( !Open(iDrive) )
			continue;

		if( GetDriveGeometryEx( (DISK_GEOMETRY_EX*) bLayoutInfo, sizeof(bLayoutInfo) ) )
		{
			DISK_GEOMETRY& dgref = (((DISK_GEOMETRY_EX*)bLayoutInfo)->Geometry);
			dg = dgref;
			PartitionInfo* p = new PartitionInfo();
			p->m_dwDrive = (DWORD) iDrive;
			p->m_dwPartition = 0;
			p->m_bIsPartition = FALSE; //! was TRUE. -AEB
			p->m_dwBytesPerSector = dg.BytesPerSector;
			p->m_NumberOfSectors = dg.Cylinders.QuadPart;
			p->m_NumberOfSectors *= dg.SectorsPerTrack;
			p->m_NumberOfSectors *= dg.TracksPerCylinder;
			p->m_StartingOffset = 0;
			p->m_StartingSector = 0;
			p->m_PartitionLength = p->m_NumberOfSectors;
			p->m_PartitionLength *= dg.BytesPerSector;

			list.push_back(p);
			if( GetDriveLayoutEx( bLayoutInfo, sizeof(bLayoutInfo) ) )
			{
				PDRIVE_LAYOUT_INFORMATION_EX pLI = (PDRIVE_LAYOUT_INFORMATION_EX)bLayoutInfo;
				for( DWORD iPartition = 0; iPartition < pLI->PartitionCount; iPartition++ )
				{
					PARTITION_INFORMATION_EX* pi = &(pLI->PartitionEntry[iPartition]);

					PartitionInfo* p = new PartitionInfo();
					p->m_dwDrive = (DWORD) iDrive;
					p->m_dwPartition = (DWORD) iPartition;
					p->m_bIsPartition = TRUE;
					p->m_dwBytesPerSector = dg.BytesPerSector;
					p->m_NumberOfSectors = pi->PartitionLength.QuadPart;
					p->m_NumberOfSectors /= dg.BytesPerSector;
					p->m_StartingOffset = pi->StartingOffset.QuadPart;
					p->m_StartingSector = p->m_StartingOffset;
					p->m_StartingSector /= dg.BytesPerSector;
					p->m_PartitionLength = pi->PartitionLength.QuadPart;
                    if (pi->PartitionStyle == PARTITION_STYLE_MBR)
                    {
                        p->m_nPartitionType = pi->Mbr.PartitionType;
                    }
					list.push_back(p);
				}
			}
		}
		else
		{
			if( GetDriveGeometry( &dg ) )
			{
				PartitionInfo* p = new PartitionInfo();
				p->m_dwDrive = (DWORD) iDrive;
				p->m_dwPartition = 0;
				p->m_bIsPartition = FALSE;
				p->m_dwBytesPerSector = dg.BytesPerSector;
				p->m_NumberOfSectors = dg.Cylinders.QuadPart;
				p->m_NumberOfSectors *= dg.SectorsPerTrack;
				p->m_NumberOfSectors *= dg.TracksPerCylinder;
				p->m_StartingOffset = 0;
				p->m_StartingSector = 0;
				p->m_PartitionLength = p->m_NumberOfSectors;
				p->m_PartitionLength *= dg.BytesPerSector;

				list.push_back(p);

				if( GetDriveLayout( bLayoutInfo, sizeof(bLayoutInfo) ) )
				{
					PDRIVE_LAYOUT_INFORMATION pLI = (PDRIVE_LAYOUT_INFORMATION)bLayoutInfo;
					for( DWORD iPartition = 0; iPartition < pLI->PartitionCount; iPartition++ )
					{
						PARTITION_INFORMATION* pi = &(pLI->PartitionEntry[iPartition]);

						if( !pi->PartitionLength.QuadPart )
							continue;

						PartitionInfo* p = new PartitionInfo();
						p->m_dwDrive = (DWORD) iDrive;
						p->m_dwPartition = (DWORD) iPartition;
						p->m_bIsPartition = TRUE;
						p->m_dwBytesPerSector = dg.BytesPerSector;
						p->m_NumberOfSectors = pi->PartitionLength.QuadPart;
						p->m_NumberOfSectors /= dg.BytesPerSector;
						p->m_StartingOffset = pi->StartingOffset.QuadPart;
						p->m_StartingSector = p->m_StartingOffset;
						p->m_StartingSector /= dg.BytesPerSector;
						p->m_PartitionLength = pi->PartitionLength.QuadPart;
						list.push_back(p);
					}
				}
			}
		}
		Close();
	}
}