Jump to content

WinAPIEx UDF


Yashied
 Share

Recommended Posts

Please show me result of the following code.

#Include <WinAPIEx.au3>

$Drive = DriveGetDrive('ALL')

For $i = 1 To $Drive[0]
    ConsoleWrite($Drive[$i] & ' ')
    $tData = _WinAPI_GetDriveNumber($Drive[$i])
    If IsDllStruct($tData) Then
        ConsoleWrite(DllStructGetData($tData, 'DeviceNumber') & @CR)
    Else
        ConsoleWrite('?' & @CR)
    EndIf
Next
Link to comment
Share on other sites

Please show me result of the following code.

#Include <WinAPIEx.au3>

$Drive = DriveGetDrive('ALL')

For $i = 1 To $Drive[0]
    ConsoleWrite($Drive[$i] & ' ')
    $tData = _WinAPI_GetDriveNumber($Drive[$i])
    If IsDllStruct($tData) Then
        ConsoleWrite(DllStructGetData($tData, 'DeviceNumber') & @CR)
    Else
        ConsoleWrite('?' & @CR)
    EndIf
Next

It's so sad! This is the result!

post-30865-0-76708900-1300808701_thumb.p

Link to comment
Share on other sites

Try this.

#Include <WinAPIEx.au3>

$Drive = DriveGetDrive('ALL')

For $i = 1 To $Drive[0]
    ConsoleWrite($Drive[$i] & ' ')
    $tData = __WinAPI_GetDriveNumber($Drive[$i])
    If IsDllStruct($tData) Then
        ConsoleWrite(DllStructGetData($tData, 'DeviceNumber') & @CR)
    Else
        ConsoleWrite('? - ' & _WinAPI_GetLastErrorMessage() & @CR)
    EndIf
Next

Func __WinAPI_GetDriveNumber($sDrive)

    Local $hFile = _WinAPI_CreateFileEx('\\.\' & $sDrive, 3, 0, 0x06)

    If Not $hFile Then
        Return SetError(1, 0, 0)
    EndIf

    Local $tSTORAGE_DEVICE_NUMBER = DllStructCreate($tagSTORAGE_DEVICE_NUMBER)
    Local $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hFile, 'dword', $IOCTL_STORAGE_GET_DEVICE_NUMBER, 'ptr', 0, 'dword', 0, 'ptr', DllStructGetPtr($tSTORAGE_DEVICE_NUMBER), 'dword', DllStructGetSize($tSTORAGE_DEVICE_NUMBER), 'dword*', 0, 'ptr', 0)

    If (@error) Or (Not $Ret[0]) Then
        $Ret = 0
    EndIf
    _WinAPI_CloseHandle($hFile)
    If Not IsArray($Ret) Then
        Return SetError(2, 0, 0)
    EndIf
    Return $tSTORAGE_DEVICE_NUMBER
EndFunc   ;==>__WinAPI_GetDriveNumber
Link to comment
Share on other sites

Try this.

#Include <WinAPIEx.au3>

$Drive = DriveGetDrive('ALL')

For $i = 1 To $Drive[0]
    ConsoleWrite($Drive[$i] & ' ')
    $tData = __WinAPI_GetDriveNumber($Drive[$i])
    If IsDllStruct($tData) Then
        ConsoleWrite(DllStructGetData($tData, 'DeviceNumber') & @CR)
    Else
        ConsoleWrite('? - ' & _WinAPI_GetLastErrorMessage() & @CR)
    EndIf
Next

Func __WinAPI_GetDriveNumber($sDrive)

    Local $hFile = _WinAPI_CreateFileEx('\\.\' & $sDrive, 3, 0, 0x06)

    If Not $hFile Then
        Return SetError(1, 0, 0)
    EndIf

    Local $tSTORAGE_DEVICE_NUMBER = DllStructCreate($tagSTORAGE_DEVICE_NUMBER)
    Local $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hFile, 'dword', $IOCTL_STORAGE_GET_DEVICE_NUMBER, 'ptr', 0, 'dword', 0, 'ptr', DllStructGetPtr($tSTORAGE_DEVICE_NUMBER), 'dword', DllStructGetSize($tSTORAGE_DEVICE_NUMBER), 'dword*', 0, 'ptr', 0)

    If (@error) Or (Not $Ret[0]) Then
        $Ret = 0
    EndIf
    _WinAPI_CloseHandle($hFile)
    If Not IsArray($Ret) Then
        Return SetError(2, 0, 0)
    EndIf
    Return $tSTORAGE_DEVICE_NUMBER
