Page 1 of 1

c/c++ program for ejecting cd/dvd drive in computer

Posted: Sun Jan 12, 2014 9:57 pm
by sanyam0108
I want to write a program for ejecting cd/dvd drive by c/c++ programming language. And I have the basic knowledge of c/c++ programming language.
Please help me with it and also suggest some notes or links associated with the question.
Please explain the logic also. :)

Re: c/c++ program for ejecting cd/dvd drive in computer

Posted: Sun Jan 12, 2014 10:50 pm
by Love4Boobies
First of all, there is no such thing as the C/C++ programming language. There exist, however, two incompatible languages called C and C++. Secondly, C and C++ are both abstract machines which know nothing about optical drives, just as they know nothing about mice, keyboard, displays, etc. In order to do what you want, you will need to use the services provided by your environment, which you have not mentioned.

Re: c/c++ program for ejecting cd/dvd drive in computer

Posted: Mon Jan 13, 2014 7:02 am
by sanyam0108
Oh ! Got your point . If I have to talk about IDE , I can use any compiler like netbeans ,etc.
Suggest me one on which you worked on . I 'll work with that from now.! :)

Re: c/c++ program for ejecting cd/dvd drive in computer

Posted: Mon Jan 13, 2014 7:05 am
by Thomas
Check out fasm examples , there is one file named beer.asm . It is trivial to convert it to C/C++ . Learn the win32 api , google on forgers win32 api tutorial

https://github.com/mcandre/fasm-win/blo ... R/BEER.ASM , api function : mciSendString

--Thomas

EDIT: invoke is macro in fasm . and other assemblers than expand to a CALL . Basically , you would have to convert the asm call into -> mciSendString("open cdaudio",NULL,NULL,NULL);

Re: c/c++ program for ejecting cd/dvd drive in computer

Posted: Mon Jan 13, 2014 8:30 am
by Love4Boobies
I meant your target environment. For example, Thomas assumed Windows.

Re: c/c++ program for ejecting cd/dvd drive in computer

Posted: Mon Jan 13, 2014 9:24 am
by sortie
This is not the programming project you are looking for.

There is no reason to write such a program: It already exists on your favourite operating system and can probably easily be scripted. If you are willing to dig, you can probably find the system API.

For instance, my Linux system comes with an 'eject' command line utility that does exactly what you need. Assuming the drive is /dev/sr0, you just execute:

Code: Select all

eject /dev/sr0
I can see using strace that eject simply does some ioctl calls like CDROMEJECT, SG_GET_VERSION_NUM, SG_IO, and BLKRRPART on /dev/sr0.

Of course, if you are using Windows or anything silly, there's probably also a command to do that in the shell and there definitely is if you use some Windows scripting language. There surely is a function in the Windows API.

But really, with a bit of googling it's fairly trivial to find the system API for this or, better yet, existing programs in the system that does so that you can invoke from a script. This all depends on what you are really trying to accomplish. Ejecting the drive is pointless on its own unless you want a cup holder. What is the goal of your program?

Re: c/c++ program for ejecting cd/dvd drive in computer

Posted: Mon Jan 13, 2014 4:10 pm
by Thomas
Love4Boobies wrote:I meant your target environment. For example, Thomas assumed Windows.
yep, it is os specific . But from the way the question was phrased my brain heuristic returned windows :mrgreen:


--Thomas

PS : in *nix environments, you probabliy can eject with an icotl call ( basically search the web for the parameters . too lazy to do the searching for the poster ) .

Re: c/c++ program for ejecting cd/dvd drive in computer

Posted: Mon Jan 13, 2014 6:09 pm
by sortie
Thomas wrote:PS : in *nix environments, you probabliy can eject with an icotl call ( basically search the web for the parameters . too lazy to do the searching for the poster ) .
And too lazy to read my post just prior to yours that tell which ioctl calls. ;-)