Setting and Retrieving a Volume Label in C
  
PSS ID Number: Q117871
Article last modified on 01-24-1995
 
1.00 1.50
 
WINDOWS
 

----------------------------------------------------------------------
The information in this article applies to:
 
 - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
----------------------------------------------------------------------
 
SUMMARY
=======
 
The ability to change the volume label of a drive is not provided by the C
Run Time (CRT). This involves the use of extended file-control block (FCB).
One of the ways to use FCB file manipulation is through MS-DOS interrupt
21h, functions 13H and 16h.
 
MORE INFORMATION
================
 
The code below can be used to change a drive's volume name. The code first
sets up an extended FCB structure to allow manipulation of files with
attributes. The member attribute of the FCB structure is set to "8h" to
specify a volume. The old volume is searched for with "*.*" as a pattern
and then deleted; the volume is then recreated with the new volume label
obtained from the caller.
 
Sample Code
-----------
 
/*  Compile options needed: none
*/
 
    #include "dos.h"
    #include "stdio.h"
    #include "string.h"
 
//  Call this function with
//      cDrive = Drive number ( '\x01', '\x02'... )
//      szName = Up to 11 chars. DOS filename
 
    void change_label(char drive, char *newname)
    {
        union _REGS inregs, outregs;
        struct _SREGS segregs;
 
        typedef struct FCBTAG  {
                char extSignature;
                char Reserved[5];
                char Attribute;
                char fcbDriveID;
                char fcbFileName[8];
                char fcbExtent[3];
                short fcbCurBlockNo;
                short fcbRecSize;
                long fcbFileSize;
                short fcbFileDate;
                short fcbFileTime;
                char fcbReserved[8];
                char fcbCurRecNo;
                long fcbRandomRecNol;
        } FCB;
 
        FCB  __far rec;
 
        FCB __far *ptr = &rec;
 
        rec.fcbDriveID = drive;
        rec.extSignature = '\xff';
        rec.Attribute = '\x08';
 
        _fmemcpy(rec.fcbFileName, "*       ",11);      //delete old label
 
        _fmemcpy(rec.fcbExtent, "*  ",11);
 
        inregs.x.dx = _FP_OFF(ptr);
        inregs.h.ah = 0x13;
        segregs.ds = _FP_SEG(ptr);
        _intdosx(&inregs, &outregs, &segregs);
 
        inregs.x.dx = _FP_OFF(ptr);                    //create a new label
        segregs.ds = _FP_SEG(ptr);
 
        _fmemset(rec.fcbFileName, ' ', 11);
        _fmemcpy(rec.fcbFileName, newname, strlen(newname));
 
        inregs.h.ah = 0x16;
 
        _intdosx(&inregs, &outregs, &segregs);
 
        inregs.h.ah = 0x10;                         //close file with FCB
        _intdosx(&inregs, &outregs, &segregs);
    }
 
REFERENCES
==========
 
For additional information, please see the following article in the
Microsoft Knowledge Base:
 
   ARTICLE ID: Q58650
   TITLE     : Changing a Drive's Volume Label
 
Additional reference words: kbinf 1.00 1.50
KBCategory: kbprg
KBSubcategory: CLngIss
 
=============================================================================
 
Copyright Microsoft Corporation 1995.
