Tuesday, October 23, 2018

#1 2018-07-30 11:59:57 am

jcwelch
Member
Registered: 2009-09-11
Posts: 50

Converting CFDictionaryRef to NSDictionary

So a while ago (Hopefully shane can remember this without drinking), I was trying to work with KextManagerCopyLoadedKextInfo(), and it wasn't working in ASOC because ASOC couldn't deal with CFDictionaryRefs at all.

If you even tried it, you got errors and no results at all, usable or not. Even with his help on the ObjC side, the data I got back wasn't terribly parseable.

But that was back in 2015, and it looks like things have changed a little.

Now, if I try: set theResultDict to current application's KextManagerCopyLoadedKextInfo(missing value, missing value), I don't get an error, (yay!) I get a pointer, which is what I expected:

«class cptr» id «data gptr00000000800B0D02006000007B5F5F434644696374696F6E6172793D7D00»

So now the question is, since I'm assuming that's a "real" pointer, is there any (relatively) easy way to get that data and shove it into an NSDIctionary.

The docs I can find on moving from CFDictionaryRef to NSDictionary are not exactly clear (enough for my wee brain yet), but what I can derive from them seems straightforward? It's dealing with the pointers that make things...interesting.

any tips/ideas?

Offline

 

#2 2018-07-30 04:17:44 pm

alldritt
Site Admin
From:: Victoria, B.C. Canada
Registered: 2002-12-12
Posts: 31
Website

Re: Converting CFDictionaryRef to NSDictionary

CFDictionaryRef pointers should be "toll-free" bridged to NSDictionary pointers, meaning that you can use the pointer in either context without any conversion.


Mark Alldritt - Late Night Software Ltd.
support@latenightsw.com | mark@macscripter.net

Offline

 

#3 2018-07-30 05:35:00 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5454

Re: Converting CFDictionaryRef to NSDictionary

Although they can be bridged as Mark describes, I've yet to see a way to do it.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2018-07-31 08:00:19 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2807
Website

Re: Converting CFDictionaryRef to NSDictionary

jcwelch wrote:

So now the question is, since I'm assuming that's a "real" pointer, is there any (relatively) easy way to get that data and shove it into an NSDIctionary.



The value returned is just an generic C pointer to an object whose class is BAImmediateData and not an CFDictionaryRef as you expect.

Applescript:

use AppleScript version "2.7" -- Code works only on High Sierra (but works on old AS version on HS)
use framework "Foundation"
use framework "CoreFoundation"
use framework "IOKit"

set theResultDict to current application's KextManagerCopyLoadedKextInfo(missing value, missing value)
set classOftheResultDict to current application's NSStringFromClass(theResultDict's |class|())
return classOftheResultDict as string

If the pointer was pointing to an CFDictionaryRef then you could indeed use NSDictionary's initWithDictionary:copyItems: to create an NSDictionary from an CFDictionaryRef. However it wouldn't be necessary because they're toll-free which means NSDictionary methods can handle CFDictionaryRef opaque types.

Update: When using CFDictionaryCreateCopy() from CoreFoundation I get an BAImmediateData type back from AppleScriptObjC as well. I was expecting at least BAGenericObject class which still allows you to send messages using AppleScriptObjC. So my conclusion is that AppleScriptObjC is handling CFDictionaryRef just as an generic C pointer, which can be anything.

While you may seem like you have found a C pointer, you're miles away at the same time because the address in «class cptr» is from another memory space and can't be coerced into an «class ocid» that easy.

Last edited by DJ Bazzie Wazzie (2018-07-31 10:52:56 am)

Offline

 

#5 2018-08-01 09:17:23 am

jcwelch
Member
Registered: 2009-09-11
Posts: 50

Re: Converting CFDictionaryRef to NSDictionary

DJ,

I wasn't trying to make assumptions on the kind of pointer, just that it looked like one :-)

But yeah, if I try initWithDictionary:copyItems, it consistently fails with unrecognized selector. Sigh. So close, and yet so far.

Offline

 

#6 2018-08-10 12:43:55 pm

jcwelch
Member
Registered: 2009-09-11
Posts: 50

Re: Converting CFDictionaryRef to NSDictionary

I did however file a bug on this with Apple. Beats doing nothing.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)