Jump to content
Sign in to follow this  
photonblaster

Error when moving code to Function

Recommended Posts

photonblaster

I created this code to get number of physical cores("Cores") and logical cores ("Threads"), works OK on my PC. This is based on code posted on the forum, sorry I lost the reference to the origal author so cannot give that person credit.

My goal is to get this function working in stand alone version and then incorporate it into larger project.

I do not really know much about DLL calls, object calls, and am just using logic and trial and error to get this code to do what I want, the hard work like the $colitems equal.... statement was done by others and I have no clue what it does.

For some reason when I try to include the code in this post the "#include" statement gets truncated, it should be "#include <CompInfo.au3>"

Opt("MustDeclareVars", 1)

#include
Global $iCores = 1, $iThreads = 1

Local $hDll_ntdll = DllOpen("ntdll.dll")
Local $hDll_winmm = DllOpen("winmm.dll")
Local $colItems, $objWMIService, $objItem, $cI_Compname
Dim $aProcessorInfo[1][10], $CoreInfo[2]
Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $i = 1
$objWMIService = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
For $objItem In $colItems
$CoreInfo[0] = $i + 1
$CoreInfo[1] = $objItem.NumberOfLogicalProcessors
$i += 1
Next
$aProcessorInfo[0][0] = UBound($aProcessorInfo) - 1
If $aProcessorInfo[0][0] < 1 Then
SetError(1, 1, 0)
EndIf
Else
SetError(1, 2, 0)
EndIf

$iCores = $CoreInfo[0]
$iThreads = $CoreInfo[1]
If $iCores > $iThreads Then $iCores = $iThreads
MsgBox(0, "core info", $iCores & " <--- Cores Threads -->" & $iThreads)

DllClose($hDll_ntdll)
DllClose($hDll_winmm)

Exit

I tried to move most of the code to a function, I get an this error message even though it compiles fine.

E:\zzzMisc\AutoIT\CoresAndThreads.au3 (17) : ==> Variable must be of type "Object".:

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

$colItems = $objWMIService^ ERROR

Opt("MustDeclareVars", 1)

#include
Global $iCores = 1, $iThreads = 1
GetCPU_INFO()
MsgBox(0, "core info", $iCores & " <--- Cores Threads -->" & $iThreads)
Exit

Func GetCPU_INFO()
Local $hDll_ntdll = DllOpen("ntdll.dll")
Local $hDll_winmm = DllOpen("winmm.dll")
Local $colItems, $objWMIService, $objItem, $cI_Compname
Dim $aProcessorInfo[1][10], $CoreInfo[2]
Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $i = 1
$objWMIService = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
For $objItem In $colItems
$CoreInfo[0] = $i + 1
$CoreInfo[1] = $objItem.NumberOfLogicalProcessors
$i += 1
Next
$aProcessorInfo[0][0] = UBound($aProcessorInfo) - 1
If $aProcessorInfo[0][0] < 1 Then
SetError(1, 1, 0)
EndIf
Else
SetError(1, 2, 0)
EndIf

$iCores = $CoreInfo[0]
$iThreads = $CoreInfo[1]
If $iCores > $iThreads Then $iCores = $iThreads

DllClose($hDll_ntdll)
DllClose($hDll_winmm)
Return
EndFunc ;==>GetCPU_INFO

Please tell me why I get this error.

Also I know the experts will "cringe" at some of my coding...eg I do not use seterror results so why carry this over from the original code example I worked from? And I do not need the EXIT and Return statements ?? but I feel more comfortable with them there. If someone wants to give constructive criticism besides telling me why the error occurs I promise to read it with a thick skin in place.

Share this post


Link to post
Share on other sites
kylomas

photonblaster,

See comment in code...

Opt("MustDeclareVars", 1)

#include <compinfo.au3>
Global $iCores = 1, $iThreads = 1
GetCPU_INFO()
MsgBox(0, "core info", $iCores & " <--- Cores Threads -->" & $iThreads)
Exit

Func GetCPU_INFO()
    Local $hDll_ntdll = DllOpen("ntdll.dll")
    Local $hDll_winmm = DllOpen("winmm.dll")
    Local $colItems, $objWMIService, $objItem, $cI_Compname = '.' ; <------ tell it which computer or "this one"
    Dim $aProcessorInfo[1][10], $CoreInfo[2]
    Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $i = 1
    $objWMIService = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $CoreInfo[0] = $i + 1
            $CoreInfo[1] = $objItem.NumberOfLogicalProcessors
            $i += 1
        Next
        $aProcessorInfo[0][0] = UBound($aProcessorInfo) - 1
        If $aProcessorInfo[0][0] < 1 Then
            SetError(1, 1, 0)
        EndIf
    Else
        SetError(1, 2, 0)
    EndIf

    $iCores = $CoreInfo[0]
    $iThreads = $CoreInfo[1]
    If $iCores > $iThreads Then $iCores = $iThreads

    DllClose($hDll_ntdll)
    DllClose($hDll_winmm)
    Return
EndFunc   ;==>GetCPU_INFO

* - note - I added $appkey to this var def in the udf as it was erroring out

Local $i = 1, $AppKey

kylomas

edit: incidentally, you don't need compinfo.au3 or the dll's to run the code you posted...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

PB,

I commented out the code that is not required for what you are trying to return...

Opt("MustDeclareVars", 1)

;#include <compinfo.au3>
Global $iCores = 1, $iThreads = 1
GetCPU_INFO()
MsgBox(0, "core info", $iCores & " <--- Cores Threads -->" & $iThreads)
Exit

Func GetCPU_INFO()
;~  Local $hDll_ntdll = DllOpen("ntdll.dll")
;~  Local $hDll_winmm = DllOpen("winmm.dll")
    Local $colItems, $objWMIService, $objItem, $cI_Compname = '.' ; <------ tell it which computer or "this one"
    Dim $aProcessorInfo[1][10], $CoreInfo[2]
    Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $i = 1
    $objWMIService = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $CoreInfo[0] = $i + 1
            $CoreInfo[1] = $objItem.NumberOfLogicalProcessors
            $i += 1
        Next

;~      $aProcessorInfo[0][0] = UBound($aProcessorInfo) - 1
;~      If $aProcessorInfo[0][0] < 1 Then
;~          SetError(1, 1, 0)
;~      EndIf
    Else
        SetError(1, 2, 0)
    EndIf

    $iCores = $CoreInfo[0]
    $iThreads = $CoreInfo[1]
    If $iCores > $iThreads Then $iCores = $iThreads

;~  DllClose($hDll_ntdll)
;~  DllClose($hDll_winmm)
    Return
EndFunc   ;==>GetCPU_INFO

If you are interested in system type info the sciptomatic tool is excellent.

You can download it h

kylomas

Edited by kylomas
  • Like 1

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
photonblaster

PB,

I commented out the code that is not required for what you are trying to return...

If you are interested in system type info the sciptomatic tool is excellent.

You can download it h

kylomas

Thank al lot, kylomas, and I will check out sciptomatic tool as well.

==============================

Rats, can't get Scriptomatic to download through my firewall, will have to do a workaround to get it.

BTW, you mentioned you added $Appkey to var def, but I did not see it in the revision you posted, and the script ran as you posted.

Edited by photonblaster

Share this post


Link to post
Share on other sites
kylomas

photonblaster,

* - note - I added $appkey to this var def in the udf as it was erroring out

compinfo.au3...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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  

×