Re: Changes to System.DeviceAttached/System.DeviceRemoved
Posted: Mon Mar 06, 2017 5:40 pm
Yeah I know...
This is a big learning thing for me... and Microsoft has some back asswards ways of doing things. and it really makes no sense as to why some of the things are the way they are.. but I would imagine it is because of rears af adding and adding and adding.. but sometimes you need to redesign the way something works. and make a means to use an old API with the newly designed addition. instead of just adding onto it. the process of getting something simple like a drive letter for a specific disk drive is bonkers. I have to go through 4 different WMI queries passing a piece of information from the previous query to it
I have attached a complete copy of the system plugin.. it has the new device change notifier as well as the power notifications.
YOU HAVE TO PUT THE PREVIOUSLY ATTACHED WindowsVersion.py file into your eg/Classes folder for this to work.
I am am still running into an issue with the network mapped drives. but This does have a working GetDevices action.. tho the help system in it seems to have some kind of a glitch. the glitch doesn't stop it from working it just appends a bunch of garbage at the end of the help and then seems show a help file that is unrelated. but when you see the garbage.. that is the end of the help file for that device type.
this is all just for a drive letter for mounting and unmounting drives
it's CRAZY!!! ALL OF THIS JUST FOR A FRIGGIN LETTER!!!
This is a big learning thing for me... and Microsoft has some back asswards ways of doing things. and it really makes no sense as to why some of the things are the way they are.. but I would imagine it is because of rears af adding and adding and adding.. but sometimes you need to redesign the way something works. and make a means to use an old API with the newly designed addition. instead of just adding onto it. the process of getting something simple like a drive letter for a specific disk drive is bonkers. I have to go through 4 different WMI queries passing a piece of information from the previous query to it
I have attached a complete copy of the system plugin.. it has the new device change notifier as well as the power notifications.
YOU HAVE TO PUT THE PREVIOUSLY ATTACHED WindowsVersion.py file into your eg/Classes folder for this to work.
I am am still running into an issue with the network mapped drives. but This does have a working GetDevices action.. tho the help system in it seems to have some kind of a glitch. the glitch doesn't stop it from working it just appends a bunch of garbage at the end of the help and then seems show a help file that is unrelated. but when you see the garbage.. that is the end of the help file for that device type.
this is all just for a drive letter for mounting and unmounting drives
it's CRAZY!!! ALL OF THIS JUST FOR A FRIGGIN LETTER!!!
Code: Select all
def GetDrives(self):
logicalDisks = self.wmi.ExecQuery(
"Select * from Win32_LogicalDisk"
)
for disk in logicalDisks:
if disk.DriveType == 2: # Removable
suffix = []
storedDrives = {}
drives = {}
elif disk.DriveType == 3: # Local
suffix = ['Drive']
storedDrives = self.localDrives
diskDrives = self.wmi.ExecQuery(
"Select * from Win32_DiskDrive"
)
drives = {drive.DeviceID: drive for drive in diskDrives}
elif disk.DriveType == 4: # Network
suffix = ['NetworkDrive']
storedDrives = self.networkDrives
mappedDrives = self.wmi.ExecQuery(
"Select * from Win32_MappedLogicalDisk"
)
drives = {drive.DeviceID: drive for drive in mappedDrives}
elif disk.DriveType == 5: # Compact
suffix = ['CD-Rom']
storedDrives = self.cdromDrives
cdromDrives = self.wmi.ExecQuery(
"Select * from Win32_CDROMDrive"
)
drives = {drive.DeviceID: drive for drive in cdromDrives}
elif disk.DriveType == 6: # Ram
suffix = []
storedDrives = {}
drives = {}
else:
suffix = []
storedDrives = {}
drives = {}
yield suffix, storedDrives, drives
def UnmountDrive(self, letter):
for suffix, storedDrives, drives in self.GetDrives():
if 'Drive' in suffix:
suffix.append('Unmounted')
elif 'NetworkDrive' in suffix:
suffix.append('Detached')
else:
suffix.append('Ejected')
for deviceId in storedDrives.keys():
drive, payload = storedDrives[deviceId]
if deviceId in drives:
continue
suffix.extend([
payload['name'],
payload['drive_letter'][:-1]
])
if suffix[-1] == letter:
self.plugin.TriggerEvent(
'.'.join(suffix),
payload
)
del (storedDrives[deviceId])
def MountDrive(self, letter=None):
for suffix, storedDrives, drives in self.GetDrives():
for deviceId in drives.keys():
if deviceId in storedDrives:
continue
if 'CD-Rom' in suffix:
cdrom = drives[deviceId]
if cdrom.MediaLoaded:
suffix.append('Inserted')
payload = dict(
drive_letter=cdrom.Drive,
max_size=cdrom.MaxMediaSize,
media_type=cdrom.MediaType,
manufacturer=cdrom.Manufacturer,
size=cdrom.Size,
status=cdrom.Status,
name=cdrom.Caption,
)
suffix.extend([
cdrom.Caption,
letter
])
if cdrom.Drive[:-1] == letter or letter is None:
storedDrives[deviceId] = (
cdrom,
payload
)
if letter is not None:
self.plugin.TriggerEvent(
'.'.join(suffix),
payload
)
elif 'NetworkDrive' in suffix:
nDrive = drives[deviceId]
suffix.append('Attached')
payload = dict(
network_path=nDrive.ProviderName,
name=nDrive.ProviderName.replace('\\', '\\\\'),
volume_name=nDrive.VolumeName,
size=nDrive.Size,
drive_letter=nDrive.Name,
free_space=nDrive.FreeSpace
)
suffix.extend([
nDrive.ProviderName.replace('\\', '\\\\'),
letter
])
if nDrive.Name[:-1] == letter or letter is None:
storedDrives[deviceId] = (
nDrive,
payload
)
if letter is not None:
self.plugin.TriggerEvent(
'.'.join(suffix),
payload
)
else:
drive = drives[deviceId]
partitionQuery = ASSOCIATORS % (
'DiskDrive',
deviceId.replace('\\', '\\\\'),
'DiskDriveToDiskPartition'
)
for partition in self.wmi.ExecQuery(partitionQuery):
diskQuery = ASSOCIATORS % (
'DiskPartition',
partition.DeviceID,
'LogicalDiskToPartition'
)
for disk in self.wmi.ExecQuery(diskQuery):
suffix.append('Mounted')
payload = dict(
drive_letter=disk.DeviceID,
free_space=disk.FreeSpace,
size=disk.Size,
volume_name=disk.VolumeName,
name=drive.Caption
)
suffix.extend([
drive.Caption,
letter
])
if disk.DeviceID[:-1] == letter or letter is None:
storedDrives[deviceId] = (
disk,
payload
)
if letter is not None:
self.plugin.TriggerEvent(
'.'.join(suffix),
payload
)
