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;
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: