Page 1 sur 1

Temperature CPU

Publié : lun. 14/juil./2008 19:01
par lionel_om
Bonjour tout le monde,

Il me semble avoir lu un topic sur quelqu'un qui avait trouvé comment avoir la température du CPU. Mais impossible de le retrouver. Peut-être que je confond.
Quelqu'un aurait un ch'ti code pour ça ?

Mer d'avance.
/Lio

Publié : lun. 14/juil./2008 22:58
par Jacobus
Vite fait en passant, un code compilé de plusieurs fonctions de Gillou
Manque la structure HardDiskInfo pour fonctionner et je la trouve nulle part. Si tu as une solution pour y débloquer.

Code : Tout sélectionner

;{- WMI Constants 
#COINIT_MULTITHREAD=0 
#RPC_C_AUTHN_LEVEL_CONNECT=2 
#RPC_C_IMP_LEVEL_IDENTIFY=2 
#EOAC_NONE=0 
#RPC_C_AUTHN_WINNT=10 
#RPC_C_AUTHZ_NONE=0 
#RPC_C_AUTHN_LEVEL_CALL=3 
#RPC_C_IMP_LEVEL_IMPERSONATE=3 
#CLSCTX_INPROC_SERVER=1 
#wbemFlagReturnImmediately=16 
#wbemFlagForwardOnly=32 
#IFlags = #wbemFlagReturnImmediately + #wbemFlagForwardOnly 
#WBEM_INFINITE=$FFFFFFFF 
#WMISeparator="," 
;} 

Procedure.l ansi2bstr(ansi.s)
    size.l = MultiByteToWideChar_(#CP_ACP, 0, ansi, -1, 0, 0)
    Global Dim unicode.w(size)
    MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi), unicode(), size)
    ProcedureReturn SysAllocString_( @unicode())
EndProcedure

Procedure bstr2string (bstr)
    Shared WMIResult.s
    WMIResult.s = ""
    pos = bstr
    While PeekW (pos)
        WMIResult = WMIResult + Chr(PeekW(pos))
        pos = pos + 2
    Wend
    ProcedureReturn @WMIResult
EndProcedure

