 |
 |
Can't get a handle on Handles
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Location: London
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|