Open USB Drive as File using NSWorkplace in Objective-C

Discussion in 'Mac Programming' started by musharraf, Jan 25, 2012.

  1. macrumors newbie

    Joined:
    Jan 25, 2012
    #1
    I am writing an application in MAC-OS using Objective-C , to read Boot-Sector of USB Drive and just to modify some bytes in it.

    I created object of NSWorkspace, but problem is that how can pass USB-Drive Path in openFile methods.



    Thanks
     
  2. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    Did you mean NSWorkspace ?

    http://developer.apple.com/library/...es/NSWorkspace_Class/Reference/Reference.html

    What have you tried that hasn't worked ? Are you sure this is the class you want for what you want to do ? What exactly are you trying to achieve where you think you need to modify some bytes in the boot sector of a USB flash drive ?

    To modify a device at such a level under Unix, you usually require access to the character device special file which will present you the device in "raw" mode. Under OS X, I believe these files will be /dev/rdisk# where # is replaced by the disk number.

    NSWorkspace doesn't sound like what you need at all, except maybe to get a list of mounted media using the - (NSArray *)mountedRemovableMedia method. But frankly, this won't give you the DSF for the USB media. openFile doesn't do what you think it does, it basically acts as if you've double clicked a file.

    You probably need to look a bit deeper here, the I/O Kit API is probably more in-tune with what you want to do, along with the I/O Registry :

    http://developer.apple.com/library/.../AH_Family_Reference/AH_Family_Reference.html

    Once you have the info for the USB device, just go for standard ANSI C or POSIX stuff to manipulate the bits in the file (fopen()/open(), fseek(), fprintf()/fputc(), fclose()/close()).
     
  3. thread starter macrumors newbie

    Joined:
    Jan 25, 2012
    #3
    Open USB Drive as File using NSWorkplace in Objective-C

    Hi,

    In Windows I am using "CreateFile" API to get handle USB DRIVE e.g( H:\\ etc.) and using "DeviceIoControl" to mount and Unmount USB Drive to update its Boot Sector.

    This same functionality I want to use in MAC-OS. I can open using ANSI-C functions but Mount/Unmount... how can be.


    Thanks
     
  4. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #4
    Did you read my second link ? That's a pretty hefty document I pointed to (the I/O kit reference). It should contain everything you need to know. But again I question why you feel the need to even modify the boot sector of a USB drive ?
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Use DiskArbitration framework to get local drives. Then feed the kDADiskDescriptionMediaBSDNameKey of the disk to opendev().

    You can also then use DADiskUnmount(), DADiskMount(), and DADiskEject().
     
  6. macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #6

Share This Page