Results 1 to 7 of 7

Thread: [RESOLVED] Get number of PC's using an exe file on the LAN

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    31

    Resolved [RESOLVED] Get number of PC's using an exe file on the LAN

    Hi. Using visual basic on VS2017.
    I am trying to find out how many pc's on the LAN are currently using an app. The app exe file is called FiF.exe.
    I have tried the following code just to try and see how many computers are on the LAN and it doesn't work. I get a zero return.

    Code:
        'test for pc's on the lan
        Public Const SV_TYPE_ALL As Integer = &HFFFFFFFF
    
        Public Structure SERVER_INFO_101
            Public Platform_ID As Integer
            <MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String
            Public Version_Major As Integer
            Public Version_Minor As Integer
            Public Type As Integer
            <MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String
        End Structure
    
        Public Declare Unicode Function NetServerEnum Lib "Netapi32.dll" (
            ByVal Servername As Integer, ByVal Level As Integer, ByRef Buffer As IntPtr, ByVal PrefMaxLen As Integer,
            ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, ByVal ServerType As Integer,
            ByVal DomainName As String, ByRef ResumeHandle As Integer) As Integer
    
        Public Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As IntPtr) As Integer
    
        Public Function GetNetworkComputers(Optional ByVal DomainName As String = Nothing) As DataTable
            Dim ServerInfo As New SERVER_INFO_101
            Dim MaxLenPref As Integer = -1
            Dim level As Integer = 101
            Dim ResumeHandle As Integer = 0
            Dim ret, EntriesRead, TotalEntries As Integer
            Dim BufPtr As IntPtr
    
            Dim dt As New DataTable
            dt.Columns.Add("IPs")
            dt.Columns.Add("PCs")
            Try
                ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries, SV_TYPE_ALL, DomainName, ResumeHandle)
                If ret = 0 Then
                    For i As Integer = 0 To EntriesRead - 1
                        ServerInfo = Marshal.PtrToStructure(IncrementPointer(BufPtr, i * Marshal.SizeOf(ServerInfo)), GetType(SERVER_INFO_101))
                        Dim Rw As DataRow = dt.NewRow
    
                        Dim IPstring As String = ""
                        For Each ip As Net.IPAddress In Net.Dns.GetHostAddresses(ServerInfo.Name)
                            'Debug.WriteLine(ip.ToString)
                            If Not ip.IsIPv6LinkLocal Then
                                IPstring = IPstring & ip.ToString
                            End If
                        Next
                        Rw(0) = IPstring.ToString
                        Rw(1) = ServerInfo.Name.ToString
                        dt.Rows.Add(Rw)
                    Next
                End If
                NetApiBufferFree(BufPtr)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Return dt
        End Function
    
        Private Shared Function IncrementPointer(ByVal ptr As IntPtr, ByVal i As Integer) As IntPtr
            If IntPtr.Size = 4 Then
                Return New IntPtr(ptr.ToInt32 + i)
            End If
            If IntPtr.Size = 8 Then
                Return New IntPtr(ptr.ToInt64 + i)
            End If
        End Function
    I've searched and googled but cannot find anything helpful.
    Any ideas anybody?
    Cheers

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Get number of PC's using an exe file on the LAN

    You mean exe that will show on the task manager, right?
    You can use the Process class and set the remote computer IP, or (because almost half of the options of the process class do not work remotely) , you can use WMI win32-process https://docs.microsoft.com/en-us/win.../win32-process) . You will probably have to use credentials
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    Lively Member
    Join Date
    Jun 2018
    Posts
    80

    Re: Get number of PC's using an exe file on the LAN

    You could loop trough your range of local ip addresses and ping for valid active devices on your LAN.
    Basic I/O will let you know if it's a PC or another device type.

    Once your have all active computers listed, and presuming they are all Windows system you know the credentials for:
    Code:
    tasklist.exe /S SYSTEM /U USERNAME /P PASSWORD
    You'd be using mostly CMD commands for this but can still do it trough a Visual Basic program using the following:
    Code:
    Process.Start("cmd", "/c YourCode")

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    31

    Re: Get number of PC's using an exe file on the LAN

    Thanks a lot. I will try these two methods and see how I go. I will resolve the post if all good. Cheers

  5. #5
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Get number of PC's using an exe file on the LAN

    If you have access to the source code of fif.exe then I would go a different route. I would modify fif.exe to "ping" a central location every few minutes while it's open. What it pings can be a new exe on a server, a web service, a log file, or a database. That's up to you. It could also send the IP of the computer it is on, the logged in user, how long the app has been opened, or any other number of details that you might want to know.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    31

    Re: Get number of PC's using an exe file on the LAN

    Thanks Maverickz. Yes, that sounds like the best idea. The suggestions from sapator and KBConsole although good, didn't quite work for me.
    I do have the source code for FiF and now that you have shared your thinking I can see how best to do this.
    When the app is first installed the pc registers itself in a sql table holding all pcs with FiF. Then I simply get that list and process it.
    Here's my code if anyone else is interested...
    Code:
        Public Function GetRunningPCCount() As Integer
            Try
                Dim myProcess As MyProcess = New MyProcess()
                Return myProcess.BindToRunningProcesses()
            Catch ex As Exception
                WriteToErrorFile(ex)
                Return 1
            End Try
        End Function
        Public Class MyProcess
            Public Function BindToRunningProcesses() As Integer
                'gets a count of all registered machines running FiF (including this one so at least 1)
                Dim RunningCount As Integer = 1
                Try
                    Dim Dt As DataTable = _Sql.GetAllPCs
                    If Dt Is Nothing Then Return RunningCount
                    If Dt.Rows.Count = 0 Then Return RunningCount
                    For i = 0 To Dt.Rows.Count - 1
                        Dim M As String = Dt.Rows(i).Item("machine")
                        Dim remoteByName As Process() = Process.GetProcessesByName("fif", M)
                        RunningCount += remoteByName.Count
                    Next
                    Return RunningCount
                Catch ex As Exception
                    WriteToErrorFile(ex)
                    Return RunningCount
                End Try
            End Function
        End Class
    And I call it from a button or whatever like this...
    Code:
        Private Sub MnuPcCount_Click(sender As Object, e As EventArgs) Handles MnuPcCount.Click
            Try
                Dim X As New ClsLicensing
                Dim PCCount As Integer = X.GetRunningPCCount
            Catch ex As Exception
                WriteToErrorFile(ex)
            End Try
        End Sub
    Thanks all. I will mark this as resolved.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    31

    Re: [RESOLVED] Get number of PC's using an exe file on the LAN

    Just realised a small problem with the above code.
    The Process.GetProcessesByName line should be in its own try/catch. If the remote computor is turned off it will error because it cant find it.
    Cheers

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