Skip to content

Instantly share code, notes, and snippets.

@nickrussler
Last active August 29, 2015 14:09
Show Gist options
  • Save nickrussler/47acd2fd6ee316a00d26 to your computer and use it in GitHub Desktop.
Save nickrussler/47acd2fd6ee316a00d26 to your computer and use it in GitHub Desktop.
Set Windows file attribute hidden using Firefox SDK API
/**
* Sets the hidden file attribute of a file
* @param {String} filepath String holding the path of the file to hide
* @returns {Boolean} true if file attribute hidden is set, else false
*/
function setWindowsFileAttributeHidden(filepath) {
const FILE_ATTRIBUTE_HIDDEN = 0x00000002;
try {
chrome.Cu.import('resource://gre/modules/ctypes.jsm');
let lib = ctypes.open('kernel32.dll');
try {
let getFileAttributes = lib.declare('GetFileAttributesW', ctypes.winapi_abi, ctypes.uint32_t, ctypes.jschar.ptr);
let setFileAttributes = lib.declare('SetFileAttributesW', ctypes.winapi_abi, ctypes.bool, ctypes.jschar.ptr, ctypes.uint32_t);
let currentFileAttributes = getFileAttributes(filepath);
if ((currentFileAttributes & FILE_ATTRIBUTE_HIDDEN) !== FILE_ATTRIBUTE_HIDDEN) {
return setFileAttributes(filepath, currentFileAttributes | FILE_ATTRIBUTE_HIDDEN);
} else {
return true;
}
} finally {
lib.close();
}
} catch (e) {
// Error could be handled here
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment