Results 1 to 11 of 11

Thread: GetSystemInfo API

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    Hi,

    I'm useless at API and I couldn't get it to work if my life depended on it!

    Anyway, I found this in the API viewer.

    Public Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)

    Public Type SYSTEM_INFO
    dwOemID As Long
    dwPageSize As Long
    lpMinimumApplicationAddress As Long
    lpMaximumApplicationAddress As Long
    dwActiveProcessorMask As Long
    dwNumberOrfProcessors As Long
    dwProcessorType As Long
    dwAllocationGranularity As Long
    dwReserved As Long
    End Type

    Now, lets say I wanted to get dwOEMID and dwActiveProcessorMask and put the result in text1 and text2. How would I go about implementing the above?

    BTW, is dwNumberOrfProcessors meant to have an "r" in the middle of "Orf" or have MS stuffed up again.

    Many thank you's

    Kind regards,

    ------------------
    - Chris
    [email protected]
    If it ain't broke - don't fix it

  2. #2
    Guest

    Post

    Probably another typo.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    WITHOUT PREDUJICE (important!)

    Don't like Microsoft much do you!
    I suggested beating Bill Gates up a while ago, but lack of enthuiasm led to drive-by-shooting being called off!

    by the way Mr. Lawyer, Mr. Policeman or anything else Mr. Important, I'm joking!! No drive by shootings have been arranged - certainly not by me. And I didn't attempt to beat him up either - I dunno where he lives!

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Sure! Here is a small example of using this API. Add a ListBox (List1) and CommandButton (Command1) to the form and copy this code:

    Code:
    Private Type SYSTEM_INFO
            dwOemID As Long
            dwPageSize As Long
            lpMinimumApplicationAddress As Long
            lpMaximumApplicationAddress As Long
            dwActiveProcessorMask As Long
            dwNumberOrfProcessors As Long
            dwProcessorType As Long
            dwAllocationGranularity As Long
            dwReserved As Long
    End Type
    Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
    Private Const PROCESSOR_INTEL_386 = 386
    Private Const PROCESSOR_INTEL_486 = 486
    Private Const PROCESSOR_INTEL_PENTIUM = 586
    
    Private Sub Command1_Click()
        Dim tSysInfo As SYSTEM_INFO
        Dim strProcType As String
        
        Call GetSystemInfo(tSysInfo)
        Select Case tSysInfo.dwProcessorType
            Case PROCESSOR_INTEL_386
                strProcType = "Intel 386"
            Case PROCESSOR_INTEL_486
                strProcType = "Intel 486"
            Case PROCESSOR_INTEL_PENTIUM
                strProcType = "Intel Pentium"
        End Select
        List1.AddItem "Number of processors: " & tSysInfo.dwNumberOrfProcessors
        List1.AddItem "Processor Type: " & strProcType
        List1.AddItem "Minimum application address: " & tSysInfo.lpMinimumApplicationAddress
        List1.AddItem "Maximum application address: " & tSysInfo.lpMaximumApplicationAddress
    End Sub
    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


  5. #5
    Addicted Member
    Join Date
    Jul 1999
    Location
    Portland, OR.
    Posts
    226

    Post

    Hi Serge..
    2 Q's:

    1- How about the different generations of Pentiums. (e.g. P1,P2...)?

    2- Is this procedure good enough to check the processor wether it's oreginal Or Not?

    As always, Thanx.

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Next code will definetely will work on WinNT, but may not work on Win95/98 (can't test it right now, don't have it here at work). Add a Listbox (List1) and a CommandButton (Command1) to the form.

    Module Code
    Code:
    Private Type SYSTEM_INFO
        dwOemID As Long
        dwPageSize As Long
        lpMinimumApplicationAddress As Long
        lpMaximumApplicationAddress As Long
        dwActiveProcessorMask As Long
        dwNumberOfProcessors As Long
        dwProcessorType As Long
        dwAllocationGranularity As Long
        wProcessorLevel As Integer
        wProcessorRevision As Integer
    End Type
    Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
    Private Const PROCESSOR_INTEL_386 = 386
    Private Const PROCESSOR_INTEL_486 = 486
    Private Const PROCESSOR_INTEL_PENTIUM = 586
    Private Const PROCESSOR_LEVEL_80386 As Long = 3
    Private Const PROCESSOR_LEVEL_80486 As Long = 4
    Private Const PROCESSOR_LEVEL_PENTIUM As Long = 5
    Private Const PROCESSOR_LEVEL_PENTIUMII As Long = 6
    Public Type udtCPU
        lClockSpeed As Long
        lProcType As Integer
        strProcLevel As String
        strProcRevision As String
        lNumberOfProcessors As Long
    End Type
    
    Public Function GetCPUInfo(ptCPUInfo As udtCPU)
        Dim tSYS As SYSTEM_INFO
        Dim intProcType As Integer
        Dim strProcLevel As String
        Dim strProcRevision As String
        
        Call GetSystemInfo(tSYS)
        
        Select Case tSYS.dwProcessorType
                Case PROCESSOR_INTEL_386: intProcType = 386
                Case PROCESSOR_INTEL_486: intProcType = 486
                Case PROCESSOR_INTEL_PENTIUM: intProcType = 586
        End Select
        
        Select Case tSYS.wProcessorLevel
            Case PROCESSOR_LEVEL_80386: strProcLevel = "Intel 80386"
            Case PROCESSOR_LEVEL_80486: strProcLevel = "Intel 80486"
            Case PROCESSOR_LEVEL_PENTIUM: strProcLevel = "Intel Pentium"
            Case PROCESSOR_LEVEL_PENTIUMII: strProcLevel = "Intel Pentium Pro or Pentium II"
        End Select
        strProcRevision = "Model " & HiByte(tSYS.wProcessorRevision) & ", Stepping " & LoByte(tSYS.wProcessorRevision)
        
        With ptCPUInfo
            .lClockSpeed = GetCPUSpeed
            .lNumberOfProcessors = tSYS.dwNumberOfProcessors
            .lProcType = intProcType
            .strProcLevel = IIf(strProcLevel = "", "None", strProcLevel)
            .strProcRevision = IIf(strProcRevision = "", "None", strProcRevision)
        End With
    End Function
    
    
    Public Function HiByte(ByVal wParam As Integer) As Byte
        HiByte = (wParam And &HFF00&) \ (&H100)
    End Function
    Public Function LoByte(ByVal wParam As Integer) As Byte
        LoByte = wParam And &HFF&
    End Function
    
    Private Function GetCPUSpeed() As Long
        Dim hKey As Long
        Dim lClockSpeed As Long
        Dim strKey As String
        
        strKey = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
        Call RegOpenKey(HKEY_LOCAL_MACHINE, strKey, hKey)
        Call RegQueryValueEx(hKey, "~MHz", 0, 0, lClockSpeed, 4)
        Call RegCloseKey(hKey)
        GetCPUSpeed = lClockSpeed
    End Function
    Form Code
    Code:
    Private Sub Command1_Click()
        Dim tCPU As udtCPU
            
        Call GetCPUInfo(tCPU)
        List1.AddItem "CPU Type:                 " & tCPU.lProcType
        List1.AddItem "Number ofCPUs:        " & tCPU.lNumberOfProcessors
        List1.AddItem "CPU Level:                 " & tCPU.strProcLevel
        List1.AddItem "CPU Revision:            " & tCPU.strProcRevision
        List1.AddItem "CPU Speed (Approx): " & tCPU.lClockSpeed
    End Sub
    I'll make it to work on Win95/98 when I come back home.

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    Serge - any news on if it works for Win95/98? Thanks for thr original code by the way, I think I understand...

    Cheers ),

    ------------------
    - Chris
    [email protected]
    If it ain't broke - don't fix it

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    You know what I could do...test it myself!! )

    Serge - how come you knew what constants applied to this function. API Viewer just lists about a million constants, and doesn't give you any relationship between Constants, Declares and Types! (at least - i can't see any)

    Thank you,

    ------------------
    - Chris
    [email protected]
    If it ain't broke - don't fix it

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    Works a treat in Win 98, apart from it reckons my clock speed is 0 (i think it is sometimes )

    Bye,

    ------------------
    - Chris
    [email protected]
    If it ain't broke - don't fix it

  10. #10
    Hyperactive Member
    Join Date
    Oct 1999
    Posts
    309

    Post

    I have the same as chrisjk...

    A cpu speed of 0... Tried it on two win98 machines...

  11. #11
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Yep...approximate speed of CPU speed is stored in the registry (WinNT only). Windows95/98 don't have that and there's no direct way of finding that out (No API or atleast I couldn't find one).

    For API declaration:
    VB's API viewer has the wrong declaration of a SYSTEM_INFO structure (maybe it's not the wrong one, but the way it's declared, it won't give you the CPU level and revision).

    Look at this C declaration of the SYSTEM_INFO:
    Code:
    typedef struct _SYSTEM_INFO { 
      union { 
        DWORD  dwOemId; 
        struct { 
          WORD wProcessorArchitecture; 
          WORD wReserved; 
        }; 
      }; 
      DWORD  dwPageSize; 
      LPVOID lpMinimumApplicationAddress; 
      LPVOID lpMaximumApplicationAddress; 
      DWORD  dwActiveProcessorMask; 
      DWORD  dwNumberOfProcessors; 
      DWORD  dwProcessorType; 
      DWORD  dwAllocationGranularity; 
      WORD  wProcessorLevel; 
      WORD  wProcessorRevision; 
    } SYSTEM_INFO;
    Now, look at the API's viewer declaration:
    Code:
    Private Type SYSTEM_INFO
            dwOemID As Long
            dwPageSize As Long
            lpMinimumApplicationAddress As Long
            lpMaximumApplicationAddress As Long
            dwActiveProcessorMask As Long
            dwNumberOrfProcessors As Long
            dwProcessorType As Long
            dwAllocationGranularity As Long
            dwReserved As Long
    End Type
    Notice in C declaration the Union keyword? It tells the compiler that the element of that structure would be either the dwOemId or the structure of wProcessorArchitecture and wReserved (It will take the biggest value for the memory storage), BUT...VB doesn't have that luxury. (Using Union), plus in MSDN Library it says that dwOemId is obsolete, which is true but it won't work without it in VB. So, it was just a matter of time porting this declaration to VB. Once again....always use MSDN....sometimes it has all the answers, sometimes not.

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width