Solved

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

Posted on 2009-05-19
4
486 Views
Last Modified: 2013-12-03
Hi
     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) ??
0
Comment
Question by:USBDev
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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.
0
 

Author Comment

by:USBDev
Comment Utility
JKR .
   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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
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(

            "\\\\?\\C:",

            GENERIC_READ | GENERIC_WRITE,

            FILE_SHARE_READ | FILE_SHARE_WRITE,

            NULL,

            OPEN_EXISTING,

            NULL,

            NULL);

if (volume_handle == INVALID_HANDLE_VALUE)

      {

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

            return 0;

      }
 

if (!DeviceIoControl(

            volume_handle,

            FSCTL_LOCK_VOLUME,

            NULL,

            0,

            NULL,

            0,

            &bytes_returned,

            NULL))

      {

          // error handling here

      }

Open in new window

0
 

Author Closing Comment

by:USBDev
Comment Utility
Thanks much this will be a huge help as well. Much appreciated.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

12 Experts available now in Live!

Get 1:1 Help Now