#include "Precompiled.h" #include "SharedIncludes.h" namespace PlatformAccessDemo { //================================================================================================== static bool InputValidation_CDDriveLetter(String^ strInput); //================================================================================================== void Program::EjectCDDemo() { EjectCDDemo(true); } //-------------------------------------------------------------------------------------------------- void Program::EjectCDDemo(bool bEject) { ЄConsole::PrintDoubleSplitter(true); String ^strDriveLetter=nullptr; bool bMultipleCD=false; ЄConsole::PrintOperation(true,"Looking for CD-drives"); for (Char ch='A'; ch<='Z'; ch++) { // Automatic string (zero terminated), from "ElementaryExtensions.Unsafe" namespace: ЄDisposableSZ dszRootPathName(ch+":\\"); if (::GetDriveType(dszRootPathName)==DRIVE_CDROM) { ЄConsole::PrintExpressive(dszRootPathName.ToString()); if (strDriveLetter) bMultipleCD=true; else strDriveLetter=gcnew String(ch,1); } } if (!strDriveLetter) { ЄConsole::Print(); ReportNotSuccess("No CD-drives were found."); ЄConsole::PressEnterOrEscapeToContinueDialog(true); return; } if (bMultipleCD) { ЄConsole::PrintDialogCaption(true,"Input valid CD-drive letter (\"E\",\"H\", etc.):"); ЄConsole::Print(); if (!ЄConsole::InputStringModify( String::Format("CD to {0}",bEject ? "eject" : "inject"), strDriveLetter, // <-- by reference gcnew ЄConsoleInputValidationCallback(InputValidation_CDDriveLetter) )) return; } ЄConsole::PrintOperation( true, (String^)"Preforming CD-{0}", bEject ? "ejection" : "injection" ); try { // Automatic string (zero terminated), from "ElementaryExtensions.Unsafe" namespace: ЄDisposableSZ dszSymbolicLink("\\\\.\\"+strDriveLetter+":"); HANDLE hDrive=::CreateFile(dszSymbolicLink,FILE_READ_DATA,0,NULL,OPEN_EXISTING,0,NULL); // ^-- implicit type convertion operator is used here: // "ЄDisposableSZ" object --> "const Char*" if (hDrive==INVALID_HANDLE_VALUE) throw gcnew Win32Exception(); // Automatic handle, from "ElementaryExtensions.Unsafe" namespace: ЄDisposableHandle dhDrive(hDrive,true); DWORD dwBytesReturned; if (!::DeviceIoControl( hDrive, bEject ? IOCTL_STORAGE_EJECT_MEDIA : IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &dwBytesReturned, NULL )) throw gcnew Win32Exception(); } catch (Win32Exception^ e) { ReportError(e); ЄConsole::PressEnterOrEscapeToContinueDialog(true); return; } ReportSuccess("Succeded."); } //================================================================================================== static bool InputValidation_CDDriveLetter(String^ strInput) { if (strInput->Length!=1 || !ЄChar::IsAsciiLetter(strInput[0])) return false; // Automatic string (zero terminated), from "ElementaryExtensions.Unsafe" namespace: ЄDisposableSZ dszRootPathName(strInput+":\\"); return ::GetDriveType(dszRootPathName)==DRIVE_CDROM; } //================================================================================================== } // PlatformAccessDemo