Jump to content

Help with DllCall and DllStructCreate


Go to solution Solved by Nine,

Recommended Posts

MsgBox(64, "DriveIsSDD Test", "Is SSD: " & DriveIsSDD("C:"))

Func DriveIsSDD($vDrive)
    Local $hDev, $vSizeQ, $vSizeD, $vBytesRet, $STORAGE_PROPERTY_QUERY, $DEVICE_SEEK_PENALTY_DESCRIPTOR

    If StringInStr($vDrive, ":\") Then
        $vDrive = StringRegExpReplace($vDrive, "(.*):.*", "$1")
    ElseIf StringLen($vDrive) = 1 Then
        $vDrive &= ":"
    EndIf

    ; FILE_SHARE_DELETE := 0x4 ; FILE_SHARE_WRITE := 0x2
    ; FILE_SHARE_READ := 0x1 ; OPEN_EXISTING := 3
    $hDev = DllCall("kernel32.dll", "dword", "CreateFileW", "wstr", "\\.\\" & $vDrive, "uint", 0, "uint", 0x7, "ptr", 0, "uint", 3, "uint", 0, "ptr", 0, "ptr", 0)
    If Not IsArray($hDev) Or $hDev[0] = -1 Or @error Then
        Return
    EndIf

    ; StorageDeviceSeekPenaltyProperty := 7 ; PropertyStandardQuery := 0
    $vSizeQ = 12
    $STORAGE_PROPERTY_QUERY = DllStructCreate("int PropertyId;int QueryType;byte AdditionalParameters[1]")
    DllStructSetData($STORAGE_PROPERTY_QUERY, "PropertyId", 7)
    DllStructSetData($STORAGE_PROPERTY_QUERY, "QueryType", 0)

    $vSizeD = 12
    $DEVICE_SEEK_PENALTY_DESCRIPTOR = DllStructCreate("int Version;int Size;byte IncursSeekPenalty")

    $vBytesRet = 0

    ; IOCTL_STORAGE_QUERY_PROPERTY := 0x002D1400
    If DllCall("kernel32.dll","bool","DeviceIoControl", "ptr", $hDev[0], "uint", 0x002D1400, "ptr", DllStructGetPtr($STORAGE_PROPERTY_QUERY), "uint", $vSizeQ, "ptr", DllStructGetPtr($DEVICE_SEEK_PENALTY_DESCRIPTOR), "uint", $vSizeD, "uint*", $vBytesRet, "ptr", 0) Then
        DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hDev[0])
        Return Not DllStructGetData($DEVICE_SEEK_PENALTY_DESCRIPTOR, "IncursSeekPenalty")
    EndIf

    ; IOCTL_ATA_PASS_THROUGH := 0x0004D02C
    ; ATA_PASS_THROUGH_EX
    ; IDENTIFY_DEVICE_DATA - NominalMediaRotationRate
    ; Sending ATA commands directly to device in Windows? - Stack Overflow
    ; https://stackoverflow.com/questions/5070987/sending-ata-commands-directly-to-device-in-windows

    DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hDev[0])
    Return 0
EndFunc

Probably there are many mistakes. Someone want to check it out?

Thanks! 😀

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

On external disk not work in my case, so i have see a different approach on stackoverflow and i want to try it out. But with DllCall i have some difficuties 😅

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Solution
Posted (edited)

Here my take on it :

#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>

IsSSD("D:")

Func IsSSD($sDriveLetter)
  Local Const $tagSTORAGE_PROPERTY_QUERY = 'int PropertyId; int QueryType; byte AdditionalParameters[1];'
  Local Const $tagDEVICE_SEEK_PENALTY_DESCRIPTOR = 'dword Version;dword Size;byte IncursSeekPenalty;'

  $sDriveLetter = StringRegExpReplace($sDriveLetter, "([[:alpha:]]:).*", "$1")
  If @error Then Return SetError(1)
  Local $hDevice = _WinAPI_CreateFile('\\.\' & $sDriveLetter, 2, 0, 6, 0, 0)
  If Not $hDevice Then Return SetError(2)

  Local $tSTORAGE_PROPERTY_QUERY = DllStructCreate($tagSTORAGE_PROPERTY_QUERY)
  $tSTORAGE_PROPERTY_QUERY.PropertyId = 7
  Local $tDEVICE_SEEK_PENALTY_DESCRIPTOR = DllStructCreate($tagDEVICE_SEEK_PENALTY_DESCRIPTOR)
  Local $iRes = _WinAPI_DeviceIoControl($hDevice, $IOCTL_STORAGE_QUERY_PROPERTY, _
      DllStructGetPtr($tSTORAGE_PROPERTY_QUERY), DllStructGetSize($tSTORAGE_PROPERTY_QUERY), _
      DllStructGetPtr($tDEVICE_SEEK_PENALTY_DESCRIPTOR), DllStructGetSize($tDEVICE_SEEK_PENALTY_DESCRIPTOR))
  _WinAPI_CloseHandle($hDevice)
  If Not $iRes then Return SetError(3)

  ConsoleWrite($sDriveLetter & " is SSD " & Not $tDEVICE_SEEK_PENALTY_DESCRIPTOR.IncursSeekPenalty & @CRLF)
EndFunc   ;==>GetDiskSerial

 

Edited by Nine
Link to comment
Share on other sites

Work fine! Thanks, i'll study it (and i don't have find on Google _WinAPI_DeviceIoControl lol, didn't know it already exist). Thanks again!

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...