Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VijayKumar

macrumors newbie
Original poster
Hi,

Can someone please help me to obtain programmatically (preferably C) the volume name of a usb storage device that is connected to a Mac (10.4) system?

Thanks in advance,
Vijay
 
You can use this (C and Carbon.framework):

Code:
    // Iterate across all mounted volumes
    for (volIndex=1; noErr == result || nsvErr != result; volIndex++) {
        FSVolumeRefNum 	volRef;
        HFSUniStr255 	volName;
        FSVolumeInfo 	volInfo;
        
        bzero((void *) &volInfo, sizeof(volInfo));
        result = FSGetVolumeInfo(kFSInvalidVolumeRefNum, 
                                volIndex,
                                &volRef,
                                kFSVolInfoFSInfo + kFSVolInfoSizes + kFSVolInfoFlags,
                                &volInfo,
                                &volName,
                                NULL);
    }

Volume name will be in volName for each volume found. Volume names are unicode-based and the HFSUniStr255 is a special string format used only here, you must convert it to something else for more wide-spread use in Mac OS X.

You can make an NSString (Objective-C Cocoa framework from volName using:

Code:
[NSString stringWithCharacters:volName.unicode length:volName.length]

And similar for a CFStringRef for C Carbon framework.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.