使用Cocoa Frameworks时,MacRuby指针,引用,取消引用 [英] MacRuby pointer, referencing, dereferencing when using Cocoa Frameworks
问题描述
在 MacRuby指向typedef结构的指针中,我学习了如何取消引用创建的指针
On MacRuby Pointer to typedef struct, I learnt how to dereference a pointer created with
x=Pointer.new_with_type
...
==> use x.value, or x[0]
运行一下!
现在我想学习我相信是相反的。我尝试使用此API。
Now I want to learn what I believe to be the "opposite". I'm trying to use this API.
OSStatus SecKeychainCopySettings (
SecKeychainRef keychain,
SecKeychainSettings *outSettings
);
第二个参数必须是指针。但我从来没有设法得到真正的outSettings的钥匙串打开,我只得到默认设置。
Second parameter must be a Pointer. But I never manage to get the real outSettings of the keychain opened, I only get the default settings.
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>
我缺少什么?
推荐答案
SecKeychainCopySettings提供的文档
Got it ! The doc to SecKeychainCopySettings mentions
outSettings
返回时,指向钥匙串设置结构的指针。
由于这个结构是版本化的,你必须为它分配内存
,并在将结构传递给
函数之前填写结构的版本。
outSettings On return, a pointer to a keychain settings structure. Since this structure is versioned, you must allocate the memory for it and fill in the version of the structure before passing it to the function.
所以我们不能创建一个指向SecKeychainSettings的指针。我们必须设置指针指向的struct的版本。
So we can't just create a Pointer to SecKeychainSettings. We must set the version of the Struct that is pointed by the pointer to to something.
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>
这篇关于使用Cocoa Frameworks时,MacRuby指针,引用,取消引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!