How to get volume uuid of hard-disk through a c/c++ program?

Discussion in 'Mac Programming' started by newtoqtmac, Mar 15, 2011.

  1. newtoqtmac macrumors newbie

    Joined:
    Mar 15, 2011
    #1
    Hi,
    I am very much new to mac programming. I need to get volume uuid of the device(hard disk where my mac installed) through a c/c++ program. How can i do this? I googled regarding this but i am getting commends like "ioreg" , "diskutil". But all these are commands to run in terminal. But i need to achieve this from a program. A sample piece of code is really helpful.

    Thank you in advance.
     
  2. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #2
  3. ccamelot macrumors member

    ccamelot

    Joined:
    Feb 21, 2010
    Location:
    Poland
    #3
    Use NSTask

    Hi,

    bash script which return disk UUID will look something like

    Code:
    /usr/sbin/diskutil info / | /usr/bin/awk '$0 ~ /UUID/ { print $3 }'
    save it to file with shebang at top -> #!/bin/sh
    and then use NSTask to run it and get returned value. Do you know how?

    Another example (written in mail):

    Code:
    NSTask *task;
    	task = [[NSTask alloc] init];
    	[task setLaunchPath: @"/usr/sbin/diskutil"];
    	
    	NSArray *arguments;
    	arguments = [NSArray arrayWithObjects:@"info",@"-plist",@"/",nil];
    	[task setArguments: arguments];
    	
    	NSPipe *pipe = [NSPipe pipe];
    	[task setStandardOutput: pipe];
    	
    	NSFileHandle *file = [pipe fileHandleForReading];
    	[task launch];
    
    NSData *data = [file readDataToEndOfFile];
        
        NSString *errorDesc = nil;
        NSPropertyListFormat format;
        NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                        propertyListFromData:data
                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                        format:&format errorDescription:&errorDesc];
    
    NSString *diskUUID = [temp objectForKey:@"VolumeUUID"];
    [task release];
    
    hope it helps.

    Les
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    C/C++ not Objective-C. ;)

    B
     
  5. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #5
    If this was in Obj-C it's simpler to just get it from the IOKit framework. I think there was an easy way of getting it via the DiskArbitration framework but it may have been deprecated. I don't remember.
     
  6. ccamelot macrumors member

    ccamelot

    Joined:
    Feb 21, 2010
    Location:
    Poland
    #6
    You're right

    :) he he - you're right. OK, maybe someone else will benefit from this snippet.

    all best
    Les
     
  7. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    You can use the DiskArbitration framework and kDADiskDescriptionVolumeUUIDKey.
     

Share This Page