EndFunc   ;==>__WinAPI_GetDriveNumber

the chinese means 'the function is not correct'

post-30865-0-96736900-1300815135_thumb.p

Link to comment
Share on other sites

Yashied,

You might want to adjust Sharing mode when using many file operations with 'CreateFile' to just FILE_SHARE_READ + FILE_SHARE_WRITE. I've found on more than one occasion that adding 'FILE_SHARE_DELETE' can cause some functions to fail. Don't ask me why, never made any sense to me. However, in the case of drives, there would be no need to add that Share mode anyway.

netegg - try changing the '0x6' to just '3' in the '_WinAPI_CreateFileEx()' function call - see if that gives you different results?

Link to comment
Share on other sites

Link to comment
Share on other sites

I can not reproduce this problem.

:)

This is from WinAPIEx 2.5 (the other was not):

Func _WinAPI_GetDriveNumber($sDrive)

    Local $hFile = _WinAPI_CreateFile('\\.\' & $sDrive, 2, 0, 0)

    If Not $hFile Then
        Return SetError(1, 0, 0)
    EndIf

    Local $tSTORAGE_DEVICE_NUMBER = DllStructCreate($tagSTORAGE_DEVICE_NUMBER)
    Local $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hFile, 'dword', 0x002D1080, 'ptr', 0, 'dword', 0, 'ptr', DllStructGetPtr($tSTORAGE_DEVICE_NUMBER), 'dword', DllStructGetSize($tSTORAGE_DEVICE_NUMBER), 'dword*', 0, 'ptr', 0)

    If (@error) Or (Not $Ret[0]) Then
        $Ret = 0
    EndIf
    _WinAPI_FreeHandle($hFile)
    If Not IsArray($Ret) Then
        Return SetError(2, 0, 0)
    EndIf
    Return SetError(0, 0, $tSTORAGE_DEVICE_NUMBER)
EndFunc   ;==>_WinAPI_GetDriveNumber
Link to comment
Share on other sites

#include <WinAPI.au3>

Const $IOCTL_STORAGE_BASE = 0x2D
Const $IOCTL_STORAGE_QUERY_PROPERTY = _MakeCtrlCode($IOCTL_STORAGE_BASE, 0x500)
Const $DFP_RECEIVE_DRIVE_DATA = 0x7C088

Const $tagSTORAGEPROPERTYQUERY = "int PropertyId;int QueryType;byte AdditionalParams"
Const $tagSTORAGEDEVICEDESCRIPTOR = "ulong Version;ulong Size;byte DeviceType;" & _
                        "byte DeviceTypeModifier;byte RemovableMedia;" & _
                        "byte CommandQueueing;ulong VendorIdOffset;" & _
                        "ulong ProductIdOffset;ulong ProductRevisionOffset;" & _
                        "ulong SerialNumberOffset;int BusType;" & _
                        "ulong RawPropertiesLength;byte RawDeviceProperties"
Const $tagDRIVERSTATUS = "byte DriverError;byte IDEStatus;byte Reserved1[2];dword Reserved2[2]"
Const $tagIDEREGS = "byte Features;byte SectorCount;byte SectorNumber;byte CylLow;" & _
                        "byte CylHigh;byte DriveHead;byte Command;byte Reserved"
Const $tagSENDCMDOUTPARAMS = "dword Size;" & $tagDRIVERSTATUS
Const $tagSENDCMDINPARAMS = "dword Size;" & $tagIDEREGS & ";byte DriveNumber;" & _
                        "byte Reserved1[3];dword Reserved2[4];byte Buffer"

$sPhysDisk = "\\.\PhysicalDrive0" ;i've try the 3 drives!
$hDisk = _WinAPI_CreateFile($sPhysDisk, 2, 6, 6, 1)
If $hDisk = -1 Then
        Exit(ConsoleWrite("_WinAPI_CreateFile fails, error code: " & _WinAPI_GetLastError() & @CRLF))
EndIf

