Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Problem sending stuff to Varibles from WMI

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Resolved [RESOLVED] [2008] Problem sending stuff to Varibles from WMI

    Good Morning,

    This is a bit of a strange one that probally has a really simple answer that almost everyone knows but because I am a begginer I don't know what I am doing. I would bug my collegue with this one as he is the vb.net king here :-p but he is currently in a meeting so was wondering if someone here can help me.

    I am making it as a service but for testings purposes I just copy bits into a Windows Application just to make sure that bit of code works ok. I have got one form and thats it in this test project and this is the code I have in there:

    Form1 Code:
    1. Imports System
    2. Imports System.Management
    3.  
    4. Public Class Form1
    5.  
    6.     Public CPUInfo As String
    7.     Public CPUSpeed As String
    8.     Public CPUCores As String
    9.  
    10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.  
    12.         ttt.Info()
    13.  
    14.         MsgBox("Info: " & CPUInfo & Environment.NewLine & _
    15.                "Speed: " & CPUSpeed & Environment.NewLine & _
    16.                "Cores: " & CPUCores)
    17.  
    18.  
    19.         Application.Exit()
    20.  
    21.  
    22.     End Sub
    23. End Class
    24.  
    25. Public Class ttt
    26.  
    27.     Public Overloads Shared Function Info() As Integer
    28.  
    29.         Try
    30.             Dim searcher As New ManagementObjectSearcher( _
    31.             "root\CIMV2", "SELECT * FROM Win32_Processor")
    32.  
    33.             For Each queryObj As ManagementObject In searcher.Get()
    34.                 CPUInfo = queryObj("Family")
    35.                 CPUInfo = CPUInfo & " " & queryObj("Manufacturer")
    36.                 CPUCores = queryObj("NumberOfCores")
    37.                 CPUSpeed = queryObj("CurrentClockSpeed") '
    38.             Next
    39.         Catch ex As Exception
    40.  
    41.         End Try
    42.  
    43.     End Function
    44.  
    45. End Class

    I have the code in a function to get the info from WMI and put it into Varibles then back in the main sub I just have a simple msgbox to display the values in just to make sure its working.

    When I run the code the message box displays but with no data from the WMI just blank. I have done some troubleshooting and break points and If i put a break point in the function just after the loop and then in the Immediate window I ask for the values of the Varibles and it returns what I am expecting to see however if I put the break point on the first message box and find out what the varibles are they equal Nothing.

    From the Function to the Form Load Event where is it loosing the data. Its probally really simple and something I have done wrong but I just don't understand what.

    Thanks,
    Max
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: [2008] Problem sending stuff to Varibles from WMI

    I have just done an experiment and declared the varibles in a module rather than within the form and the code all works fine now. Can someone try and explain this to me, why has it done it like this?

    Thanks,
    Max
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  3. #3
    Member
    Join Date
    Jun 2008
    Posts
    37

    Re: [2008] Problem sending stuff to Varibles from WMI

    It looks like CPUInfo, CPUSpeed and CPUcores are class level variables in Form1 however you are trying to reference them in class ttt as a shortname, so that equates to ttt.CPUInfo etc...

    Try fully qualifying the names in TTT as Form1.CPUInfo, Form1.CPUSpeed and Form1.CPUCores

    I think if you have Option Explicit on, it would of flagged those as undefined and thrown an error at design time.

    EDIT: damn wanted to post it before you did

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: [2008] Problem sending stuff to Varibles from WMI

    HaHa well thanks anyway I understand what Your saying I worked out how to fix it but I didn't understand why it was happening which you have cleared up for me. I didn't even think about that so thanks :-)
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


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