Jim Lawless' Blog


A Command-Line CD Tray Opener

Originally published on: Thu, 20 Aug 2009 02:30:38 +0000

Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C

I needed a command-line utility for Windows that would open or close the CD tray. The result is cdctrl.exe. Please refer to the C source code below.

cdctrl.c


// cdctrl
// A command-line CD-ROM tray opener/closer.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


#include <windows.h>
#include <stdio.h>
#include <ctype.h>
 
#pragma comment(lib, "winmm.lib")
 
void syntax(void);

int main(int argc,char **argv) {
   if(argc<2) {
      syntax();
      return 1;
   }
   if(toupper(*argv[1])=='O') {
      mciSendString("set CDAudio door open wait",0,0,0);
   }
   else
   if(toupper(*argv[1])=='C') {
      mciSendString("set CDAudio door closed",0,0,0);
   }
   else {
      syntax();
      return 1;
   }
   return 0;
}

void syntax(void) {
   printf("Syntax:\n\tcdctrl open\nor\n\tcdctrl close\n");
}

If you execute the above program with no arguments, you should see the following: Syntax: cdctrl open or cdctrl close

The source code and EXE for the cdctrl can be found here:

http://www.mailsend-online.com/wp/cdctrl.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Java in a Windows EXE with launch4j
Next post:Tracing XSLT with a Tiny Java Web Server


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

MailSend is Free

Send GMail From the Windows Command-Line with MailWrench

Windows Text to Speech in WSH JavaScript

My Journey Began with BASIC

Setting Text Color in a Batch File

BPL: Batch Programming Language Interpreter

WSH2EXE - The Final Chapter

Locking a Windows Session

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service