ProcedureDLL.s WMIC(root.s, WMICommand.s)
    CoInitializeEx_(0, #COINIT_MULTITHREAD)
    hres = CoInitializeSecurity_(0, -1, 0, 0, #RPC_C_AUTHN_LEVEL_CONNECT, #RPC_C_IMP_LEVEL_IDENTIFY, 0, #EOAC_NONE, 0)
    If hres <> 0 : e$ = "unable to call CoInitializeSecurity" : Goto cleanup : EndIf
    hres = CoCreateInstance_(?CLSID_WbemLocator, 0, #CLSCTX_INPROC_SERVER, ?IID_IWbemLocator, @loc.IWbemLocator)
    If hres <> 0 : e$ = "unable to call CoCreateInstance" : Goto cleanup : EndIf
    hres = loc\ConnectServer(ansi2bstr(root), 0, 0, 0, 0, 0, 0, @svc.IWbemServices)
    If hres <> 0 : e$ = "unable to call IWbemLocator::ConnectServer" : loc\Release() : Goto cleanup : EndIf
    hres = svc\QueryInterface(?IID_IUnknown, @pUnk.IUnknown)
    hres = CoSetProxyBlanket_(svc, #RPC_C_AUTHN_WINNT, #RPC_C_AUTHZ_NONE, 0, #RPC_C_AUTHN_LEVEL_CALL, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, #EOAC_NONE)
    If hres <> 0 : e$ = "unable to call CoSetProxyBlanket" : svc\Release() : loc\Release() : Goto cleanup : EndIf
    hres = CoSetProxyBlanket_(pUnk, #RPC_C_AUTHN_WINNT, #RPC_C_AUTHZ_NONE, 0, #RPC_C_AUTHN_LEVEL_CALL, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, #EOAC_NONE)
    If hres <> 0 : e$ = "unable to call CoSetProxyBlanket" : svc\Release() : loc\Release() : pUnk\Release() : Goto cleanup : EndIf
    pUnk\Release()
    
    hres=CoCreateInstance_(?CLSID_WbemRefresher,0,#CLSCTX_INPROC_SERVER,?IID_IWbemRefresher,@pRefresher.IWbemRefresher)
    If hres <> 0 : e$ = "unable to call CoCreateInstance" : svc\Release() : loc\Release() : Goto cleanup : EndIf
    hres=pRefresher\queryinterface(?IID_IWbemConfigureRefresher,@pConfig.IWbemConfigureRefresher)
    If hres <> 0 : e$ = "unable to QueryInterface" : svc\Release() : loc\Release() : pRefresher\release() : Goto cleanup : EndIf
    pRefresher\refresh(0)
    
    k = CountString(WMICommand, #WMISeparator)
    Global Dim wmitxt$(k)
    For i = 0 To k
        wmitxt$(i) = StringField(WMICommand, i + 1, #WMISeparator)
    Next
    
    hres = svc\ExecQuery(ansi2bstr("WQL"), ansi2bstr(wmitxt$(0)), #IFlags, 0, @pEnumerator.IEnumWbemClassObject)
    If hres <> 0 : e$ = "unable to call IWbemServices::ExecQuery" : svc\Release() : loc\Release() : pRefresher\release() : Goto cleanup : EndIf
    hres = pEnumerator\reset()
    Repeat
        hres = pEnumerator\Next(#WBEM_INFINITE, 1, @pclsObj.IWbemClassObject, @uReturn)
        If hres = 0
            For i = 1 To k
                mem = AllocateMemory(1000)
                hres = pclsObj\get(ansi2bstr(wmitxt$(i)), 0, mem, 0, 0)
                Type = PeekW(mem)
                Select Type
                    Case 8
                        val.s = PeekS(bstr2string(PeekL(mem + 8)))
                    Case 3
                        val.s = Str(PeekL(mem + 8))
                    Default
                        val.s = ""
                EndSelect
                If uReturn <> 0 : If wmi$ : wmi$ = wmi$ + "|" + wmitxt$(i) + "=" + val : Else : wmi$ = wmitxt$(i) + "=" + val : EndIf : EndIf
                FreeMemory(mem)
            Next
            pclsObj\Release()
        EndIf
    Until uReturn = 0
    svc\Release() : loc\Release() : pEnumerator\Release() : pRefresher\release()
    cleanup :
    CoUninitialize_()
    If e$
        ProcedureReturn "ERROR : " + e$
    EndIf
    ProcedureReturn wmi$
EndProcedure

DataSection
    CLSID_IEnumWbemClassObject :
    ; 1B1CAD8C-2DAB-11D2-B604-00104B703EFD
        Data.l $1B1CAD8C
        Data.w $2DAB, $11D2
        Data.b $B6, $04, $00, $10, $4B, $70, $3E, $FD
    IID_IEnumWbemClassObject :
    ; 7C857801-7381-11CF-884D-00AA004B2E24
        Data.l $7C857801
        Data.w $7381, $11CF
        Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
    CLSID_WbemLocator :
    ; 4590f811-1d3a-11d0-891f-00aa004b2e24
        Data.l $4590F811
        Data.w $1D3A, $11D0
        Data.b $89, $1F, $00, $AA, $00, $4B, $2E, $24
    IID_IWbemLocator :
    ; dc12a687-737f-11cf-884d-00aa004b2e24
        Data.l $DC12A687
        Data.w $737F, $11CF
        Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
    IID_IUnknown :
    ; 00000000-0000-0000-C000-000000000046
        Data.l $00000000
        Data.w $0000, $0000
        Data.b $C0, $00, $00, $00, $00, $00, $00, $46
    IID_IWbemRefresher:
    ;49353c99-516b-11d1-aea6-00c04fb68820
        Data.l $49353C99
        Data.w $516B, $11D1
        Data.b $AE, $A6, $00, $C0, $4F, $B6, $88, $20
    CLSID_WbemRefresher:
    ;c71566f2-561E-11D1-AD87-00C04FD8FDFF
        Data.l $C71566F2
        Data.w $561E, $11D1
        Data.b $AD,$87,$00,$C0,$4F,$D8,$FD,$FF
    IID_IWbemConfigureRefresher:
    ;49353c92-516b-11d1-aea6-00c04fb68820
        Data.l $49353C92
        Data.w $516B, $11D1
        Data.b $AE, $A6, $00, $C0, $4F, $B6, $88, $20
EndDataSection


ProcedureDLL OpenSMART(DriveNumber.b) ; renvoie 0 en cas d'erreurs, DriveNumber : numéro du disque
    ; on verifie que drivenumber ne depasse pas 4 (le nombre maximum de disques IDE)
    #MAX_IDE_DRIVES = 4
    If DriveNumber = < #MAX_IDE_DRIVES
        ; l'ouverture d'un disque en lecture des attributs SMART dépend de la version de windows
        os = OSVersion()
        If os = 5 Or os = 20 Or os = 50 Or os = 60 Or os = 65
            ; on ouvre un acces au disque physique en lecture/ecriture
            OpenSMART = CreateFile_("\\.\PHYSICALDRIVE" + Str(DriveNumber), #GENERIC_READ Or #GENERIC_WRITE, #FILE_SHARE_READ Or #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0)
        ElseIf os = 10 Or os = 30 Or os = 40
            ; si on est sous 9x/ME on ouvre un handle du pilote SMART
            ; (dans \WINDOWS\SYSTEM ou \WINDOWS\SYSTEM\IOSUBSYS)
            OpenSMART = CreateFile_("\\.\Smartvsd", 0, 0, 0, #CREATE_NEW, 0, 0)
        Else ; plateforme non supportée
            OpenSMART = 0
        EndIf
        ProcedureReturn OpenSMART
    EndIf
EndProcedure

ProcedureDLL.s SmartChangeHighLowByte(Instring.s)
    ; Change BIG-Little Endian
    sdummy.s = ""
    l = Len(Instring)
    For I = 1 To l Step 2
        If (I + 1) <= l
            sdummy.s = sdummy.s + Mid(Instring, I + 1, 1) + Mid(Instring, I, 1)
        EndIf
    Next I
    ProcedureReturn sdummy.s
EndProcedure

ProcedureDLL SmartGetHardDiskInfo(*Drive.HardDiskInfo)
    *Drive\Model = SmartHardDiskInfo\Model
    *Drive\Serial = SmartHardDiskInfo\Serial
    *Drive\Firmware = SmartHardDiskInfo\Firmware
EndProcedure

ProcedureDLL SmartInit(Drive)
    
    ;/ Variables Globales
    Global SmartHardDiskInfo.HardDiskInfo
    
    ;/ Initialisation of the LinkedList
    Static Flag
    If Flag = 0
        Global NewList SmartLList.SmartAttributes()
        Flag = 1
    Else
        ClearList(SmartLList())
    EndIf
    
    
    bin.SENDCMDINPARAMS
    bout.SENDCMDOUTPARAMS
    
    hdh = CreateFile_("\\.\PhysicalDrive" + Str(Drive), #GENERIC_READ | #GENERIC_WRITE, #FILE_SHARE_READ | #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0)
    If hdh
        bin\bDriveNumber = Drive
        bin\cBufferSize = 512
        
        If (Drive & 1)
            bin\irDriveRegs\bDriveHeadReg = $B0
        Else
            bin\irDriveRegs\bDriveHeadReg = $A0
        EndIf
        bin\irDriveRegs\bCommandReg = $EC
        bin\irDriveRegs\bSectorCountReg = 1
        bin\irDriveRegs\bSectorNumberReg = 1
        
        ;/ Fill Disk Information ( Model / Serial / Firmware )
        br = 0
        Result = DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
        If br > 0
            hddfr = 55 : hddln = 40 :
            SmartHardDiskInfo\Model = SmartChangeHighLowByte(PeekS( @bout\bBuffer[0] + hddfr - 1, hddln ) )
            SmartHardDiskInfo\Model = LTrim(RTrim(SmartHardDiskInfo\Model))
            hddfr = 21 : hddln = 20 :
            SmartHardDiskInfo\Serial = Trim(SmartChangeHighLowByte(PeekS( @bout\bBuffer[0] + hddfr - 1, hddln ) ))
            hddfr = 47 : hddln = 8
            SmartHardDiskInfo\Firmware = SmartChangeHighLowByte(PeekS( @bout\bBuffer[0] + hddfr - 1, hddln ) )
        EndIf
        
        bin\irDriveRegs\bFeaturesReg = #SMART_ENABLE_SMART_OPERATIONS
        bin\irDriveRegs\bSectorCountReg = 1
        bin\irDriveRegs\bSectorNumberReg = 1
        bin\irDriveRegs\bCylLowReg = #SMART_CYL_LOW
        bin\irDriveRegs\bCylHighReg = #SMART_CYL_HI
        bin\irDriveRegs\bCommandReg = #IDE_EXECUTE_SMART_FUNCTION
        bin\bDriveNumber = Drive
        If (Drive & 1)
            bin\irDriveRegs\bDriveHeadReg = $B0
        Else
            bin\irDriveRegs\bDriveHeadReg = $A0
        EndIf
        br = 0
        Result = DeviceIoControl_( hdh, #DFP_SEND_DRIVE_COMMAND, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
        If br > 0 ;/ #SMART_ENABLE_SMART_OPERATIONS successful
            Retour = 1
        EndIf
        
        ;/ Fill ThresHolds Value
        bin\irDriveRegs\bFeaturesReg = #SMART_READ_ATTRIBUTE_THRESHOLDS
        br = 0
        Result = DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
        If br > 0 ;/ #SMART_READ_Attribute_THRESHOLDS successful"
            I = 0
            *pTA.ATTRTHRESHOLD = @bout\bBuffer[0] + 2
            While *pTA\bAttrID & $FF <> 0
                AddElement(SmartLList())
                SmartLList()\Attribute = (*pTA\bAttrID & $FF)
                SmartLList()\Threshold = (*pTA\bWarrantyThreshold & $FF)
                *pTA.ATTRTHRESHOLD = *pTA.ATTRTHRESHOLD + SizeOf(ATTRTHRESHOLD)
            Wend
        EndIf
        
        ;/ Fill Value / Worst / Raw
        bin\irDriveRegs\bFeaturesReg = #SMART_READ_ATTRIBUTE_VALUES
        br = 0
        Result = DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
        If br > 0 ;/ #SMART_READ_Attribute_VALUES successful
            I = 0
            *pDA.DRIVEAttribute = @bout\bBuffer[0] + 2
            ResetList(SmartLList())
            While *pDA\bAttrID & $FF <> 0
                NextElement(SmartLList())
                SmartLList()\Status = (*pDA\wStatusFlags & $FFFF )
                SmartLList()\Value = (*pDA\bAttrValue & $FF )
                SmartLList()\Worst = (*pDA\bWorstValue & $FF )
                SmartLList()\Raw = ((*pDA\bRawValue[0] & $FF) + (*pDA\bRawValue[1] & $FF) * 256 )
                *pDA.DRIVEAttribute = *pDA.DRIVEAttribute + SizeOf(DRIVEAttribute)
            Wend
        EndIf
    Else
        Retour = 0
    EndIf
    ProcedureReturn Retour
EndProcedure

ProcedureDLL SmartGetAttributes(*Smart.SmartAttributes)
    Static Pointeur
    
    If Pointeur >= CountList(SmartLList())
        Pointeur = 0
        ProcedureReturn 0
        
    Else
        SelectElement(SmartLList(), Pointeur)
        *Smart\Attribute = SmartLList()\Attribute
        *Smart\Status = SmartLList()\Status
        *Smart\Threshold = SmartLList()\Threshold
        *Smart\Value = SmartLList()\Value
        *Smart\Worst = SmartLList()\Worst
        *Smart\Raw = SmartLList()\Raw
        Pointeur + 1
    EndIf
    ProcedureReturn 1
EndProcedure

ProcedureDLL.s SmartAttribute2Text(Attribute)
    Select Attribute
        Case 1 : szName.s = "Raw Read Error Rate" : szName.s = "Taux d'erreur de lecture brute"
        Case 2 : szName = "Throughput Performance" : szName.s = "Performance du débit"
        Case 3 : szName = "Spin Up Time" : szName.s = "Temps de mise en rotation"
        Case 4 : szName = "Start Stop Count" : szName.s = "Nombre de Marche/Arrêt"
        Case 5 : szName = "Reallocated Sector Ct" : szName.s = "Nombre de secteur réalloué"
        Case 6 : szName = "Read Channel Margin"
        Case 7 : szName = "Seek Error Rate" : szName.s = "Taux d'erreur de déplacement"
        Case 8 : szName = "Seek Time Performance" : szName.s = "Performance du temps de déplacement des têtes"
        Case 9 : szName = "Power On Hours" : szName.s = "Nombre d'heures d'alimentation"
        Case 10 : szName = "Spin Retry Count" : szName.s = "Nombre d'échec de rotation"
        Case 11 : szName = "Calibration Retry Count"
        Case 12 : szName = "Power Cycle Count" : szName.s = "Nombre de cycles d'alimentation"
        Case 13 : szName = "Read Soft Error Rate"
        Case 191 : szName = "G-Sense Error Rate" : szName.s = "Taux d'erreurs G-Sence"
        Case 192 : szName = "Power-Off Retract Count" : szName.s = "Nombre d'arrêt de coupure d'alimentation"
        Case 193 : szName = "Load Cycle Count" : szName.s = "Nombre de chargement"
        Case 194 : szName = "Temperature Celsius" : szName.s = "Température en degré centigarde"
        Case 195 : szName = "Hardware ECC Recovered"
        Case 196 : szName = "Reallocated Event Count" : szName.s = "Nombre d'événements réalloués"
        Case 197 : szName = "Current Pending Sector" : szName.s = "Nombre actuel de secteurs en attente"
        Case 198 : szName = "Offline Uncorrectable" : szName.s = "Extinction incorrect"
        Case 199 : szName = "UDMA CRC Error Count" : szName.s = "Nombre d'erreurs Ultra DMA CRC"
        Case 200 : szName = "Write Error Count" : szName.s = "Nombre d'erreurs d'écriture"
        Case 201 : szName = "Detected TA Count"
        Case 202 : szName = "TA Increase Count"
        Case 203 : szName = "Run Out Cancel"
        Case 204 : szName = "Shock Count Write Opern"
        Case 205 : szName = "Shock Rate Write Opern"
        Case 206 : szName = "Flying Height"
        Case 207 : szName = "Spin High Current"
        Case 208 : szName = "Spin Buzz"
        Case 209 : szName = "Offline Seek Performnce"
        Case 220 : szName = "Disk Shift"
        Case 221 : szName = "G-Sense Error Rate" : szName.s = "Taux d'erreurs G-Sence"
        Case 222 : szName = "Loaded Hours"
        Case 223 : szName = "Load Retry Count"
        Case 224 : szName = "Load Friction"
        Case 225 : szName = "Load Cycle Count" : szName.s = "Chargement du nombre de cycle"
        Case 226 : szName = "Load-in Time"
        Case 227 : szName = "Torq-amp Count"
        Case 228 : szName = "Power-off Retract Count"
        Case 230 : szName = "Head Amplitude"
        Case 231 : szName = "Temperature Celcius" : szName.s = "Température en degré centigrade"
        Case 240 : szName = "Head Flying Hours"
        Case 250 : szName = "Read Error Retry Rate"
        Default : szName = "Unknown Attribute" : szName.s = "Attribut inconnu"
    EndSelect
    ProcedureReturn szName
EndProcedure

ProcedureDLL Round2(Number.f)
    x.f = Number-Round(Number,0)
    If x>=0.5
        ProcedureReturn Round(Number, 1)
    Else
        ProcedureReturn Round(Number, 0)
    EndIf
EndProcedure

ProcedureDLL CPUTemperature(Temp, Mode) ; Temp=0 Température courante, Temp=1 Température critique ; Mode=0 en Celsius, Mode=1 en Fahrenheit
    If temp = 0
        t$ = "currenttemperature"
    ElseIf temp = 1
        t$ = "CriticalTripPoint"
    Else
        ProcedureReturn
    EndIf
    wmic$ = WMIC("root\wmi", "select * from msacpi_thermalzonetemperature," + t$)
    If FindString(wmic$, "ERROR", 0)
        ProcedureReturn -273
    EndIf
    wmic$ = StringField(wmic$, 2, "=")
    TempCPU.f = (Val(wmic$) - 2732) / 10
    If mode = 1
        TempCPU = tempcpu * (9 / 5) + 32
    EndIf
    ProcedureReturn Round2(TempCPU)
EndProcedure

ProcedureDLL HDDTemperature(DriveNumber, Mode) ; Retourne la température en Celcius si Mode=0 et en Fahrenheit si Mode=1
    If SmartInit(DriveNumber)
        SmartGetHardDiskInfo(Disk.HardDiskInfo)
        While SmartGetAttributes(Smart.SmartAttributes)
            If Smart\Attribute = 194
                TempHDD.f = smart\raw
            EndIf
        Wend
        If Mode = 1
            TempHDD = temphdd * (9 / 5) + 32
        EndIf
        ProcedureReturn Round2(tempHDD)
    EndIf
EndProcedure

Debug CPUTemperature(0, 0)
;Debug HDDTemperature(DriveNumber, 0)

Publié : mar. 15/juil./2008 0:33
par lionel_om
Merci bien Jacobus.
Voici le code complet :

Code : Tout sélectionner

;{- WMI Constants
#COINIT_MULTITHREAD=0
#RPC_C_AUTHN_LEVEL_CONNECT=2
#RPC_C_IMP_LEVEL_IDENTIFY=2
#EOAC_NONE=0
#RPC_C_AUTHN_WINNT=10
#RPC_C_AUTHZ_NONE=0
#RPC_C_AUTHN_LEVEL_CALL=3
#RPC_C_IMP_LEVEL_IMPERSONATE=3
#CLSCTX_INPROC_SERVER=1
#wbemFlagReturnImmediately=16
#wbemFlagForwardOnly=32
#IFlags = #wbemFlagReturnImmediately + #wbemFlagForwardOnly
#WBEM_INFINITE=$FFFFFFFF
#WMISeparator=","
;}

Procedure.l ansi2bstr(ansi.s)
  Size.l = MultiByteToWideChar_(#CP_ACP, 0, ansi, -1, 0, 0)
  Global Dim unicode.w(Size)
  MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi), unicode(), Size)
  ProcedureReturn SysAllocString_( @unicode())
EndProcedure

Procedure bstr2string (bstr)
  Shared WMIResult.s
  WMIResult.s = ""
  pos = bstr
  While PeekW (pos)
    WMIResult = WMIResult + Chr(PeekW(pos))
    pos = pos + 2
  Wend
  ProcedureReturn @WMIResult
EndProcedure

ProcedureDLL.s WMIC(root.s, WMICommand.s)
  CoInitializeEx_(0, #COINIT_MULTITHREAD)
  hres = CoInitializeSecurity_(0, -1, 0, 0, #RPC_C_AUTHN_LEVEL_CONNECT, #RPC_C_IMP_LEVEL_IDENTIFY, 0, #EOAC_NONE, 0)
  If hres <> 0 : e$ = "unable to call CoInitializeSecurity" : Goto cleanup : EndIf
  hres = CoCreateInstance_(?CLSID_WbemLocator, 0, #CLSCTX_INPROC_SERVER, ?IID_IWbemLocator, @loc.IWbemLocator)
  If hres <> 0 : e$ = "unable to call CoCreateInstance" : Goto cleanup : EndIf
  hres = loc\ConnectServer(ansi2bstr(root), 0, 0, 0, 0, 0, 0, @svc.IWbemServices)
  If hres <> 0 : e$ = "unable to call IWbemLocator::ConnectServer" : loc\Release() : Goto cleanup : EndIf
  hres = svc\QueryInterface(?IID_IUnknown, @pUnk.IUnknown)
  hres = CoSetProxyBlanket_(svc, #RPC_C_AUTHN_WINNT, #RPC_C_AUTHZ_NONE, 0, #RPC_C_AUTHN_LEVEL_CALL, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, #EOAC_NONE)
  If hres <> 0 : e$ = "unable to call CoSetProxyBlanket" : svc\Release() : loc\Release() : Goto cleanup : EndIf
  hres = CoSetProxyBlanket_(pUnk, #RPC_C_AUTHN_WINNT, #RPC_C_AUTHZ_NONE, 0, #RPC_C_AUTHN_LEVEL_CALL, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, #EOAC_NONE)
  If hres <> 0 : e$ = "unable to call CoSetProxyBlanket" : svc\Release() : loc\Release() : pUnk\Release() : Goto cleanup : EndIf
  pUnk\Release()
  
  hres=CoCreateInstance_(?CLSID_WbemRefresher,0,#CLSCTX_INPROC_SERVER,?IID_IWbemRefresher,@pRefresher.IWbemRefresher)
  If hres <> 0 : e$ = "unable to call CoCreateInstance" : svc\Release() : loc\Release() : Goto cleanup : EndIf
  hres=pRefresher\queryinterface(?IID_IWbemConfigureRefresher,@pConfig.IWbemConfigureRefresher)
  If hres <> 0 : e$ = "unable to QueryInterface" : svc\Release() : loc\Release() : pRefresher\release() : Goto cleanup : EndIf
  pRefresher\refresh(0)
  
  k = CountString(WMICommand, #WMISeparator)
  Global Dim wmitxt$(k)
  For i = 0 To k
    wmitxt$(i) = StringField(WMICommand, i + 1, #WMISeparator)
  Next
  
  hres = svc\ExecQuery(ansi2bstr("WQL"), ansi2bstr(wmitxt$(0)), #IFlags, 0, @pEnumerator.IEnumWbemClassObject)
  If hres <> 0 : e$ = "unable to call IWbemServices::ExecQuery" : svc\Release() : loc\Release() : pRefresher\release() : Goto cleanup : EndIf
  hres = pEnumerator\reset()
  Repeat
  hres = pEnumerator\Next(#WBEM_INFINITE, 1, @pclsObj.IWbemClassObject, @uReturn)
  If hres = 0
    For i = 1 To k
      mem = AllocateMemory(1000)
      hres = pclsObj\get(ansi2bstr(wmitxt$(i)), 0, mem, 0, 0)
      Type = PeekW(mem)
      Select Type
        Case 8
          val.s = PeekS(bstr2string(PeekL(mem + 8)))
        Case 3
          val.s = Str(PeekL(mem + 8))
        Default
          val.s = ""
      EndSelect
      If uReturn <> 0 : If wmi$ : wmi$ = wmi$ + "|" + wmitxt$(i) + "=" + val : Else : wmi$ = wmitxt$(i) + "=" + val : EndIf : EndIf
      FreeMemory(mem)
    Next
    pclsObj\Release()
  EndIf
Until uReturn = 0
svc\Release() : loc\Release() : pEnumerator\Release() : pRefresher\release()
cleanup :
CoUninitialize_()
If e$
  ProcedureReturn "ERROR : " + e$
EndIf
ProcedureReturn wmi$
EndProcedure

DataSection
  CLSID_IEnumWbemClassObject :
  ; 1B1CAD8C-2DAB-11D2-B604-00104B703EFD
  Data.l $1B1CAD8C
  Data.w $2DAB, $11D2
  Data.b $B6, $04, $00, $10, $4B, $70, $3E, $FD
  IID_IEnumWbemClassObject :
  ; 7C857801-7381-11CF-884D-00AA004B2E24
  Data.l $7C857801
  Data.w $7381, $11CF
  Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
  CLSID_WbemLocator :
  ; 4590f811-1d3a-11d0-891f-00aa004b2e24
  Data.l $4590F811
  Data.w $1D3A, $11D0
  Data.b $89, $1F, $00, $AA, $00, $4B, $2E, $24
  IID_IWbemLocator :
  ; dc12a687-737f-11cf-884d-00aa004b2e24
  Data.l $DC12A687
  Data.w $737F, $11CF
  Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
  IID_IUnknown :
  ; 00000000-0000-0000-C000-000000000046
  Data.l $00000000
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  IID_IWbemRefresher:
  ;49353c99-516b-11d1-aea6-00c04fb68820
  Data.l $49353C99
  Data.w $516B, $11D1
  Data.b $AE, $A6, $00, $C0, $4F, $B6, $88, $20
  CLSID_WbemRefresher:
  ;c71566f2-561E-11D1-AD87-00C04FD8FDFF
  Data.l $C71566F2
  Data.w $561E, $11D1
  Data.b $AD,$87,$00,$C0,$4F,$D8,$FD,$FF
  IID_IWbemConfigureRefresher:
  ;49353c92-516b-11d1-aea6-00c04fb68820
  Data.l $49353C92
  Data.w $516B, $11D1
  Data.b $AE, $A6, $00, $C0, $4F, $B6, $88, $20
EndDataSection


ProcedureDLL OpenSMART(DriveNumber.b) ; renvoie 0 en cas d'erreurs, DriveNumber : numéro du disque
  ; on verifie que drivenumber ne depasse pas 4 (le nombre maximum de disques IDE)
  #MAX_IDE_DRIVES = 4
  If DriveNumber = < #MAX_IDE_DRIVES
    ; l'ouverture d'un disque en lecture des attributs SMART dépend de la version de windows
    os = OSVersion()
    If os = 5 Or os = 20 Or os = 50 Or os = 60 Or os = 65
      ; on ouvre un acces au disque physique en lecture/ecriture
      OpenSMART = CreateFile_("\\.\PHYSICALDRIVE" + Str(DriveNumber), #GENERIC_READ Or #GENERIC_WRITE, #FILE_SHARE_READ Or #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0)
    ElseIf os = 10 Or os = 30 Or os = 40
      ; si on est sous 9x/ME on ouvre un handle du pilote SMART
      ; (dans \WINDOWS\SYSTEM ou \WINDOWS\SYSTEM\IOSUBSYS)
      OpenSMART = CreateFile_("\\.\Smartvsd", 0, 0, 0, #CREATE_NEW, 0, 0)
    Else ; plateforme non supportée
      OpenSMART = 0
    EndIf
    ProcedureReturn OpenSMART
  EndIf
EndProcedure

ProcedureDLL.s SmartChangeHighLowByte(Instring.s)
  ; Change BIG-Little Endian
  sdummy.s = ""
  l = Len(Instring)
  For i = 1 To l Step 2
    If (i + 1) <= l
      sdummy.s = sdummy.s + Mid(Instring, i + 1, 1) + Mid(Instring, i, 1)
    EndIf
  Next i
  ProcedureReturn sdummy.s
EndProcedure










Structure IDEREGS
  bFeaturesReg.b
  bSectorCountReg.b
  bSectorNumberReg.b
  bCylLowReg.b
  bCylHighReg.b
  bDriveHeadReg.b
  bCommandReg.b
  bReserved.b
EndStructure
Structure SENDCMDINPARAMS
  cBufferSize.l
  irDriveRegs.IDEREGS
  bDriveNumber.b
  bReserved.b[3]
  dwReserved.l[4]
EndStructure

Structure  DRIVERSTATUS
  bDriveError.b
  bIDEStatus.b
  bReserved.b[2]
  dwReserved.l[2]
EndStructure
Structure  SENDCMDOUTPARAMS
  cBufferSize.l
  DStatus.DRIVERSTATUS     
  bBuffer.b[512]
EndStructure

Structure DRIVEAttribute
  bAttrID.b
  wStatusFlags.w
  bAttrValue.b
  bWorstValue.b
  bRawValue.b[6]
  bReserved.b
EndStructure

Structure ATTRTHRESHOLD
  bAttrID.b
  bWarrantyThreshold.b
  bReserved.b[10]
EndStructure



#DFP_RECEIVE_DRIVE_DATA = $7C088
#DFP_SEND_DRIVE_COMMAND = $7C084
#SMART_ENABLE_SMART_OPERATIONS = $D8
#SMART_CYL_LOW = $4F
#SMART_CYL_HI = $C2
#IDE_EXECUTE_SMART_FUNCTION = $B0
#SMART_READ_ATTRIBUTE_VALUES = $D0
#SMART_READ_ATTRIBUTE_THRESHOLDS = $D1




Structure HardDiskInfo  
  Model.s
  Firmware.s
  Serial.s
  Capacity.l
EndStructure

Structure SmartAttributes
  Attribute.l
  Threshold.l
  Status.l
  Value.l
  Worst.l
  Raw.l
EndStructure







Global SmartHardDiskInfo.HardDiskInfo
Global NewList SmartLList.SmartAttributes()

ProcedureDLL SmartGetHardDiskInfo(*Drive.HardDiskInfo)
  *Drive\Model = SmartHardDiskInfo\Model
  *Drive\Serial = SmartHardDiskInfo\Serial
  *Drive\Firmware = SmartHardDiskInfo\Firmware
EndProcedure

ProcedureDLL SmartInit(Drive)
  
  ;/ Initialisation of the LinkedList
  Static Flag
  If Flag = 0
    Flag = 1
  Else
    ClearList(SmartLList())
  EndIf
  
  
  bin.SENDCMDINPARAMS
  bout.SENDCMDOUTPARAMS
  
  hdh = CreateFile_("\\.\PhysicalDrive" + Str(Drive), #GENERIC_READ | #GENERIC_WRITE, #FILE_SHARE_READ | #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0)
  If hdh
    bin\bDriveNumber = Drive
    bin\cBufferSize = 512
    
    If (Drive & 1)
      bin\irDriveRegs\bDriveHeadReg = $B0
    Else
      bin\irDriveRegs\bDriveHeadReg = $A0
    EndIf
    bin\irDriveRegs\bCommandReg = $EC
    bin\irDriveRegs\bSectorCountReg = 1
    bin\irDriveRegs\bSectorNumberReg = 1
    
    ;/ Fill Disk Information ( Model / Serial / Firmware )
    br = 0
    Result = DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
    If br > 0
      hddfr = 55 : hddln = 40 :
      SmartHardDiskInfo\Model = SmartChangeHighLowByte(PeekS( @bout\bBuffer[0] + hddfr - 1, hddln ) )
      SmartHardDiskInfo\Model = LTrim(RTrim(SmartHardDiskInfo\Model))
      hddfr = 21 : hddln = 20 :
      SmartHardDiskInfo\Serial = Trim(SmartChangeHighLowByte(PeekS( @bout\bBuffer[0] + hddfr - 1, hddln ) ))
      hddfr = 47 : hddln = 8
      SmartHardDiskInfo\Firmware = SmartChangeHighLowByte(PeekS( @bout\bBuffer[0] + hddfr - 1, hddln ) )
    EndIf
    
    bin\irDriveRegs\bFeaturesReg = #SMART_ENABLE_SMART_OPERATIONS
    bin\irDriveRegs\bSectorCountReg = 1
    bin\irDriveRegs\bSectorNumberReg = 1
    bin\irDriveRegs\bCylLowReg = #SMART_CYL_LOW
    bin\irDriveRegs\bCylHighReg = #SMART_CYL_HI
    bin\irDriveRegs\bCommandReg = #IDE_EXECUTE_SMART_FUNCTION
    bin\bDriveNumber = Drive
    If (Drive & 1)
      bin\irDriveRegs\bDriveHeadReg = $B0
    Else
      bin\irDriveRegs\bDriveHeadReg = $A0
    EndIf
    br = 0
    Result = DeviceIoControl_( hdh, #DFP_SEND_DRIVE_COMMAND, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
    If br > 0 ;/ #SMART_ENABLE_SMART_OPERATIONS successful
      Retour = 1
    EndIf
    
    ;/ Fill ThresHolds Value
    bin\irDriveRegs\bFeaturesReg = #SMART_READ_ATTRIBUTE_THRESHOLDS
    br = 0
    Result = DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
    If br > 0 ;/ #SMART_READ_Attribute_THRESHOLDS successful"
      i = 0
      *pTA.ATTRTHRESHOLD = @bout\bBuffer[0] + 2
      While *pTA\bAttrID & $FF <> 0
        AddElement(SmartLList())
        SmartLList()\Attribute = (*pTA\bAttrID & $FF)
        SmartLList()\Threshold = (*pTA\bWarrantyThreshold & $FF)
        *pTA.ATTRTHRESHOLD = *pTA.ATTRTHRESHOLD + SizeOf(ATTRTHRESHOLD)
      Wend
    EndIf
    
    ;/ Fill Value / Worst / Raw
    bin\irDriveRegs\bFeaturesReg = #SMART_READ_ATTRIBUTE_VALUES
    br = 0
    Result = DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
    If br > 0 ;/ #SMART_READ_Attribute_VALUES successful
      i = 0
      *pDA.DRIVEAttribute = @bout\bBuffer[0] + 2
      ResetList(SmartLList())
      While *pDA\bAttrID & $FF <> 0
        NextElement(SmartLList())
        SmartLList()\Status = (*pDA\wStatusFlags & $FFFF )
        SmartLList()\Value = (*pDA\bAttrValue & $FF )
        SmartLList()\Worst = (*pDA\bWorstValue & $FF )
        SmartLList()\Raw = ((*pDA\bRawValue[0] & $FF) + (*pDA\bRawValue[1] & $FF) * 256 )
        *pDA.DRIVEAttribute = *pDA.DRIVEAttribute + SizeOf(DRIVEAttribute)
      Wend
    EndIf
  Else
    Retour = 0
  EndIf
  ProcedureReturn Retour
EndProcedure

ProcedureDLL SmartGetAttributes(*Smart.SmartAttributes)
  Static Pointeur
  
  If Pointeur >= CountList(SmartLList())
    Pointeur = 0
    ProcedureReturn 0
    
  Else
    SelectElement(SmartLList(), Pointeur)
    *Smart\Attribute = SmartLList()\Attribute
    *Smart\Status = SmartLList()\Status
    *Smart\Threshold = SmartLList()\Threshold
    *Smart\Value = SmartLList()\Value
    *Smart\Worst = SmartLList()\Worst
    *Smart\Raw = SmartLList()\Raw
    Pointeur + 1
  EndIf
  ProcedureReturn 1
EndProcedure

ProcedureDLL.s SmartAttribute2Text(Attribute)
  Select Attribute
    Case 1 : szName.s = "Raw Read Error Rate" : szName.s = "Taux d'erreur de lecture brute"
    Case 2 : szName = "Throughput Performance" : szName.s = "Performance du débit"
    Case 3 : szName = "Spin Up Time" : szName.s = "Temps de mise en rotation"
    Case 4 : szName = "Start Stop Count" : szName.s = "Nombre de Marche/Arrêt"
    Case 5 : szName = "Reallocated Sector Ct" : szName.s = "Nombre de secteur réalloué"
    Case 6 : szName = "Read Channel Margin"
    Case 7 : szName = "Seek Error Rate" : szName.s = "Taux d'erreur de déplacement"
    Case 8 : szName = "Seek Time Performance" : szName.s = "Performance du temps de déplacement des têtes"
    Case 9 : szName = "Power On Hours" : szName.s = "Nombre d'heures d'alimentation"
    Case 10 : szName = "Spin Retry Count" : szName.s = "Nombre d'échec de rotation"
    Case 11 : szName = "Calibration Retry Count"
    Case 12 : szName = "Power Cycle Count" : szName.s = "Nombre de cycles d'alimentation"
    Case 13 : szName = "Read Soft Error Rate"
    Case 191 : szName = "G-Sense Error Rate" : szName.s = "Taux d'erreurs G-Sence"
    Case 192 : szName = "Power-Off Retract Count" : szName.s = "Nombre d'arrêt de coupure d'alimentation"
    Case 193 : szName = "Load Cycle Count" : szName.s = "Nombre de chargement"
    Case 194 : szName = "Temperature Celsius" : szName.s = "Température en degré centigarde"
    Case 195 : szName = "Hardware ECC Recovered"
    Case 196 : szName = "Reallocated Event Count" : szName.s = "Nombre d'événements réalloués"
    Case 197 : szName = "Current Pending Sector" : szName.s = "Nombre actuel de secteurs en attente"
    Case 198 : szName = "Offline Uncorrectable" : szName.s = "Extinction incorrect"
    Case 199 : szName = "UDMA CRC Error Count" : szName.s = "Nombre d'erreurs Ultra DMA CRC"
    Case 200 : szName = "Write Error Count" : szName.s = "Nombre d'erreurs d'écriture"
    Case 201 : szName = "Detected TA Count"
    Case 202 : szName = "TA Increase Count"
    Case 203 : szName = "Run Out Cancel"
    Case 204 : szName = "Shock Count Write Opern"
    Case 205 : szName = "Shock Rate Write Opern"
    Case 206 : szName = "Flying Height"
    Case 207 : szName = "Spin High Current"
    Case 208 : szName = "Spin Buzz"
    Case 209 : szName = "Offline Seek Performnce"
    Case 220 : szName = "Disk Shift"
    Case 221 : szName = "G-Sense Error Rate" : szName.s = "Taux d'erreurs G-Sence"
    Case 222 : szName = "Loaded Hours"
    Case 223 : szName = "Load Retry Count"
    Case 224 : szName = "Load Friction"
    Case 225 : szName = "Load Cycle Count" : szName.s = "Chargement du nombre de cycle"
    Case 226 : szName = "Load-in Time"
    Case 227 : szName = "Torq-amp Count"
    Case 228 : szName = "Power-off Retract Count"
    Case 230 : szName = "Head Amplitude"
    Case 231 : szName = "Temperature Celcius" : szName.s = "Température en degré centigrade"
    Case 240 : szName = "Head Flying Hours"
    Case 250 : szName = "Read Error Retry Rate"
    Default : szName = "Unknown Attribute" : szName.s = "Attribut inconnu"
  EndSelect
  ProcedureReturn szName
EndProcedure

ProcedureDLL Round2(Number.f)
  x.f = Number-Round(Number,0)
  If x>=0.5
    ProcedureReturn Round(Number, 1)
  Else
    ProcedureReturn Round(Number, 0)
  EndIf
EndProcedure

ProcedureDLL CPUTemperature(Temp, Mode) ; Temp=0 Température courante, Temp=1 Température critique ; Mode=0 en Celsius, Mode=1 en Fahrenheit
  If Temp = 0
    t$ = "currenttemperature"
  ElseIf Temp = 1
    t$ = "CriticalTripPoint"
  Else
    ProcedureReturn
  EndIf
  wmic$ = WMIC("root\wmi", "select * from msacpi_thermalzonetemperature," + t$)
  If FindString(wmic$, "ERROR", 0)
    ProcedureReturn -273
  EndIf
  wmic$ = StringField(wmic$, 2, "=")
  TempCPU.f = (Val(wmic$) - 2732) / 10
  If Mode = 1
    TempCPU = TempCPU * (9 / 5) + 32
  EndIf
  ProcedureReturn Round2(TempCPU)
EndProcedure

ProcedureDLL HDDTemperature(DriveNumber, Mode) ; Retourne la température en Celcius si Mode=0 et en Fahrenheit si Mode=1
  If SmartInit(DriveNumber)
    SmartGetHardDiskInfo(Disk.HardDiskInfo)
    While SmartGetAttributes(Smart.SmartAttributes)
      If Smart\Attribute = 194
        TempHDD.f = Smart\Raw
      EndIf
    Wend
    If Mode = 1
      TempHDD = TempHDD * (9 / 5) + 32
    EndIf
    ProcedureReturn Round2(TempHDD)
  EndIf
EndProcedure

Debug CPUTemperature(0, 0)
;Debug HDDTemperature(DriveNumber, 0)
Néanmoins, ça ne marche pas chez moi : la valeur retournée est "-273", soit la valeur en cas d'erreur. Je vais tester sur un autre PC. Si vous pouvez tester vous aussi...

/Lio

Publié : mar. 15/juil./2008 0:38
par lionel_om
Ca marche sur mon portable en fait. Les deux pcs sont des multi-cœurs: le portable un C2D T5500 et le fixe un A X2 3800+. Je testerai ça aussi sur un AMD simple cœur pour voir si ca vient du constructeur...

EDIT : ne marche pas non plus sur un Athlon XP 2000+. Donc ce code ne marche peut-être que sur les Pentium...

/Lio

Publié : mar. 15/juil./2008 8:58
par Backup
lionel_om a écrit :Donc ce code ne marche peut-être que sur les Pentium...

/Lio
non ! il ne marche pas sous P4 !! :)

Publié : mar. 15/juil./2008 18:33
par Droopy
renvoie -273 sur mon C2duo !

Publié : mar. 15/juil./2008 19:25
par Jacobus
Pareil pour moi, ça renvoie le zéro absolu (on s'caille les meules dans mon ordi) Visiblement ça déconne dans cette procédure ProcedureDLL.s WMIC(root.s, WMICommand.s) qui renvoie "ERROR" ...

Il faut aussi adapter en fonction de son os (ci-dessous) comme pour Vista par exemple qui n'est pris en compte initialement (os = 70) le code devait fonctionner avec xp.

Code : Tout sélectionner

;   Debug Str(#PB_OS_Windows_NT3_51)   = 5
;   Debug Str(#PB_OS_Windows_95)       = 10
;   Debug Str(#PB_OS_Windows_NT_4)     = 20
;   Debug Str(#PB_OS_Windows_98)       = 30
;   Debug Str(#PB_OS_Windows_ME)       = 40
;   Debug Str(#PB_OS_Windows_2000)     = 50
;   Debug Str(#PB_OS_Windows_XP)       = 60
;   Debug Str(#PB_OS_Windows_Server_2003) = 65
;   Debug Str(#PB_OS_Windows_Vista)       = 70
;   Debug Str(#PB_OS_Windows_Server_2008) = 75
;   Debug Str(#PB_OS_Windows_Future)      = 100 
ProcedureDLL OpenSMART(DriveNumber.b) ; renvoie 0 en cas d'erreurs, DriveNumber : numéro du disque
  ; on verifie que drivenumber ne depasse pas 4 (le nombre maximum de disques IDE)
  #MAX_IDE_DRIVES = 4
  If DriveNumber = < #MAX_IDE_DRIVES
    ; l'ouverture d'un disque en lecture des attributs SMART dépend de la version de windows
    os = OSVersion()
    If os = 5 Or os = 20 Or os = 50 Or os = 60 Or os = 65 Or os = 70
      ; on ouvre un acces au disque physique en lecture/ecriture
      OpenSMART = CreateFile_("\\.\PHYSICALDRIVE" + Str(DriveNumber), #GENERIC_READ Or #GENERIC_WRITE, #FILE_SHARE_READ Or #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0)
    ElseIf os = 10 Or os = 30 Or os = 40
      ; si on est sous 9x/ME on ouvre un handle du pilote SMART
      ; (dans \WINDOWS\SYSTEM ou \WINDOWS\SYSTEM\IOSUBSYS)
      OpenSMART = CreateFile_("\\.\Smartvsd", 0, 0, 0, #CREATE_NEW, 0, 0)
    Else ; plateforme non supportée
      OpenSMART = 0
    EndIf
    ProcedureReturn OpenSMART
  EndIf
EndProcedure
à suivre...

Publié : mar. 15/juil./2008 20:46
par lionel_om
Ne change rien pour moi. Et mes 3 PCs sont sous Win XP (pro ou Media Center)

Publié : jeu. 17/juil./2008 17:22
par Gillou
Salut à tous!

J'arrive à la bourre, ;)

Je me suis réinstallé un windows via vbox, mais faut que je test sur un vrai windows.

avez-vous déjà tenter d'accéder à votre wmi, via wmic.exe (lancer via cmd.exe)

Les commandes ont du etre réécrite avec le sp2 d'xp ??? ... Une doc que je viens de trouver http://technet.microsoft.com/en-us/libr ... 42610.aspx

En tout cas après quelques tests, j'arrive à récup les infos du processeur... ex: 'CPU GET /ALL' avant c'était 'select...' donc normal que le code ne marche plus.

Mais pour le reste impossible de récup les infos (temperature, voltage...), est-ce le fait de passer par la machine virtuelle, sans doute mais pas totalement, faut que je trouve d'autres docs... :roll:

En tout cas, il faudra de toute façon réécrire une partie du code pour obtenir le retour de la commande WMI

Je vais voir ça ce soir, sur mon autre PC (733MHz... avec XP SP2 pour le test...)

Publié : ven. 18/juil./2008 9:09
par brossden
Pour faire un test rapide d'après le post de Gillou

Code : Tout sélectionner

cmd.s = "wmic CPU GET /Value"
txt.s = GetProgramResult(cmd)
txt = RemoveString(txt,Chr(13)+Chr(13))
MessageRequester(cmd,txt)

Publié : ven. 18/juil./2008 20:17
par lionel_om
brossden a écrit :Pour faire un test rapide d'après le post de Gillou

Code : Tout sélectionner

cmd.s = "wmic CPU GET /Value"
txt.s = GetProgramResult(cmd)
txt = RemoveString(txt,Chr(13)+Chr(13))
MessageRequester(cmd,txt)
Je ne vois pas les infos de température... :?
Sinon c'est vrai qu'il y a pas mal d'infos...

PS : pour ceux qui n'utilisent pas la Droopy Lib et qui veulent tester ce code, récupérez la fonction ExecuteProgram() ici : http://www.purebasic.fr/french/viewtopi ... gramresult

/Lio

Publié : ven. 18/juil./2008 21:58
par Gillou
Un peu de doc, en fait il faut passer par root\cimv2 et pas root\wmi...

donc un petit code rapide

Pour les class dispo :
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Puis cliquer sur Win 32 classes (liste déroulante en haut) pour obtenir la structure et la description de la class

Pour l'instant je n'arrive qu'à recup le processeur, pour la température??? (j'ai pas encore tester sans la vbox)

Code : Tout sélectionner

;{- WMI Constants
#COINIT_MULTITHREAD=0
#RPC_C_AUTHN_LEVEL_CONNECT=2
#RPC_C_IMP_LEVEL_IDENTIFY=2
#EOAC_NONE=0
#RPC_C_AUTHN_WINNT=10
#RPC_C_AUTHZ_NONE=0
#RPC_C_AUTHN_LEVEL_CALL=3
#RPC_C_IMP_LEVEL_IMPERSONATE=3
#CLSCTX_INPROC_SERVER=1
#wbemFlagReturnImmediately=16
#wbemFlagForwardOnly=32
#IFlags = #wbemFlagReturnImmediately + #wbemFlagForwardOnly
#WBEM_INFINITE=$FFFFFFFF
#WMISeparator=","
;}

Procedure.l ansi2bstr(ansi.s)
  Size.l = MultiByteToWideChar_(#CP_ACP, 0, ansi, -1, 0, 0)
  Global Dim unicode.w(Size)
  MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi), unicode(), Size)
  ProcedureReturn SysAllocString_( @unicode())
EndProcedure

Procedure bstr2string (bstr)
  Shared WMIResult.s
  WMIResult.s = ""
  pos = bstr
  While PeekW (pos)
    WMIResult = WMIResult + Chr(PeekW(pos))
    pos = pos + 2
  Wend
  ProcedureReturn @WMIResult
EndProcedure

ProcedureDLL.s WMIC(root.s, WMICommand.s)
  CoInitializeEx_(0, #COINIT_MULTITHREAD)
  hres = CoInitializeSecurity_(0, -1, 0, 0, #RPC_C_AUTHN_LEVEL_CONNECT, #RPC_C_IMP_LEVEL_IDENTIFY, 0, #EOAC_NONE, 0)
  If hres <> 0 : e$ = "unable to call CoInitializeSecurity" : Goto cleanup : EndIf
  Debug "Security ok"
  hres = CoCreateInstance_(?CLSID_WbemLocator, 0, #CLSCTX_INPROC_SERVER, ?IID_IWbemLocator, @loc.IWbemLocator)
  If hres <> 0 : e$ = "unable to call CoCreateInstance" : Goto cleanup : EndIf
  Debug "Create instance ok"
  hres = loc\ConnectServer(ansi2bstr(root), 0, 0, 0, 0, 0, 0, @svc.IWbemServices)
  If hres <> 0 : e$ = "unable to call IWbemLocator::ConnectServer" : loc\Release() : Goto cleanup : EndIf
  Debug "Connection to server ok"
  hres = svc\QueryInterface(?IID_IUnknown, @pUnk.IUnknown)
  hres = CoSetProxyBlanket_(svc, #RPC_C_AUTHN_WINNT, #RPC_C_AUTHZ_NONE, 0, #RPC_C_AUTHN_LEVEL_CALL, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, #EOAC_NONE)
  If hres <> 0 : e$ = "unable to call CoSetProxyBlanket" : svc\Release() : loc\Release() : Goto cleanup : EndIf
  hres = CoSetProxyBlanket_(pUnk, #RPC_C_AUTHN_WINNT, #RPC_C_AUTHZ_NONE, 0, #RPC_C_AUTHN_LEVEL_CALL, #RPC_C_IMP_LEVEL_IMPERSONATE, 0, #EOAC_NONE)
  If hres <> 0 : e$ = "unable to call CoSetProxyBlanket" : svc\Release() : loc\Release() : pUnk\Release() : Goto cleanup : EndIf
  pUnk\Release()
 
  hres=CoCreateInstance_(?CLSID_WbemRefresher,0,#CLSCTX_INPROC_SERVER,?IID_IWbemRefresher,@pRefresher.IWbemRefresher)
  If hres <> 0 : e$ = "unable to call CoCreateInstance" : svc\Release() : loc\Release() : Goto cleanup : EndIf
  hres=pRefresher\queryinterface(?IID_IWbemConfigureRefresher,@pConfig.IWbemConfigureRefresher)
  If hres <> 0 : e$ = "unable to QueryInterface" : svc\Release() : loc\Release() : pRefresher\release() : Goto cleanup : EndIf
  pRefresher\refresh(0)
  
  k = CountString(WMICommand, #WMISeparator)
  Global Dim wmitxt$(k)
  For i = 0 To k
    wmitxt$(i) = StringField(WMICommand, i + 1, #WMISeparator)
  Next
 
  hres = svc\ExecQuery(ansi2bstr("WQL"), ansi2bstr(wmitxt$(0)), #IFlags, 0, @pEnumerator.IEnumWbemClassObject)
  If hres <> 0 : e$ = "unable to call IWbemServices::ExecQuery" : svc\Release() : loc\Release() : pRefresher\release() : Goto cleanup : EndIf
  Debug "Query excute with success"
  hres = pEnumerator\reset()
  Repeat
  hres = pEnumerator\Next(#WBEM_INFINITE, 1, @pclsObj.IWbemClassObject, @uReturn)
  If hres = 0
    For i = 1 To k
      mem = AllocateMemory(1000)
      hres = pclsObj\get(ansi2bstr(wmitxt$(i)), 0, mem, 0, 0)
      Type = PeekW(mem)
      Select Type
        Case 8
          val.s = PeekS(bstr2string(PeekL(mem + 8)))
        Case 3
          val.s = Str(PeekL(mem + 8))
        Default
          val.s = ""
      EndSelect
      If uReturn <> 0 : If wmi$ : wmi$ = wmi$ + "|" + wmitxt$(i) + "=" + val : Else : wmi$ = wmitxt$(i) + "=" + val : EndIf : EndIf
      FreeMemory(mem)
    Next
    pclsObj\Release()
  EndIf
Until uReturn = 0
svc\Release() : loc\Release() : pEnumerator\Release() : pRefresher\release()
cleanup :
CoUninitialize_()
If e$
  ProcedureReturn "ERROR : " + e$
EndIf
ProcedureReturn wmi$
EndProcedure

DataSection
  CLSID_IEnumWbemClassObject :
  ; 1B1CAD8C-2DAB-11D2-B604-00104B703EFD
  Data.l $1B1CAD8C
  Data.w $2DAB, $11D2
  Data.b $B6, $04, $00, $10, $4B, $70, $3E, $FD
  IID_IEnumWbemClassObject :
  ; 7C857801-7381-11CF-884D-00AA004B2E24
  Data.l $7C857801
  Data.w $7381, $11CF
  Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
  CLSID_WbemLocator :
  ; 4590f811-1d3a-11d0-891f-00aa004b2e24
  Data.l $4590F811
  Data.w $1D3A, $11D0
  Data.b $89, $1F, $00, $AA, $00, $4B, $2E, $24
  IID_IWbemLocator :
  ; dc12a687-737f-11cf-884d-00aa004b2e24
  Data.l $DC12A687
  Data.w $737F, $11CF
  Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
  IID_IUnknown :
  ; 00000000-0000-0000-C000-000000000046
  Data.l $00000000
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  IID_IWbemRefresher:
  ;49353c99-516b-11d1-aea6-00c04fb68820
  Data.l $49353C99
  Data.w $516B, $11D1
  Data.b $AE, $A6, $00, $C0, $4F, $B6, $88, $20
  CLSID_WbemRefresher:
  ;c71566f2-561E-11D1-AD87-00C04FD8FDFF
  Data.l $C71566F2
  Data.w $561E, $11D1
  Data.b $AD,$87,$00,$C0,$4F,$D8,$FD,$FF
  IID_IWbemConfigureRefresher:
  ;49353c92-516b-11d1-aea6-00c04fb68820
  Data.l $49353C92
  Data.w $516B, $11D1
  Data.b $AE, $A6, $00, $C0, $4F, $B6, $88, $20
EndDataSection

Debug WMIC("root\Cimv2", "Select * from Win32_PerfFormattedData_PerfOS_Processor,PercentProcessorTime")
Debug WMIC("root\Cimv2", "Select * from Win32_TemperatureProbe,CurrentReading")

Publié : sam. 19/juil./2008 11:33
par lionel_om
J'ai la ligne suivante qui ne passe pas :

Code : Tout sélectionner

hres = loc\ConnectServer(ansi2bstr(root), 0, 0, 0, 0, 0, 0, @svc.IWbemServices) 
Le code d'erreur renvoyé est le suivant sous XP Pro :

Code : Tout sélectionner

#WBEM_E_OUT_OF_MEMORY     = 2147749894  ; There was Not enough memory To complete The operation.
Impossible de trouver quelqu'un qui a ce problème.

Est-ce que WinXP Pro est considéré comme Windows 2000 or Windows NT 4.0? Car si c'est le cas, la doc dit : "You cannot use the user principal name (UPN) format"... Mais je ne pense pas que ça soit ça...
J'ai aussi tenté de double slash pour "root\Cimv2" -> "root\\Cimv2". Rien à faire, toujours pas et la même erreur réside.

/Lio

Publié : sam. 19/juil./2008 16:03
par Gillou
Effectivement, pas assez de mémoire pour finir l'opération???, bizarre comme erreur

lien vers MSDN

Je vois pas bien comment résoudre ça, aucune autre doc plus présice :?