Sign in to follow this  
Followers 0

Drive handle via CreateFile

3 posts in this topic

#1 ·  Posted

Trying to get the handle to drive 0 and this isn't working; why? thanks...

(this one returns 0)
$hDrv = _WinAPI_CreateFileEx( "\\.\PhysicalDrive0", 0, 3, 0, 3, 0, 0) ; Returns 0

(this one returns null)
$hDrv = DllCall('kernel32.dll', 'ptr', 'CreateFile', 'str', "\\.\PhysicalDrive0", 'dword', 0, 'dword', 3, 'ptr', 0, 'dword', 3, 'dword', 0, 'ptr', 0) ; Returns null

(from WinAPIEx.au3)
Func _WinAPI_CreateFileEx($sFile, $iCreation, $iAccess = 0, $iShare = 0, $iFlagsAndAttributes = 0, $tSecurity = 0, $hTemplate = 0)
Local $Ret = DllCall('kernel32.dll', 'ptr', 'CreateFileW', 'wstr', $sFile, 'dword', $iAccess, 'dword', $iShare, 'ptr', DllStructGetPtr($tSecurity), 'dword', $iCreation, 'dword', $iFlagsAndAttributes, 'ptr', $hTemplate)
If (@error) Or ($Ret[0] = Ptr(-1)) Then
  Return SetError(1, 0, 0)
EndIf
Return $Ret[0]
EndFunc   ;==>_WinAPI_CreateFileEx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Share this post


Link to post
Share on other sites

#3 ·  Posted

On Vista+ Admin rights might be necessary.

MSDN: CreateFile function

"dwDesiredAccess [in]

If this parameter is zero, the application can query certain metadata such as file, directory, or device attributes without accessing that file or device, even if GENERIC_READ access would have been denied."

"Physical Disks and Volumes

Direct access to the disk or to a volume is restricted. For more information, see "Changes to the file system and to the storage stack to restrict direct disk access and direct volume access in Windows Vista and in Windows Server 2008" in the Help and Support Knowledge Base at http://support.microsoft.com/kb/942448."

So querying device attributes with dwDesiredAccess = 0 will work without admin rights, whereas access to the content will require admin rights.

Share this post


Link to post
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
Sign in to follow this  
Followers 0