Skip to content

Commit d792739

Browse files
committed
feat: windows get device ID in node
1 parent 73707c5 commit d792739

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src-node/utils.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,45 @@ function _getMacDeviceID() {
396396
});
397397
}
398398

399+
/**
400+
* Get the Windows device ID (MachineGuid).
401+
* @returns {Promise<string|null>}
402+
*
403+
* In a Windows batch file, you can get this with:
404+
* reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid
405+
*/
406+
function _getWindowsDeviceID() {
407+
return new Promise((resolve, reject) => {
408+
exec(
409+
'reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid',
410+
{ encoding: 'utf8' },
411+
(err, stdout) => {
412+
if (err) {
413+
console.error('Failed to get Windows device ID:', err.message);
414+
return reject(err);
415+
}
416+
417+
// Example output:
418+
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography
419+
// MachineGuid REG_SZ 4c4c4544-0034-5a10-8051-cac04f305a31
420+
const match = stdout.match(/MachineGuid\s+REG_[A-Z]+\s+([a-fA-F0-9-]+)/);
421+
if (match && match[1]) {
422+
resolve(match[1].trim());
423+
} else {
424+
resolve(null);
425+
}
426+
}
427+
);
428+
});
429+
}
430+
399431
async function getDeviceID() {
400432
if (process.platform === "linux") {
401433
return _getLinuxDeviceID();
402434
} else if (process.platform === "darwin") {
403435
return _getMacDeviceID();
404436
} else if (process.platform === "win32") {
405-
return "not implemented"; //await _getWindowsDeviceID();
437+
return _getWindowsDeviceID();
406438
}
407439
throw new Error(`Unsupported platform: ${process.platform}`);
408440
}

0 commit comments

Comments
 (0)