2013-07-27 46 views
2

MacRuby Pointer to typedef struct提领的时候,我学会了如何与非关联MacRuby的指针,参考,使用Cocoa框架

x=Pointer.new_with_type 
... 
==> use x.value, or x[0] 

创建的指针作品一种享受!

现在我想了解我认为是“相反”的东西。我正在尝试使用此API。

OSStatus SecKeychainCopySettings (
    SecKeychainRef keychain, 
    SecKeychainSettings *outSettings 
); 

第二个参数必须是指针。但我从来没有设法打开钥匙串的真正outSettings,我只获得默认设置。

framework 'Security' 
keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}') 
SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject) 

#attempt #1 
settings=Pointer.new_with_type('{SecKeychainSettings=IBBI}') 
SecKeychainCopySettings(keychainObject.value, settings) 
p settings.value 
#<SecKeychainSettings version=0 lockOnSleep=false useLockInterval=false lockInterval=0> 

#attempt #2 
settings2=SecKeychainSettings.new 
result = SecKeychainCopySettings(keychainObject.value, settings2) 
p settings2 
#<SecKeychainSettings version=0 lockOnSleep=false useLockInterval=false lockInterval=0> 

钥匙串的设置应打开阅读

#<SecKeychainSettings version=0 lockOnSleep=true useLockInterval=true lockInterval=1800> 

我缺少什么?

回答

0

Got it! 的文档,以SecKeychainCopySettings提到

outSettings 在返回时,指向一个钥匙串的设置结构。 由于此结构已进行版本控制,因此必须为其分配内存,并在将其传递给 函数之前填写结构版本。

所以我们不能只创建一个指向SecKeychainSettings的指针。我们必须将指针指向的Struct的版本设置为某个值。

settings=Pointer.new_with_type('{SecKeychainSettings=IBBI}') 
#settings[0] dereferences the Pointer 
#for some reason, settings[0][0]=1 does not work, nor settings[0].version=1 
settings[0]=[1,false,false,0] 
#we are redefining the complete SecKeychainSettings struct 
# [0]=version [1]=lockOnSleep [2]=useLockInterval [3]=lockInterval 
result = SecKeychainCopySettings(keychainObject.value, settings) 
p settings 
=> #<SecKeychainSettings version=1 lockOnSleep=true useLockInterval=false lockInterval=300> irb(main):019:0>