$tSCIP = DllStructCreate($tagSENDCMDINPARAMS)
$tSCOP = DllStructCreate($tagSENDCMDOUTPARAMS & ";char Buffer[1024]")
$tQuery = DllStructCreate($tagSTORAGEPROPERTYQUERY)
$tDescr = DllStructCreate($tagSTORAGEDEVICEDESCRIPTOR)
DllStructSetData($tQuery, "PropertyId", 0)
DllStructSetData($tQuery, "QueryType", 0)

$iResult = _DeviceIoControl($hDisk, $IOCTL_STORAGE_QUERY_PROPERTY, _
                $tQuery, DllStructGetSize($tQuery), _
                $tDescr, DllStructGetSize($tDescr))
If $iResult = 0 Then
        Exit(ConsoleWrite("_DeviceIoControl fails, error code: " & @error & @CRLF))
EndIf

If DllStructGetData($tDescr, "BusType") = 2 Then
        DllStructSetData($tSCIP, "Command", 0xA1)
Else
        DllStructSetData($tSCIP, "Command", 0xEC)
EndIf

$iResult = _DeviceIoControl($hDisk, $DFP_RECEIVE_DRIVE_DATA, _
                $tSCIP, DllStructGetSize($tSCIP), _
                $tSCOP, DllStructGetSize($tSCOP))
If $iResult = 0 Then
        Exit(ConsoleWrite("_DeviceIoControl fails, error code: " & @error & @CRLF))
EndIf

Dim $sSerialNum
For $i = 21 to 40 Step 2
        $sSerialNum &= DllStructGetData($tSCOP, "Buffer", $i + 1)
        $sSerialNum &= DllStructGetData($tSCOP, "Buffer", $i)
Next
$sSerialNum = StringReplace($sSerialNum, " ", "")
Msgbox(0, "", $sPhysDisk & "'s serial number: " & $sSerialNum)

$tSCIP = 0
$tSCOP = 0
$tQuery = 0
$tDescr = 0
_WinAPI_CloseHandle($hDisk)

Func _MakeCtrlCode($iDevType, $iFunction, $iMethod = 0, $iAccess = 0)
        Return bitOR(bitShift($iDevType,-16),bitShift($iAccess, -14),bitShift($iFunction, -2), $iMethod)
EndFunc        ;==>_MakeCtrlCode

Func _DeviceIoControl($hDevice, $iCtrlCode, $pIn, $iIn, $pOut, $iOut, $pOverlapped = 0)
        Local $iResult

        If IsDllStruct($pIn) Then $pIn = DllStructGetPtr($pIn)
        If IsDllStruct($pOut) Then $pOut = DllStructGetPtr($pOut)
        If IsDllStruct($pOverlapped) Then $pOverlapped = DllStructGetPtr($pOverLapped)
        $iResult = DllCall("Kernel32.dll", "int", "DeviceIoControl", "ptr", $hDevice, _
                        "dword", $iCtrlCode, "ptr", $pIn, "dword", $iIn, _
                        "ptr", $pOut, "dword", $iOut, "int*", 0, "ptr", $pOverlapped)
        Return SetError(_WinAPI_GetLastError(), $iResult[7], $iResult[0])
EndFunc        ;==>_DeviceIoControl

i can't remember either. But this seems right!

And the funtion _winapi_getdrivernumber also does't get the drive1! Like Under!

i've watched all the function about drive in winapiex, and find out the lack may be produced by how to turn the driver letter(as c:, d:, etc) into the physical drive name (as PhysicalDrive0, PhysicalDrive1, etc). Maybe!

post-30865-0-38270600-1301083946_thumb.p

Edited by netegg
Link to comment
Share on other sites

Hi Yashied,

Another question about _WinAPI_GetProcessCommandLine. I try my best, but it only returns the parameter parts of the command line. So I search the forum and find under:

