?
Solved

Management.ManagementObjectSearcher is not defined in VB 2005

Posted on 2009-03-31
5
Medium Priority
?
982 Views
Last Modified: 2013-11-26
Each time I reference the code below in VB 2005 I get an error saying that Management.ManagementObjectSearcher is not defined can anyone suggest anything. This code works in Vb 2003
'API Declarations
    Public Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    Public Structure SECURITY_ATTRIBUTES
        Dim nLength As Long
        Dim lpSecurityDescriptor As Long
        Dim bInheritHandle As Long
    End Structure
    'API constants
    Public Const FILE_SHARE_READ = &H1
    Public Const FILE_SHARE_WRITE = &H2
    Public Const OPEN_EXISTING = 3
    Public Const FILE_ATTRIBUTE_NORMAL = &H80
    'Return TRUE if the COM exists, FALSE if the COM does not exist
    Public Function COMAvailable(ByVal COMNum As Integer) As Boolean
        Dim hCOM As Long
        Dim ret As Long
        Dim sec As SECURITY_ATTRIBUTES
 
        'try to open the COM port
        hCOM = CreateFile("COM" & COMNum & "", 0, FILE_SHARE_READ + FILE_SHARE_WRITE, sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
        If hCOM = -1 Then
            COMAvailable = False
        Else
            COMAvailable = True
            'close the COM port
            ret = CloseHandle(hCOM)
        End If
    End Function
    Public Sub DiscoverIndicatorCOMPort()
        Dim result As MsgBoxResult
        Dim strMessage As String
 
        Try
            Dim myWMIClassSearcher As New Management.ManagementObjectSearcher("root\cimv2", "SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%'") 'Get all Serial Ports
            Dim myPorts As Management.ManagementObjectCollection = myWMIClassSearcher.Get()
            'Cycle throug all COM ports on local PC
            For Each myPort As Management.ManagementObject In myPorts
                If myPort.GetPropertyValue("Name") = "Communications Port (COM1)" Then
                    Exit For
                End If
 
                IndicatorCOMPort = myPort.GetPropertyValue("Name")
                Dim myStrings()
                myStrings = Strings.Split(IndicatorCOMPort)
                IndicatorCOMPort = myStrings(3).Trim("("c, ")"c, Chr(34))
                IndicatorCOMPort = IndicatorCOMPort.Trim("C", "O", "M")
                ActiveCOM.Add(IndicatorCOMPort)
                'Exit For
            Next
            Me.IndicatorCOM.Text = "Indicator COM Port: " & IndicatorCOMPort
            Me.mnuSetupIndicatorCOM.Enabled = False
        Catch ex As Exception
            Me.IndicatorCOM.Text = "Status: " & "No Active COM Port Discovered"
 
            strMessage = "No COM Ports Discovered:" & vbCrLf & vbCrLf & "Select [ Yes ] rediscover COM Ports:" & vbCrLf & vbCrLf & "Select [ No ] To exit" & vbCrLf
            result = MsgBox(strMessage, MsgBoxStyle.Information + MsgBoxStyle.YesNo, "")
            If result = MsgBoxResult.Yes Then
                DiscoverIndicatorCOMPort()
            ElseIf result = MsgBoxResult.No Then
                Me.Close()
            End If
        End Try
    End Sub

Open in new window

0
Comment
Question by:cmdolcet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24037167
Have you added the references properly?
0
 

Author Comment

by:cmdolcet
ID: 24038141
What reference????
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24038180
Project menu -> Properties -> References tab. Check to make sure that a valid reference exists for the directoryservices and if not then add it using the add button.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 24038741
For WMI, you would need a reference to the System.Management.dll, which is not added by default to the project.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24040941
Brilliant!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question