Jump to content

Access COM interface IDiskQuotaUser ?


Javik
 Share

Recommended Posts

Is there a way to read data directly from the Windows Component Object Model (COM) interface?

I am trying to make a really simple disk space reporter tool for accounts on an Active Directory domain, to read the disk quota limit for the logged on user's home directory, and report how much disk space they are currently using.

,

MSDN:  IDiskQuotaUser interface

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365033(v=vs.85).aspx

GetQuotaLimit

GetQuotaUsed

 

 

Link to comment
Share on other sites

Yes, it should be possible. You should start by creating a DiskQuotaControl object with ObjCreateInterface. Initialize the object and use FindUserName to obtain a pointer to an IDiskQuotaUser interface. Create a DiskQuotaUser object and call GetQuotaLimit and GetQuotaUsed.

Here is some code to get started:

#AutoIt3Wrapper_UseX64=y ; <<<<<<<<<<<<<<<< You MUST use proper setting >>>>>>>>>>>>>>>>

#include <WinAPIDiag.au3>

Opt( "MustDeclareVars", 1 )

Global Const $sCLSID_DiskQuotaControl = "{7988B571-EC89-11CF-9C00-00AA00A14F56}"

Global Const $sIID_IDiskQuotaControl = "{7988B572-EC89-11CF-9C00-00AA00A14F56}"
Global Const $stag_IDiskQuotaControl = _ ; There's only added parameter types for used methods
  "Initialize hresult(wstr;bool);" & _
  "SetQuotaState hresult();" & _
  "GetQuotaState hresult();" & _
  "SetQuotaLogFlags hresult();" & _
  "GetQuotaLogFlags hresult();" & _
  "SetDefaultQuotaThreshold hresult();" & _
  "GetDefaultQuotaThreshold hresult();" & _
  "GetDefaultQuotaThresholdText hresult();" & _
  "SetDefaultQuotaLimit hresult();" & _
  "GetDefaultQuotaLimit hresult();" & _
  "GetDefaultQuotaLimitText hresult();" & _
  "AddUserSid hresult();" & _
  "AddUserName hresult();" & _
  "DeleteUser hresult();" & _
  "FindUserSid hresult();" & _
  "FindUserName hresult(wstr;ptr*);" & _
  "CreateEnumUsers hresult();" & _
  "CreateUserBatch hresult();" & _
  "InvalidateSidNameCache hresult();" & _
  "GiveUserNameResolutionPriority hresult();" & _
  "ShutdownNameResolution hresult();"

Global Const $sIID_IDiskQuotaUser = "{7988B574-EC89-11CF-9C00-00AA00A14F56}"
Global Const $stag_IDiskQuotaUser = _ ; There's only added parameter types for used methods
  "GetID hresult();" & _
  "GetName hresult();" & _
  "GetSidLength hresult();" & _
  "GetSid hresult();" & _
  "GetQuotaThreshold hresult();" & _
  "GetQuotaThresholdText hresult();" & _
  "GetQuotaLimit hresult(int64*);" & _
  "GetQuotaLimitText hresult();" & _
  "GetQuotaUsed hresult(int64*);" & _
  "GetQuotaUsedText hresult();" & _
  "GetQuotaInformation hresult();" & _
  "SetQuotaThreshold hresult();" & _
  "SetQuotaLimit hresult();" & _
  "Invalidate hresult();" & _
  "GetAccountStatus hresult();"


Example()

Func Example()
  Local $oErrorHandler = ObjEvent( "AutoIt.Error", "ErrorHandler" )

  ; Create DiskQuotaControl object
  Local $oDiskQuotaControl = ObjCreateInterface( $sCLSID_DiskQuotaControl, $sIID_IDiskQuotaControl, $stag_IDiskQuotaControl )
  If Not IsObj( $oDiskQuotaControl ) Then Return ConsoleWrite( "$oDiskQuotaControl ERR" & @CRLF )
  ConsoleWrite( "$oDiskQuotaControl OK" & @CRLF )

  ; Initialize DiskQuotaControl to C-drive with read-only access
  Local $hResult = $oDiskQuotaControl.Initialize( "C:\", False ) ; <<<<<<<<<<<<<<<< Add volume root >>>>>>>>>>>>>>>>
  If $hResult Then Return ConsoleWrite( "Initialize ERR: 0x" & Hex( $hResult ) & " (" & _WinAPI_GetErrorMessage( $hResult ) & ")" & @CRLF )
  ConsoleWrite( "Initialize OK" & @CRLF )

  ; Find user
  Local $pUser
  $hResult = $oDiskQuotaControl.FindUserName( "User name", $pUser ) ; <<<<<<<<<<<<<<<< Add user name >>>>>>>>>>>>>>>>
  If $hResult Then Return ConsoleWrite( "FindUserName ERR: 0x" & Hex( $hResult ) & " (" & _WinAPI_GetErrorMessage( $hResult ) & ")" & @CRLF )
  ConsoleWrite( "FindUserName OK" & @CRLF )

  ; Create IDiskQuotaUser object
  Local $oDiskQuotaUser = ObjCreateInterface( $pUser, $sIID_IDiskQuotaUser, $stag_IDiskQuotaUser )
  If Not IsObj( $oDiskQuotaUser ) Then Return ConsoleWrite( "$oDiskQuotaUser ERR" & @CRLF )
  ConsoleWrite( "$oDiskQuotaUser OK" & @CRLF )

  ; Quota limit
  Local $iLimit
  $hResult = $oDiskQuotaUser.GetQuotaLimit( $iLimit )
  If $hResult Then Return ConsoleWrite( "GetQuotaLimit ERR: 0x" & Hex( $hResult ) & " (" & _WinAPI_GetErrorMessage( $hResult ) & ")" & @CRLF )
  ConsoleWrite( "GetQuotaLimit OK" & @CRLF )
  ConsoleWrite( "$iLimit = " & $iLimit & @CRLF )

  ; Quota used
  Local $iUsed
  $hResult = $oDiskQuotaUser.GetQuotaUsed( $iLimit )
  If $hResult Then Return ConsoleWrite( "GetQuotaUsed ERR: 0x" & Hex( $hResult ) & " (" & _WinAPI_GetErrorMessage( $hResult ) & ")" & @CRLF )
  ConsoleWrite( "GetQuotaUsed OK" & @CRLF )
  ConsoleWrite( "$iUsed = " & $iUsed & @CRLF )
EndFunc

; Use an error handler under development. Eg. comma instead of semicolon
; in the interface description will be caught by such an error handler.
Func ErrorHandler( $oError )
  ConsoleWrite( @ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted!" & @CRLF & _
    "  err.number is: " & @TAB & "0x" & Hex($oError.number) & @CRLF & _
    "  err.windescription:" & @TAB & $oError.windescription & _
    "  err.description is: " & @TAB & $oError.description & @CRLF & _
    "  err.source is: " & @TAB & $oError.source & @CRLF & _
    "  err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
    "  err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
    "  err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
    "  err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
    "  err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF )
EndFunc

 

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

×
×
  • Create New...