Announcement

Collapse
No announcement yet.

Variant and uint64

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Peter Lameijn
    replied
    Here it is. (Bit messy, it's only a test.) Size, TotalCylinders and TotalSectors are UINT64. It's for enumerating USB storage devices.
    Code:
    ' ========================================================================================
    ' WMI example - Retrieving Disk information
    ' ========================================================================================
    
    #Compile Exe
    #Dim All
    #Include "win32api.inc"
    #Include "TB_WMILIB.INC"    ' // WMI helper functions
                                ' see [url]http://www.powerbasic.com/support/pbforums/showpost.php?p=171374&postcount=3[/url]
    
    ' ========================================================================================
    ' Main
    ' ========================================================================================
    Function PBMain
    
       Local hr As Long                   ' // HRESULT
       Local oServices As Dispatch        ' // Services object
       Local vServices As Variant         ' // Services object reference
       Local oItems As Dispatch           ' // Generic collection object
       Local vItems As Variant            ' // Generic collection object reference
       Local oItem As Dispatch            ' // Generic item object
       Local vItem As Variant             ' // Generic item object reference
       Local penum As Dword               ' // Collection's enumerator reference
       Local vCount As Variant            ' // Number of items in the collection
       Local vVar As Variant              ' // General purpose variant
       Local vRes As Variant              ' // General purpose variant
       Local i As Long                    ' // Loop counter
       Local x As Long                    ' // Loop counter
       Local Result As Long
       Local Tmp As String
       Local Disp As String
       Dim vArray(0) As Variant           ' // General purpose array of variants
    
    
       ' // Connect to WMI using a moniker
       hr = WmiGetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2", vServices)
       If hr <> %S_Ok Then GoTo Terminate
       Set oServices = vServices
       vServices = Empty
       If IsFalse IsObject(oServices) Then GoTo Terminate
    
       ' // Execute a query to get a reference to the collection of running processes
       vVar = "SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'"
       Object Call oServices.ExecQuery(vVar) To vItems
       If ObjResult Then GoTo Terminate
       Set oItems = vItems
       vItems = Empty
       If IsFalse IsObject(oItems) Then GoTo Terminate
    
       ' // Retrieve the number of items in the collection
       Object Get oItems.Count To vCount
    
       ' // Retrieve a reference to the collection's enumerator
       hr = Wmi_NewEnum(ObjPtr(oItems), penum)
       If hr <> %S_Ok Or penum = %NULL Then GoTo Terminate
    
       ' // Iterate throught the collection of objects.
       For i = 1 To Variant#(vCount)
          ' // Retrieve a reference to the next object in the collection
          hr = WmiEnum_NextItem(penum, vItem)
          If hr <> %S_Ok Then Exit For
          Set oItem = vItem
          If IsFalse IsObject(oItem) Then Exit For
          '------------------------------------------------------------------------
          Object Get oItem.MediaType To vRes
          Disp = $Cr & "MediaType: "  & $Tab & Variant$(vRes)
          '------------------------------------------------------------------------
          Object Get oItem.VolumeSerialNumber To vRes
          Disp = Disp & $Cr & "VolumeSerialNumber: "  & $Tab & Variant$(vRes)
          '------------------------------------------------------------------------
          Object Get oItem.Model To vRes
          Disp = Disp & $Cr & "Model: "  & $Tab & Variant$(vRes)
          '------------------------------------------------------------------------
          Object Get oItem.PNPDeviceID To vRes
          Disp = Disp & $Cr & "PNP Device ID: "  & $Tab & Variant$(vRes)
          '------------------------------------------------------------------------
          Object Get oItem.DeviceID To vRes
          Disp = Disp & $Cr & "Device ID: "  & $Tab & Variant$(vRes)
          '------------------------------------------------------------------------
          Object Get oItem.Size To vRes
          Disp = Disp & $Cr & "Size: "  & $Tab & Variant$(Vres)'Format$(Variant#(Vres))
          '------------------------------------------------------------------------
          Object Get oItem.TotalCylinders To vRes
          Disp = Disp & $Cr & "Cylinders: "  & $Tab & Variant$(vRes) 'Format$(Variant#(vRes))
          '------------------------------------------------------------------------
          Object Get oItem.TotalHeads To vRes
          Disp = Disp & $Cr & "Heads: "  & $Tab & Format$(Variant#(vRes))
          '------------------------------------------------------------------------
          Object Get oItem.TotalSectors To vRes
          Disp = Disp & $Cr & "Sectors: "  & $Tab & Variant$(vRes) 'Format$(Variant#(vRes))
          '------------------------------------------------------------------------
          Object Get oItem.BytesPerSector To vRes
          Disp = Disp & $Cr & "BytesPerSector: "  & $Tab & Format$(Variant#(vRes))
          '------------------------------------------------------------------------
          Set oItem = Nothing
          MsgBox Disp,, Tmp
       Next
    
       WmiRelease penum
       If IsObject(oItems) Then Set oItems = Nothing
    Terminate:
       If IsObject(oServices) Then Set oServices = Nothing
    End Function
    ' ========================================================================================

    Leave a comment:


  • Michael Mattias
    replied
    Strange enough, when I use VariantVT it returns NULL string (%VT_NULL)
    But if I use Variant$ on any UINT64 member of Win32_DiskDrive, the correct value is returned...
    VT_NULL does NOT describe a "null string", it describes NULL. (Nothing, no value of any type, not even what type it isn't.)

    VARIANT$() cannot possibly return the 'correct value' of a VT_UI8 (UINT64): UINT64 is numeric; VARIANT$() returns a string.

    Here's an idea: Let's try, "SHOW FAILING CODE." (I probably should have thought of that earlier.)


    MCM

    Leave a comment:


  • Peter Lameijn
    replied
    Strange enough, when I use VariantVT it returns NULL string (%VT_NULL)
    But if I use Variant$ on any UINT64 member of Win32_DiskDrive, the correct value is returned...

    Leave a comment:


  • Michael Mattias
    replied
    VARIANT#(VT_UI8 var) Works here... (PB/Win 9.0.1)
    Code:
    FUNCTION PBMAIN () AS LONG
    
        LOCAL StStart AS SYSTEMTIME, STEnd AS SYSTEMTIME
        
        CALL TestVUint64()
        EXIT FUNCTION
    
    
    END FUNCTION
    
    FUNCTION TestVUint64() AS LONG
      LOCAL pV  AS VARIANTAPI PTR, v AS VARIANT
      
      LOCAL Q AS QUAD, pQ AS QUAD PTR
      
      LET V = EMPTY   '  intialize
      PV    = VARPTR (V) ' pointer
      
      @pV.vt  = %VT_UI8    ' unint 64
      pQ      = VARPTR (@pV.VD)
      Q       = 12345&&
      @pq     = Q
      
      
      ' get variant#() value from intrinsic function
      
      Q  =  VARIANT#(V)
      MSGBOX FORMAT$(Q)
    
    ' hmm, try with bit 63 set...
      LET Q = -12345&&
      @pq   = Q
      Q     = VARIANT#(V)
      MSGBOX FORMAT$(Q),, "With value -12345"
    
      
        
    END FUNCTION
    .. so you will HAVE to show your failing code if you are having problems.


    MCM
    Last edited by Michael Mattias; 30 Sep 2009, 08:39 AM. Reason: Added code to show results with bit 63 set.

    Leave a comment:


  • Michael Mattias
    replied
    Purpose
    Return the numeric value contained in a Variant variable.

    Syntax
    numericvar = VARIANT#(vrntvar)

    Remarks
    The value returned by VARIANT# may be any range from BYTE to DOUBLE/QUAD/CURRENCY, depending upon the internal representation used within the Variant
    A uint64 will be out of range for a QUAD when bit 63 is set. While I'm surprised that VARIANT#() does not (allegedly, code not shown) just return the 64 bits in a sign-agnostic manner... wait a minute, I don't believe it doesn't. Let me write some test code and see what happens.

    (While I am doing this, you should make a note that VARIANT#() also does not handle VT_DECIMAL types, but there is code here to handle that for you). (Yes, I have submitted a new feature suggestion for same).

    MCM

    Leave a comment:


  • Peter Lameijn
    started a topic Variant and uint64

    Variant and uint64

    How do I retrieve an unsigned quad (UINT64) from a Variant? I tried several things and they all return '0'...
    For instance Variant#(MyVariant) also returns '0'. Do I need to use VariantVT() somehow?

    [edit]
    For some reason, if I use Variant$ on places where an uint64 is expected, all goes well...
    Is this a MS docu error, or does it have another cause? (Happens with Win32_DiskDrive )
    [/edit]
    Last edited by Peter Lameijn; 30 Sep 2009, 07:48 AM.
Working...
X