Detecting a CD Eject Event while running a C++ program off a CD

Posted on 2009-05-19
Last Modified: 2013-12-03
     I'm running a C++ (TrollTech QT) program off a CD on a Windows machine.

If a user ejects the CD while the application is still running, I'm unable to detect the event through my program. Additionally my appilcation becomes unstable and I'm unable to gracefully handle any crashes that happen. Is it possible to do any of the below ...
1) detect CD eject event and prevent the CD eject while my application is running?
2) If #1 is not possible....then after CD is ejected , display a message to the user indicating that the CD has been ejected (This part may need to be loaded into memory.....but how) ??
Question by:USBDev
  • 2
  • 2
LVL 86

Expert Comment

ID: 24428172
Well, you *can* detect the open event, but I am afraid this is not going to help you much. Once the tray is openend (and you cannot do much about it) your app is still to experience problems. I'd rather suggest to copy your app to a temporary folder and launch it from there.

Author Comment

ID: 24447403
   Thanks for your response. running the program off  hard disk is exactly what we are not to do. This is because there is confidential information on the CD which needs to remain on the CD. If we do copy on the hard disk we need to delete this data after the program is done and the user exits or the cd is ejected. Hence the problem.

Any other solutions /suggestions you hve are welcome.
We're also exploring spawning off seprate processes into memory which keep polling for files on a CD ...etct...etc.
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 24468013
OK, yet the notification event won't help you - you just will know that you're gonna crash soon. What you could do is locking the drive, so the user cannot open it, e.g.
HANDLE volume_handle;

volume_handle = CreateFile(








if (volume_handle == INVALID_HANDLE_VALUE)


            printf("Failed to open handle to drive %c: - error code %u", drive, GetLastError());

            return 0;


if (!DeviceIoControl(










          // error handling here


Open in new window


Author Closing Comment

ID: 31583301
Thanks much this will be a huge help as well. Much appreciated.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now