Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can define the IOCTL_ATA_PASS_THROUGH in delphi?

I'm working with the DeviceIoControl function and I need pass the IOCTL_ATA_PASS_THROUGH value to that function. I can't found any delphi translatation for this constant, just i found this C++ definition.

# define IOCTL_ATA_PASS_THROUGH  CTL_CODE(IOCTL_SCSI_BASE, 0x040B, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)

but i have problems to translate that value to delphi using the CTL_CODE macro. the question is how i can define the IOCTL_ATA_PASS_THROUGH in delphi?

like image 286
Salvador Avatar asked Dec 21 '22 03:12

Salvador


2 Answers

The CTL_CODE macro is defined as

#define CTL_CODE(DeviceType, Function, Method, Access) (
  ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
)

So the delphi equivalent of the IOCTL_ATA_PASS_THROUGH const is something like this

uses
  Windows;

const
//#define IOCTL_ATA_PASS_THROUGH  CTL_CODE(IOCTL_SCSI_BASE, 0x040B, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
IOCTL_SCSI_BASE       = FILE_DEVICE_CONTROLLER;
IOCTL_ATA_PASS_THROUGH= (IOCTL_SCSI_BASE shl 16) or ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14) or ($040B shl 2) or (METHOD_BUFFERED);

Note : Unfortunally delphi doesn't supports macros, but you can create a function

function CTL_CODE(DeviceType, _Function, Method, Access: Cardinal): Cardinal;
begin
  Result := (DeviceType shl 16) or (Access Shl 14) or (_Function shl 2) or (Method);
end;

and get the value in run-time in this way

Flag:=CTL_CODE(IOCTL_SCSI_BASE, $040B , METHOD_BUFFERED, (FILE_READ_ACCESS or FILE_WRITE_ACCESS));
like image 183
RRUZ Avatar answered Dec 24 '22 02:12

RRUZ


It has a value of $0004d02c. I obtained this with the following C program.

#include <windows.h>
#include <Ntddscsi.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("%.8x", IOCTL_ATA_PASS_THROUGH);
    return 0;
}

I personally feel that it's safer to use the actual Windows header files than to attempt translation, but perhaps that's just because I don't know enough about C!

like image 32
David Heffernan Avatar answered Dec 24 '22 02:12

David Heffernan