runner-ralf
Goto Top

NullReferenzeException

Ich will endlich von VB6 auf VB.Net umsteigen. Aber in meinem Alter(49) geht alles etwas langsamer face-smile

Hallo alle zusammen,

ich bin nicht gerade Fit in VB.Net und darum kann ich mit dem Fehler nichts anfangen. Unter VBA läuft das Ding aber unter VB.Net bleibt der Curser bei Next stehen und brungt eben diese Fehlermeldung. Ich weiß nicht was der Compiler von mir will..... Habe im Forum was gelesen das ich bei den Variablen vor Object noch das Wort New schreiben muss. Hat aber auch nichts geholfen.

Könnt ihr mir einen Tipp geben?

Gruß

Ralf

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim objWMI As New Object
Dim objInstances As New Object
Dim objInstance As New Object
Dim strServer As String
strServer = "PCL09077283"

objWMI = GetObject("winmgmts://" & strServer & "/root\cimv2")
objInstances = objWMI.InstancesOf("Win32_OperatingSystem", 48)

On Error Resume Next
For Each objInstance In objInstances
MsgBox(objInstance.BootDevice)
MsgBox(objInstance.BuildNumber)
MsgBox(objInstance.BuildType)
MsgBox(objInstance.Caption)
MsgBox(objInstance.CodeSet)
MsgBox(objInstance.CountryCode)
MsgBox(objInstance.CreationClassName)
MsgBox(objInstance.CSCreationClassName)
MsgBox(objInstance.CSDVersion)
MsgBox(objInstance.CSName)
MsgBox(objInstance.CurrentTimeZone)
MsgBox(objInstance.DataExecutionPrevention_32BitApplications)
MsgBox(objInstance.DataExecutionPrevention_Available)
MsgBox(objInstance.DataExecutionPrevention_Drivers)
MsgBox(objInstance.DataExecutionPrevention_SupportPolicy)
MsgBox(objInstance.Debug)
MsgBox(objInstance.Description)
MsgBox(objInstance.Distributed)
MsgBox(objInstance.EncryptionLevel)
MsgBox(objInstance.ForegroundApplicationBoost)
MsgBox(objInstance.FreePhysicalMemory)
MsgBox(objInstance.FreeSpaceInPagingFiles)
MsgBox(objInstance.FreeVirtualMemory)
MsgBox(objInstance.InstallDate)
MsgBox(objInstance.LargeSystemCache)
MsgBox(objInstance.LastBootUpTime)
MsgBox(objInstance.LocalDateTime)
MsgBox(objInstance.Locale)
MsgBox(objInstance.Manufacturer)
MsgBox(objInstance.MaxNumberOfProcesses)
MsgBox(objInstance.MaxProcessMemorySize)
MsgBox(objInstance.Name)
MsgBox(objInstance.NumberOfLicensedUsers)
MsgBox(objInstance.NumberOfProcesses)
MsgBox(objInstance.NumberOfUsers)
MsgBox(objInstance.Organization)
MsgBox(objInstance.OSLanguage)
MsgBox(objInstance.OSProductSuite)
MsgBox(objInstance.OSType)
MsgBox(objInstance.OtherTypeDescription)
MsgBox(objInstance.PAEEnabled)
MsgBox(objInstance.PlusProductID)
MsgBox(objInstance.PlusVersionNumber)
MsgBox(objInstance.Primary)
MsgBox(objInstance.ProductType)
MsgBox(objInstance.QuantumLength)
MsgBox(objInstance.QuantumType)
MsgBox(objInstance.RegisteredUser)
MsgBox(objInstance.SerialNumber)
MsgBox(objInstance.ServicePackMajorVersion)
MsgBox(objInstance.ServicePackMinorVersion)
MsgBox(objInstance.SizeStoredInPagingFiles)
MsgBox(objInstance.Status)
MsgBox(objInstance.SuiteMask)
MsgBox(objInstance.SystemDevice)
MsgBox(objInstance.SystemDirectory)
MsgBox(objInstance.SystemDrive)
MsgBox(objInstance.TotalSwapSpaceSize)
MsgBox(objInstance.TotalVirtualMemorySize)
MsgBox(objInstance.TotalVisibleMemorySize)
MsgBox(objInstance.Version)
MsgBox(objInstance.WindowsDirectory)
On Error GoTo 0
Next '!!!!!!!!!!!!!!!!!!!!!! DA bleibt das Programm stehen
End Sub

