CreateFile Windows error in Delphi THandleStream

I have a simple class created from THandleStream that I use to edit a raw volume. I call the createfile () function of Windows to open the disk, but the function never returns a valid descriptor, but rather an exotic error code (348 in debug mode and 304 at runtime, but the error does not occur, it's just that the descriptor value looks weird ) this is what i am doing:

Constructor TDiskStream.Create(Const aDrive: String);
Var
  Hdl: Cardinal;
  A,B: Int64;
Begin

  Hdl := CreateFile( PChar(ADrive), 
                     GENERIC_WRITE, 
                     FILE_SHARE_READ Or FILE_SHARE_WRITE, 
                     0, OPEN_EXISTING, 0, 0);

  Inherited Create( Hdl );
  GetDiskFreeSpaceEx( PChar( RightStr(ADrive,2) + '\'), A, FSize, @B );

End;

The descriptor value corresponds to the error code ERROR_INCOMPATIBLE_WITH_GLOBAL_SHORT_NAME_REGISTRY_SETTING on msdn, but this seems very unusual. The aDrive parameter is set correctly (in the format \\. \ X:) .

What's wrong here, how do I manage to get my actual file descriptor?

+3
2

CreateFile - , INVALID_HANDLE_VALUE (-1). CreateFile :

var
  H: THandle;

begin
  H:= CreateFile(PChar('\\.\D:'), GENERIC_READ,
    FILE_SHARE_WRITE or FILE_SHARE_READ, nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if H = INVALID_HANDLE_VALUE then
    raise Exception.Create(Format('Oops - error opening disk: %d', [GetLastError]));
...
+10

. , ?

CreateFile . , Invalid_Handle_Value. GetLastError. , , . MSDN.

CreateFile , , :

if Hdl = Invalid_Handle_Value then
  RaiseLastOSError;
+4

All Articles