コード例 #1
0
ファイル: aedevice.cpp プロジェクト: alevy/comet
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;
}
コード例 #2
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;
}