devxlogo

Error Checking Using DriveComboBox Component

Error Checking Using DriveComboBox Component

Question:
I am finding it difficult to check whether an error has occurred in selecting a drive when it is not available at the time. I have tried to use IOResult when the change event takes place, but it doesn’t seem to work. The error that occurs is I/O result 21. IOResult works with other I/O errors that occur, such as “file not found,” but using IOResult with a component like DriveComboBox doesn’t work. I would appreciate if you could help me solve this problem.

Answer:
One way that I’ve found that works is to use the Windows API call GetVolumeInformation to test if a drive is ready. GetVolumeInformation will retrieve information like serial numbers and disk capacities. If GetVolumeInformation can read a drive it returns true, otherwise it returns false. I’ve written a wrapper function that will help you out with your problem:

{=================================================This checks the drive to see if it's ready. IfGetVolumeInformation returns false, the drive isn't ready.Value of Drv formal parameter should be somethinglike: 'C:' or 'F:'=================================================}function CheckDriveOK(Drv : String) : Boolean; var  //Volume Information Variables  nVNameSer   : PDWORD;  pVolName    : PChar;  FSSysFlags,  maxCmpLen   : DWord;  pFSBuf      : PChar;begin  //initialize vars  GetMem(pVolName, MAX_PATH);  GetMem(pFSBuf, MAX_PATH);  GetMem(nVNameSer, MAX_PATH);  //Now, get the volume information  Result := GetVolumeInformation(PChar(drv),              pVolName, MAX_PATH, nVNameSer,             maxCmpLen, FSSysFlags, pFSBuf,              MAX_PATH);end;
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist