Last active
November 19, 2015 08:25
-
-
Save skyksandr/46a90f47fcaa38bd7cd9 to your computer and use it in GitHub Desktop.
Handle usb drive attach/detach with device path in electron (atom-shell) with hookWindowMessage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function setWindowsObserver(callback, mainWindow) { | |
| var ref = require('ref'); | |
| var StructType = require('ref-struct'); | |
| var DEV_BROADCAST_VOLUME = StructType({ | |
| dbcv_size: ref.types.uint32, // DWORD dbcv_size; | |
| dbcv_devicetype: ref.types.uint32, // DWORD dbcv_devicetype; | |
| dbcv_reserved: ref.types.uint32, // DWORD dbcv_reserved; | |
| dbcv_unitmask: ref.types.uint32, // DWORD dbcv_unitmask; | |
| dbcv_flags: ref.types.short // WORD dbcv_flags; | |
| }); | |
| mainWindow.hookWindowMessage( | |
| Number.parseInt('0x0219'), // WM_DEVICECHANGE | |
| function(w_param, l_param_pointer) { | |
| var event_name; | |
| if (w_param.readUInt32LE(0) == 0x8000) { // DBT_DEVICEARRIVAL | |
| event_name = 'mounted'; | |
| } else if (w_param.readUInt32LE(0) == 0x8004) { // DBT_DEVICEREMOVECOMPLETE | |
| event_name = 'unmounted' | |
| } | |
| if (!event_name) return; | |
| var l_param = ref.readPointer(l_param_pointer, 0, DEV_BROADCAST_VOLUME.size); | |
| l_param.type = DEV_BROADCAST_VOLUME; | |
| var val = l_param.deref(); | |
| var device_path = getDriveLetterFromUnitmask(val.dbcv_unitmask) + ":\\"; | |
| callback(event_name, device_path); | |
| } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment