我试图帮助某人,所以我写了一些ctypes来检查工作站是否被锁定。
我成功获取此信息,但我无法从字典中读取值。
这段代码运行完美,直到我需要将获得的值转换为字符串。请参阅评论//read value
我在评论中添加了什么错误。
Cu.import('resource://gre/modules/ctypes.jsm');
var lib = {
CoreGraphics: '/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics',
CoreFoundation: '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation'
};
for (var l in lib) {
lib[l] = ctypes.open(lib[l]);
}
//start mactypes
var Boolean = ctypes.unsigned_char;
var UniChar = ctypes.jschar; // uint16 with automatic conversion
//end mactypes
//start CoreFoundationTypes
var CFTypeRef = ctypes.void_t.ptr;
var __CFString = new ctypes.StructType('__CFString');
var CFStringRef = __CFString.ptr;
var __CFAllocator = new ctypes.StructType('__CFAllocator');
var CFAllocatorRef = __CFAllocator.ptr;
var CFIndex = ctypes.long;
var CFRange = new ctypes.StructType('CFRange', [{location: CFIndex}, {length: CFIndex}]);
//end CoreFoundationTypes
//dictionary functionality
var __CFDictionary = new ctypes.StructType('__CFDictionary');
var CFDictionaryRef = __CFDictionary.ptr;
var CFDictionaryGetValue = lib.CoreFoundation.declare('CFDictionaryGetValue', ctypes.default_abi, ctypes.void_t.ptr/*returns CFTypeRef*/, CFDictionaryRef, ctypes.void_t.ptr/*CFStringRef*/);
//end dictionary functionality
//string functionality
var CFStringCreateWithCharacters = lib.CoreFoundation.declare('CFStringCreateWithCharacters', ctypes.default_abi, CFStringRef, CFAllocatorRef, UniChar.ptr, CFIndex);
var CFStringGetLength = lib.CoreFoundation.declare('CFStringGetLength', ctypes.default_abi, CFIndex, CFStringRef);
var CFStringGetCharacters = lib.CoreFoundation.declare('CFStringGetCharacters', ctypes.default_abi, ctypes.void_t, CFStringRef, CFRange, UniChar.ptr);
//end string functionality
//common declares
var CFRelease = lib.CoreFoundation.declare('CFRelease', ctypes.default_abi, ctypes.void_t, CFTypeRef);
function convertCFString(CFStr) { //CFStr is something like that is returned by `CFStringCreateWithCharacters` see: https://github.com/philikon/osxtypes/blob/b359c655b39e947d308163994f7cce94ca14d98f/README.txt#L20
//start from: https://github.com/philikon/osxtypes/blob/b359c655b39e947d308163994f7cce94ca14d98f/README.txt#L22
var length = CFStringGetLength(CFStr);
var chars = ctypes.jschar.array(length)()
CFStringGetCharacters(CFStr, CFRange(0, length), chars);
var back = chars.readString();
//end from: //start from: https://github.com/philikon/osxtypes/blob/b359c655b39e947d308163994f7cce94ca14d98f/README.txt#L22
return back;
}
function makeCFStr(input) { //input is just a js string so like `var input = 'blah';`
return CFStringCreateWithCharacters(null, input, input.length); //see: https://github.com/philikon/osxtypes/blob/b359c655b39e947d308163994f7cce94ca14d98f/README.txt#L20
}
//end common declares
var CGSessionCopyCurrentDictionary = lib.CoreGraphics.declare('CGSessionCopyCurrentDictionary', ctypes.default_abi, CFDictionaryRef);
var CGSessionDict = CGSessionCopyCurrentDictionary();
console.log(uneval(CGSessionDict));
var kCGSSessionOnConsoleKey_str = 'kCGSSessionOnConsoleKey';
var kCGSSessionOnConsoleKey = CFStringCreateWithCharacters(null, kCGSSessionOnConsoleKey_str, kCGSSessionOnConsoleKey_str.length); //works // i figured it should be a string because of: https://github.com/JuliaEichler/Mac_OSX_SDKs/blob/392649d7112884481a94b8cd1f601f3a5edae999/MacOSX10.6.sdk/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Headers/CGSession.h#L38 here they are making CFSTR of a text string
var kCGSSessionOnConsoleKey_val = CFDictionaryGetValue(CGSessionDict, kCGSSessionOnConsoleKey);
console.log('kCGSSessionOnConsoleKey_val:', kCGSSessionOnConsoleKey_val, uneval(kCGSSessionOnConsoleKey_val)); //printing `"kCGSSessionOnConsoleKey_val:" CData { } "ctypes.voidptr_t(ctypes.UInt64("0x7fff7a13b7f0"))"`
if (kCGSSessionOnConsoleKey_val.isNull()) {
console.log('CFDictionaryGetValue isNull so the key is not present in the dictionary, I am guesing');
} else {
//read value
var kCGSSessionOnConsoleKey_val_str = convertCFString(kCGSSessionOnConsoleKey_val); //throwing `Exception: expected type pointer, got ctypes.voidptr_t(ctypes.UInt64("0x7fff7a13b7f0"))`
}
CFRelease(kCGSSessionOnConsoleKey); //do release on things made with CFStringCreateWithCharacters per https://github.com/philikon/osxtypes/blob/master/examples/content/iphoto.js#L89
for (var l in lib) {
lib[l].close();
}
所以基本上在这个convertCFString
中有一个函数CFStringGetLength
,它取CFStringRef
的第一个参数,定义为new ctypes.void_t.ptr
。我在CFDictionaryGetValue
的返回值上运行此函数,此函数的返回值也是ctypes.void_t.ptr
,它前面没有new
。< / p>
以下是convertCFString
(以及CFStringGetLength
)工作的示例:
var kCGSSessionOnConsoleKey_str = 'kCGSSessionOnConsoleKey';
var kCGSSessionOnConsoleKey = CFStringCreateWithCharacters(null, kCGSSessionOnConsoleKey_str, kCGSSessionOnConsoleKey_str.length);
var str = convertCFString(kCGSSessionOnConsoleKey);
console.log('str:', str);
我试过像这样投射:
var casted = ctypes.cast(kCGSSessionOnConsoleKey_val, CFStringRef.ptr).contents;
这使它看起来与__CFString.ptr(ctypes.UInt64("0x7fff7a13b7f0"))
完全相同,但当我对此进行convertCFString
时,它会崩溃firefox。
答案 0 :(得分:0)
想出来,我错了。它也是一个布尔值,所以我不得不把它转换为CFBooleanRef:
var casted = ctypes.cast(kCGSSessionOnConsoleKey_val, CFBooleanRef);
解决方案在这里:https://gist.github.com/Noitidart/03052979069adc80a00c