troubleshooting Question

ReadFile error ERROR_NOACCESS

Avatar of DickStorey
DickStorey asked on
Delphi
6 Comments1 Solution2801 ViewsLast Modified:
I have translated the modified async filter from the DirectShow SDK to Delphi.  I have a problem with ReadFile on a local media file opened with GENERIC_READ, FILE_SHARE_READ and FILE_FLAG_OVERLAPPED.

It returns ERROR_NOACCESS.  The parameters going into the call are the same in C++ and in Delphi but Delphi returns
ERROR_NOACCESS whereas C++ returns ERROR_IO_PENDING

Any Ideas what the problem might be?
// Create the file
function TFileStream.Initialize(Filename: string): HResult;
begin
  if FFileHandle <> INVALID_HANDLE_VALUE then
  begin
    Result := E_FAIL;
    exit;
  end;
 
  FFileHandle := CreateFile(PChar(filename), GENERIC_READ, FILE_SHARE_READ,
    nil, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
 
  if FFileHandle = INVALID_HANDLE_VALUE then
    Result := HResultFromWin32(GetLastError)
  else
    Result := S_OK;
end;
 
// Attempt to read it
function TFileStream.StartRead(Buffer: PByte; BytesToRead: Cardinal;
  Align: boolean; Overlapped: POverlapped; var Pending: boolean;
  var BytesRead: cardinal): HResult;
var
  res: BOOL;
  err: DWORD;
begin
  Pending := false;
 
  Lock;
  try
    // all these parameters are valid
    res := ReadFile(FFileHandle, Buffer, BytesToRead, BytesRead, Overlapped);
 
    if not res  then
    begin
      err := GetLastError;
      if (err = ERROR_IO_PENDING) then
      begin
        Pending := true;
        Result := S_OK;
      end
      else if err = ERROR_NOACCESS then // why does this happen?
      begin
        Beep;
        Result := HResultFromWin32(err);
      end
      else
        Result := HResultFromWin32(err);
    end
    else
      Result := S_OK;
  finally
    UnLock;
  end;
end;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros