Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Can't get a handle on Handles

Can't get a handle on Handles
Thread Tools
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 9, 2003, 09:39 AM
 
This is no doubt a very simple Q - sorry for being thick, I've tried googling, but can't find the answer:

I am doing some QuickTime stuff and have to pass a handle as a parameter - then once the function is complete I can get info from the Handle.

I'm not really familiar with Handles - although I remember being told that they are pointers to pointers.

Anyway - here's my code:

Code:
infoErr = GetComponentInfo ((Component)componentInstance , &returnedDescription, componentNameHandle, nil , nil); if (noErr != infoErr) { NSLog(@"there was an error getting component info: %d", infoErr); } else { [manufacturerTF setStringValue: NSFileTypeForHFSTypeCode(returnedDescription.componentManufacturer)]; [subTypeTF setStringValue: NSFileTypeForHFSTypeCode(returnedDescription.componentSubType)]; NSData *myData = [NSData dataWithBytes: &componentNameHandle length: GetHandleSize ( componentNameHandle)]; // NSLog(@"Component Name: %@", [NSString stringWithCString: (const char *)&componentNameHandle // length: GetHandleSize ( componentNameHandle) ] ); NSLog(@" myData: %@", myData);
According to the docs the handle I am trying to get the data out of should contain the name of the component I am querying.

However I just get junk:

2003-06-09 15:35:53.609 MyFirstQTApp[4006] myData: <000f1ecc 40280000 00>


Am I misunderstanding the whole Handle thing - can I just de-reference the handle like so: &handle to get a pointer?

I couldn't find anything in the Carbon Memory docs about converting Handles to pointers - only creating, disposing and getting the size.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 9, 2003, 10:34 AM
 
OK - I was confusing & and *

* is the value of
& is the address of
     
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status: Offline
Reply With Quote
Jun 9, 2003, 10:38 AM
 
wouldn't deferencing the handle be *handle, not &handle? anyway, I think the answer is yes

here's how I get strings from handles

if (GetHandleSize(handle))
char *s = (char *)*handle;

and to put them back in to handles

BlockMoveData(string, *handle, stringLength);

also I think you you view the contents of a handle in the PB debugger if you just keep expanding those disclosure triangles
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jun 9, 2003, 04:55 PM
 
Handles are, indeed, pointers to pointers. So, to dereference the handle and get a pointer, use *myHandle. To dereference it twice and get the first byte contained in the Handle, use **myHandle. If the handle points to a structure, you can use (*myHandle)->structureElement.

But, the tricky part is that the pointer being pointed to can move around in memory, and the OS keeps track of the pointers being used in Handles. The first part means that if there's any chance that the pointer may move, you need to lock the handle before dereferencing it. In practice, it's probably safest to just always lock it. Here's how to do it:
Code:
SInt16 oldState; oldState = HGetState(myHandle); HLock(myHandle); // dereference the handle and do stuff with it HSetState(myHandle, oldState);
Finally, since the Mac keeps track of Handles you've allocated, you can't just create a pointer to a pointer using malloc and call that a Handle, you need to use NewHandle (or some other Mac Toolbox call that creates a more specific kind of Handle). Likewise, to delete a Handle, use DisposeHandle (or some other Mac Toolbox call to delete a more specific kind of Handle).
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 9, 2003, 05:39 PM
 
Thanks for the explanation Smeger - I didn't know I had to lock the handle.
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jun 10, 2003, 11:32 AM
 
I was under the impression that handles actually DON'T move on OS X, and that HLock et al were NOPs? I could be wrong, never paid that much attention to Carbon ugliness
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jun 10, 2003, 07:54 PM
 
Originally posted by Angus_D:
I was under the impression that handles actually DON'T move on OS X, and that HLock et al were NOPs? I could be wrong, never paid that much attention to Carbon ugliness
I haven't heard this, but it'd make sense given how virtual memory works in OS X. Anyone got a definitive source for this?
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:40 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2