Borland C ++ Builder FAQ
The content The last update: 12/12/2008

How program to control CD-ROM'om?

The author: trainer
The initial link: - - -

To begin with we define auxiliary variables and functions which facilitate transmission of commands:
#include <mmsystem.h> 
#include <stdio.h> 
#pragma comment (lib, "winmm.lib") 

/* Commands */ 
char * CdCmdSet = "set"; 
char * CdCmdStatus = "status"; 
char * CdCmdPlay = "play"; 
char * CdCmdPause = "pause"; 
char * CdCmdStop = "stop"; 
char * CdCmdResume = "resume"; 

/* Flags of commands */ 
char * CdCmdFlagOpen = "door open"; 
char * CdCmdFlagClose = "door closed"; 
char * CdCmdCurTrack = "current track"; 
char * CdCmdCurMode = "mode"; 
char * CdCmdFlagEmpty = ""; 

/* performance Flags */ 
char * CdFlagWait = "wait"; 
char * CdFlagEmpty = ""; 

/* the Buffer in which we will form teams */ 
char CdCommandBuffer [256]; 

/* Creation of a command without instructions of a name of CD-ROM'a */ 
char * CreateCdCommand (char * pBuffer, char * pCommand, char * pCmdFlags, char * pFlags) { 
   sprintf (pBuffer, "%s cdaudio %s %s", pCommand, pCmdFlags, pFlags); 
   return pBuffer; 
}

/* Creation of a command with instructions of a name of CD-ROM'a */ 
char * CreateCdCommandEx (char * pBuffer, char DriveLetter, char * pCommand, char * pCmdFlags, char * pFlags) { 
   sprintf (pBuffer, "%s cdaudio! %c: %s %s", pCommand, DriveLetter, pCmdFlags, pFlags); 
   return pBuffer; 
}
Now, using auxiliary functions and variables, to give CD-ROM'om control instructions not simply, and it is very simple.
  1. To open CD-ROM, expecting performance of this command:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdSet,CdCmdFlagOpen,CdFlagWait), 
    		NULL, 
    		0, 
    		INVALID_HANDLE_VALUE);
  2. To close CD_ROM, expecting performance of this command:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdSet,CdCmdFlagClose,CdFlagWait), 
    		NULL, 
    		0, 
    		INVALID_HANDLE_VALUE);
  3. To begin playback without performance waiting:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdPlay,CdCmdFlagEmpty,CdFlagEmpty), 
    		NULL, 
    		0, 
    		INVALID_HANDLE_VALUE);
  4. To pause display (pause) without performance waiting:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdPause,CdCmdFlagEmpty,CdFlagEmpty), 
    		NULL, 
    		0, 
    		INVALID_HANDLE_VALUE);
  5. To restart playback without performance waiting:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdResume,CdCmdFlagEmpty,CdFlagEmpty), 
    		NULL, 
    		0, 
    		INVALID_HANDLE_VALUE);
  6. To stop playback (stop) without performance waiting:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdStop,CdCmdFlagEmpty,CdFlagEmpty), 
    		NULL, 
    		0, 
    		INVALID_HANDLE_VALUE);
  7. To receive number of the current track, expecting performance of this command:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdStatus,CdCmdCurTrack,CdFlagWait), 
    		CdCommandBuffer, 
    		255, 
    		INVALID_HANDLE_VALUE); 
    ShowMessage ("Current track is" +AnsiString (CdCommandBuffer)); 
    
  8. To come into a current fortune, expecting performance of this command:
    mciSendStringA (CreateCdCommand (CdCommandBuffer,CdCmdStatus,CdCmdCurMode,CdFlagWait), 
    		CdCommandBuffer, 
    		255, 
    		INVALID_HANDLE_VALUE); 
    ShowMessage ("Current mode is \'" + AnsiString (CdCommandBuffer) + "\'"); 
    
If at system there are some drives of CD for the job of a specific drive instead of function CreateCdCommand it is necessary to use CreateCdCommandEx