Content-Key: 152533

Url: https://administrator.de/contentid/152533

Printed on: April 25, 2024 at 00:04 o'clock

Member: MonoTone
MonoTone Oct 14, 2010 at 14:40:40 (UTC)
Goto Top
Wenn du von VB6 auf .NET umsteigen willst.
Dann richtig!
Es nützt nix einfach VB6 Code in einer .NET Entwicklungsumgebung zu schreiben.

Mal ein Ansatz für WMI unter .NET:

Zu allererst fügst du dir einen Verweis für System.Management hinzu.

Dann Importierst du System.Management
Imports System.Management
Module Module1
    Function Wmi() As String
        Dim ReturnStr As String = String.Empty
        Try
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _  
                "SELECT * FROM Win32_OperatingSystem")  
            For Each queryObj As ManagementObject In searcher.Get()
                For Each prop As PropertyData In queryObj.Properties
                    If prop.Value IsNot Nothing Then
                        ReturnStr &= prop.Name & ": " & prop.Value.ToString & vbCrLf  
                    End If
                Next
            Next
        Catch err As ManagementException
            ReturnStr = ("An error occurred while querying for WMI data: " & err.Message)  
        End Try
        Return ReturnStr
    End Function
    Sub Main()
        Console.Write(Wmi)
       Console.Write(vbCrLf & " Drücke eine beliebige Taste zum beenden")  
        Console.Read()
    End Sub
End Module

Dies ist eine kleine Konsolenanwendung, die dir die Verwendung von WMI in .NET verdeutlichen soll.
Damit kannst du lokal sämtlich WMI Daten abrufen. Dazu musst du nur den ObjectSearcher anpassen. Momentan fragst du die 'Klasse' Win32_OperatingSystem im NameSpace "roor\CIMV2" ab.

Du kannst dir die Funktion auch leicht umschreiben.
ZB
Private Function wmi(Byval WMIClass as String)
 Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _  
                "SELECT * FROM " & WMIClass)  
End Function

Die Ausgabe erfolgt dann in der Console die mit Read() pausiert wird.
Das ganze hier ist eine einfache Konsolenanwendung

Zum Umstieg noch ein paar Worte:

On Error etc ist obsolet und du solltest es NIE MEHR in .NET Anwendungen verwenden.
Vermeiden sollte man Variablendeklaration als Object ( vergelichbar mit any in vb6)
Sondern wenn möglich immer mit dem korrekten Datentypen deklarieren.
New ist für die Erstellung von Objekten aus Klassen fast immer notwendig.
Die Verwendung ist oben auch vorhanden:
Dim searcher As New ManagementObjectSearcher
Würden wir das New weglassen, dann hätten wir lediglich eine Deklaration
Dim searcher As ManagementObjectSearcher
Dies weisst den Kompiler daraufhin, wieviel Speicher dem Objekt reserviert wird.
searcher ist momentan ein deklariertes aber nicht instanziertes Objekt.
Mit anderen Worten, es ist NOTHING.
Wollen wir mit diesem Objekt arbeiten, dann müssen wir es instanzieren:

der Konstruktor JEDER KLASSE ist NEW.
Daher
Dim searcher As New ManagementObjectSearcher
Der Konstruktor für ManagementObjectSearcher erlaubt die Übergabe einiger Parameter.
In meinem Beispiel Übergebe ich den ManagementScope und ObjectQuery als String.
Korrekterweise müsste es eigentlich so aussehen:
 Dim search As New ManagementObjectSearcher()
            search.Query = New ObjectQuery("SELECT * FROM Win32_OperatingSystem")  
            search.Scope = New ManagementScope("root\CIMV2")  

Du siehst, auch ObjectQuery und ManagementScope sind Klassen. Um ein Objekt daraus zu erzeugen MUSS
man wieder das Schlüsselwort New verwenden.


Um .NET besser zu verstehen, solltest du vll selber mal eine ganz einfache Klasse schreiben.
Dich mit Schlüsselwörtern wie Shared, Private, Public, Inherit usw beschäftigen.
Dann kommt das Gespür für die Klassen und Objekte in .NET wie von selbst

MfG Mono