none
Public Function Help

    Question

  • Public Function IsUserlandConnected() Dim forever As Boolean = True Dim USBName As String = String.Empty Dim USBSearcher As New ManagementObjectSearcher( "root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'") For Each queryObj As ManagementObject In USBSearcher.Get() USBName += (queryObj("Description")) Next If USBName = "Apple Mobile Device USB Driver" Then Return True Else Return False End If End Function


    I used this code before to detect if a iPhone is plugged into my computer. I'm not sure why it's not working anymore tho. Can anyone help me fix it? Also is there anyway to read information from it and put the info into a label? Thanks
    Monday, March 27, 2017 7:03 PM

Answers

  • Hi

    OK, try this. It should list everything in the ListBox. Can you see your entity listed there?

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Management
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            Dim boo As Boolean = IsUserlandConnected()
        End Sub
        Public Function IsUserlandConnected() As Boolean
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                 ListBox1.Items.Add(queryObj.ToString)
    
                ' I don't have an Apple Mobile Device USB Driver
                ' so just for this example using 'thermalzone'
                If queryObj.ToString.Contains("Apple Mobile Device USB Driver") Then
                    ListBox1.Items.Add("iPhone Connected: Yes")
                    Return True
                End If
            Next
            ListBox1.Items.Add("iPhone Connected: No")
            Return False
        End Function


    Regards Les, Livingston, Scotland

    • Marked as answer by TimeFluid Tuesday, March 28, 2017 12:56 AM
    Monday, March 27, 2017 10:22 PM

