Re: problems accessing compact flash card

From: Nicolai Hansen (nic_at_aub.dk)
Date: 11/22/04


Date: 21 Nov 2004 23:23:50 -0800

Marco van de Voort <marcov@stack.nl> wrote in message news:<slrncpopef.ddp.marcov@turtle.stack.nl>...
> On 2004-11-18, Nicolai Hansen <nic@aub.dk> wrote:
> > I am working on an application storing data on a compact flash card. I
> > want to eject this card after the data is saved.
> > The eject function works fine when it is used alone, but when I save
> > some data to the card, the CreateFile part returns an invalid handle.
> > As if the drive is in use.
> > I tried to flush all files before closing them but this does not work.
> > Anyone got a clue about this?
>
> - After closing it can take up to seconds before locks are removed.
> - do you start programs from that cf?

I found the problem. There was a drivecombobox pointing to the CF
drive :)

In case anyone ever need to eject a CF from Delphi, this is how I've
done it (this took me hours and hours to figure out):

var
  h: THandle;
  dw: DWORD;

begin
 ...

  if SaveForm.EjectCF then
  begin
    repeat
      t:=0;
      repeat
        sleep(100);
        h:=CreateFile(PChar('\\.\'+SaveForm.Drive+':'), GENERIC_ALL,
0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
        if integer(h)<0 then // wtf happened to if h=INVALID_HANDLE?
          CloseHandle(h);
        inc(t);
      until (integer(h)>-1) or (t=100);
      DeviceIOControl(h, {IOCTL_STORAGE_EJECT_MEDIA}$2d4808, nil, 0,
nil, 0, dw, nil); // will just fail if h is invalid
      i:=GetLastError;
      CloseHandle(h);
    until (i=0) or (MessageDlg('Failed to eject CF', mtWarning,
[mbAbort, mbRetry], 0)=mrAbort);
  end;
end;



Relevant Pages

  • Re: All-in-One Card Reader : Safely Remove Hardware
    ... I spent some additional time playing around with the Memory Stick Pro card ... The EJECT tool, as you say, is now my preferred method of finishing the task ... Why should I then not expect the card reader to eject the media card? ...
    (microsoft.public.windowsxp.general)
  • Re: Extended attributes question
    ... I've tried eject before closing the window, ... I have a new MP3 player which absolutely refuses to work ... create a special folder (on one of my ... do a fresh FAT format on the card if ea data. ...
    (comp.os.os2.misc)
  • Re: Soft Eject of devices
    ... unless you have a special PCMCIA controller that can eject cards w/out human ... requesting eject will only get you a software eject. ... have to remove the card. ... Please do not send e-mail directly to this alias. ...
    (microsoft.public.development.device.drivers)
  • Re: problems accessing compact flash card
    ... > I am working on an application storing data on a compact flash card. ... > want to eject this card after the data is saved. ... > I tried to flush all files before closing them but this does not work. ...
    (comp.lang.pascal.delphi.misc)
  • Group Policy - allowing eject rights kills autoplay
    ... I am trying to allow users in a school environment to eject memory cards so ... that they can leave their card readers permanently attached to their PCs ... editing group policy to change the local security policy for ... Autoplay is not disabled by policy, and when the eject policy is changed ...
    (microsoft.public.windows.group_policy)