#include<winapiex.au3>
Global Const $tag_PROCESS_BASIC_INFORMATION = "int ExitStatus;ptr PebBaseAddress;ptr AffinityMask;ptr BasePriority;ulong UniqueProcessId;ulong InheritedFromUniqueProcessId;"
Func _WinAPI_GetProcessCommandLineFromPID($PID=0)
    If Not $PID Then
        $PID = _WinAPI_GetCurrentProcessID()
        If Not $PID Then
            Return SetError(1, 0, 0)
        EndIf
    EndIf

    Local $hProcess=DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x410, 'int', False, 'int', $PID)
    
    Local $PBI=DllStructCreate($tag_PROCESS_BASIC_INFORMATION)
    DllCall("ntdll.dll", "int", "ZwQueryInformationProcess", "hwnd", $hProcess[0], "int", 0, "ptr", DllStructGetPtr($PBI), "int", DllStructGetSize($PBI), "int",0)
    
    Local $dw=DllStructCreate("ptr")
    DllCall("kernel32.dll", "int", "ReadProcessMemory", "hwnd", $hProcess[0], "ptr", DllStructGetData($PBI,2) + 0x10, "ptr", DllStructGetPtr($dw), "int", 4, "ptr", 0)
    
    Local $uString = DllStructCreate("ushort Length;ushort MaxLength;ptr String")
    DllCall("kernel32.dll", "int", "ReadProcessMemory", "hwnd", $hProcess[0], "ptr", DllStructGetData($dw, 1)+0x40, "ptr", DllStructGetPtr($uString), "int", DllStructGetSize($uString), "ptr", 0)
    
    Local $Ret=DllCall("kernel32.dll", "int", "ReadProcessMemory", "hwnd", $hProcess[0], "ptr", DllStructGetData($uString, "String"), "wstr", 0, "int", DllStructGetData($uString, "Length") + 2, "int*", 0)
    DllCall("kernel32.dll", 'int', 'CloseHandle', "hwnd", $hProcess[0])
    
    If (@error) Or (Not $Ret[5]) Then
        $Ret = 0
    EndIf
    _WinAPI_CloseHandle($hProcess[0])
    If Not IsArray($Ret) Then
        Return SetError(1, 0, 0)
    EndIf
    Return $Ret[3]
EndFunc

I add this to the winapiex.au3 for myself. but I think this maybe better than the origin _WinAPI_GetProcessCommandLine.

Pls check!

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 3 weeks later...

The library has been updated.

v3.3

Changes

  • Added the following functions.

    _WinAPI_BeginBufferedPaint

    _WinAPI_BufferedPaintClear

    _WinAPI_BufferedPaintInit

    _WinAPI_BufferedPaintSetAlpha

    _WinAPI_BufferedPaintUnInit

    _WinAPI_CreateDIB

    _WinAPI_CreateNullRgn

    _WinAPI_DrawThemeEdge

    _WinAPI_DrawThemeIcon

    _WinAPI_DwmInvalidateIconicBitmaps

    _WinAPI_DwmSetIconicLivePreviewBitmap

    _WinAPI_DwmSetIconicThumbnail

    _WinAPI_EndBufferedPaint

    _WinAPI_GetBufferedPaintBits

    _WinAPI_GetBufferedPaintDC

    _WinAPI_GetBufferedPaintTargetDC

    _WinAPI_GetBufferedPaintTargetRect

    _WinAPI_GetBoundsRect

    _WinAPI_GetThemeBackgroundContentRect

    _WinAPI_GetThemeBackgroundExtent

    _WinAPI_GetThemeBackgroundRegion

    _WinAPI_GetThemeBitmap

    _WinAPI_GetThemeBool

    _WinAPI_GetThemeEnumValue

    _WinAPI_GetThemeFilename

    _WinAPI_GetThemeMetric

    _WinAPI_GetThemePropertyOrigin

    _WinAPI_GetThemeString

    _WinAPI_GetThemeSysBool

    _WinAPI_GetThemeSysColor

    _WinAPI_GetThemeSysColorBrush

    _WinAPI_GetThemeSysFont

    _WinAPI_GetThemeSysInt

    _WinAPI_GetThemeSysSize

    _WinAPI_GetThemeSysString

    _WinAPI_GetThemeTextExtent

    _WinAPI_GetThemeTextMetrics

    _WinAPI_GetThemeTransitionDuration

    _WinAPI_GetWindowTheme

    _WinAPI_PaintDesktop

    _WinAPI_SaveHICONToFile

    _WinAPI_SetBoundsRect

  • Added examples for the functions above.
  • Removed the following functions.

    _WinAPI_DllGetVersion

  • Updated documentation.

Edited by Yashied
Link to comment
Share on other sites

Its a shame the Voting system can be turned up all the way to 11 (Spinal Tap >> http://en.wikipedia.org/wiki/Up_to_11) because the new additions are what I have been missing, cheers Yashied.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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...