All replies

  • Hi

    This might help (needs ListBox1 on Form):

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Management
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            Dim boo As Boolean = IsUserlandConnected()
        End Sub
        Public Function IsUserlandConnected() As Boolean
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                ' I don't have an Apple Mobile Device USB Driver
                ' so just for this example using 'thermalzone'
                If queryObj.ToString.ToLower.Contains("thermalzone") Then
                    ListBox1.Items.Add(queryObj.ToString)
                    Return True
                End If
            Next
            Return False
        End Function


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, March 27, 2017 8:05 PM
    Monday, March 27, 2017 8:04 PM
  • I get a red line under ManagementObjectSearcher and ManagementObject?
    Monday, March 27, 2017 8:21 PM
  • I get a red line under ManagementObjectSearcher and ManagementObject?

    What does the error message say in the popup when you hover the mouse
    cursor over the red line?

    - Wayne

    Monday, March 27, 2017 8:28 PM
  • I get a red line under ManagementObjectSearcher and ManagementObject?

    Hi

    Did you insert this line?

    Imports System.Management


    Regards Les, Livingston, Scotland

    Monday, March 27, 2017 8:29 PM
  • Yes.
    Monday, March 27, 2017 8:42 PM
  • Monday, March 27, 2017 8:43 PM
  • What happened when you used Alt+Enter or Ctrl+.?

    Monday, March 27, 2017 8:48 PM
  • Hi

    Works fine here. Using Visual Studio 2017 Community.


    Regards Les, Livingston, Scotland

    Monday, March 27, 2017 8:49 PM
  • Have you added a Reference to System.Management to the project?

    Open the project properties, click on "References" in the left pane.
    If it doesn't show in the list of references, click on the "Add" button.
    Scroll down the list of available References until you get to
    System.Management and put a checkmark in the square box at its left.
    Click OK.

    - Wayne
    Monday, March 27, 2017 8:54 PM
  • Hi, 

    I had a same king of problem like you this morning. 

    I've a Gillette electric razor, the electric function did not run anymore. 

    However, 2 days ago I had done a new battery in it. 

    I added again a new battery. 

    The first one was probably old, it did run again.

    Therefore after all the replies which seems to work. 

    Are you sure the IPhone is still functioning?

    :-)


    Success
    Cor

    Monday, March 27, 2017 8:59 PM
  • Works after adding the references. So now I'm a bit confused tho. The original code I used showed if my iPhone was plugged in. If it was a label would change to Connected and if the iPhone wasn't plugged in it would say wasn't connected. How can I get that back to working? Here's the code.

    Public Function IsUserlandConnected()
            Dim forever As Boolean = True
            Dim USBName As String = String.Empty
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName += (queryObj("Description"))
            Next
            If USBName = "Apple Mobile Device USB Driver" Then
                Return True
            Else
                Return False
            End If
        End Function

    Also thanks to everyone that's helping me with this!

    Monday, March 27, 2017 9:17 PM
  • Works after adding the references. So now I'm a bit confused tho. The original code I used showed if my iPhone was plugged in. If it was a label would change to Connected and if the iPhone wasn't plugged in it would say wasn't connected. How can I get that back to working? Here's the code.

    Public Function IsUserlandConnected()
            Dim forever As Boolean = True
            Dim USBName As String = String.Empty
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName += (queryObj("Description"))
            Next
            If USBName = "Apple Mobile Device USB Driver" Then
                Return True
            Else
                Return False
            End If
        End Function

    Also thanks to everyone that's helping me with this!

    It should work in the example I posted. You need to alter the text: where I used 'thermalzone', you would substitute your string. Then, the call to the function will return True or False as you need.

    Regards Les, Livingston, Scotland

    Monday, March 27, 2017 9:20 PM
  • I've been messing around with it... I'll be honest I'm confused. It's been awhile since I've done any visual basic. Here's what I changed, but it's always showing no phone plugged in. I'm sorry for my lack of knowledge with this.

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Management
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            Dim boo As Boolean = IsUserlandConnected()
        End Sub
        Public Function IsUserlandConnected() As Boolean
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                ' I don't have an Apple Mobile Device USB Driver
                ' so just for this example using 'thermalzone'
                If queryObj.ToString.ToLower.Contains("Apple Mobile Device USB Driver") Then
                    'ListBox1.Items.Add(queryObj.ToString)
                    ListBox1.Items.Add("iPhone Connected: Yes")
                    Return True
                End If
            Next
            ListBox1.Items.Add("iPhone Connected: No")
            Return False
        End Function
    End Class

    Monday, March 27, 2017 9:52 PM
  • Hi

    Try this change

    If queryObj.ToString.Contains("Apple Mobile Device USB Driver") Then


    Regards Les, Livingston, Scotland

    Monday, March 27, 2017 9:57 PM
  • Still not working..

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Management
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            Dim boo As Boolean = IsUserlandConnected()
        End Sub
        Public Function IsUserlandConnected() As Boolean
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                ' I don't have an Apple Mobile Device USB Driver
                ' so just for this example using 'thermalzone'
                If queryObj.ToString.Contains("Apple Mobile Device USB Driver") Then
                    'ListBox1.Items.Add(queryObj.ToString)
                    ListBox1.Items.Add("iPhone Connected: Yes")
                    Return True
                End If
            Next
            ListBox1.Items.Add("iPhone Connected: No")
            Return False
        End Function

    Monday, March 27, 2017 10:13 PM
  • Hi

    OK, try this. It should list everything in the ListBox. Can you see your entity listed there?

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Management
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            Dim boo As Boolean = IsUserlandConnected()
        End Sub
        Public Function IsUserlandConnected() As Boolean
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                 ListBox1.Items.Add(queryObj.ToString)
    
                ' I don't have an Apple Mobile Device USB Driver
                ' so just for this example using 'thermalzone'
                If queryObj.ToString.Contains("Apple Mobile Device USB Driver") Then
                    ListBox1.Items.Add("iPhone Connected: Yes")
                    Return True
                End If
            Next
            ListBox1.Items.Add("iPhone Connected: No")
            Return False
        End Function


    Regards Les, Livingston, Scotland

    • Marked as answer by TimeFluid Tuesday, March 28, 2017 12:56 AM
    Monday, March 27, 2017 10:22 PM
  • Still not working..

    Instead of just making arbitrary changes, use the debugger and step through the code to determine what's happening.  For instance, insert a breakpoint at the For Each ... line, end examine the USBSearcher object in the watch window.  How many result objects are returned and what are their values? 

    Monday, March 27, 2017 10:23 PM
  • Alright. I got it all working. I have another question tho. Is there away to constantly be checking if the iPhone is plugged in? I was thinking about just a time to constantly check, but I feel like that's going to lag out the program. I was looking for away for it to run CODE to check if the iPhone is unplugged or plugged in anytime.

    CODE: The code I'm talking about it running all the time.

    If IsUserlandConnected.Equals(True) Then
                'iPhone is connected
                Label1.Text = "iPhone is connected."
            Else
                If IsUserlandConnected.Equals(False) Then
                    'iPhone isn't connected
                    Label1.Text = "iPhone isn't connected."
                End If
            End If
    Thanks

    Tuesday, March 28, 2017 12:34 AM