-
Oct 22nd, 2015, 08:31 PM
#1
Thread Starter
Fanatic Member
Get remote users
I am looking for a way to retrieve all users (or more so there username) that are currently running a instance of my application.
The application will be installed on a mapped drive, so all users will be just firing up a new instance of the application across our network from a shortcut on there desktop.
I've read about some API's to maybe get this done, but got quickly confused.
Basically I want to be able to hit a button to open a new form with a listbox on it that is populated with the current users. I would also need to be able to kill there instance to do Database maintenance if needed.
Sorry I don't have anything started yet as this is still in the conceptual stage for me.
Anyone know of any good tuts out there to accomplish this? In the mean time I'll start designing the form.
Life is about making some things happen, not waiting around for something to happen.
-
Oct 22nd, 2015, 09:16 PM
#2
Thread Starter
Fanatic Member
Re: Get remote users
So here's what I would like to accomplish, only getting the multipal users that are using the app.
Also I found out I needed a listview instead of a listbox.
Code:
Public Class frmCurrentUsers
Private Sub frmCurrentUsers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Get the Machine Name
Dim PCName As String = Environment.MachineName
' Get the current logged in user name
Dim User As String = Environment.UserName
' Set up an array of string
Dim str(2) As String
' Setup a listviewitem object
Dim lstviewitem As ListViewItem
' Add strings to the array
Str(0) = PCName
str(1) = User
' Create a new listviewitem object with the array
lstviewitem = New ListViewItem(str)
' Add the array to the listview
ListView1.Items.Add(lstviewitem)
End Sub
Life is about making some things happen, not waiting around for something to happen.
-
Oct 23rd, 2015, 03:04 AM
#3
Re: Get remote users
the easiest will be that your application logs machinename and username to a table whenever it is started and removes the entry when it is shut down. Add a datetime to the record as well so you can filter out the ones where the app crashed or was shot down via tskmgr and the entry was therefore left over. to shut down the remote process you can use WMI where you can list processes running on a remote machine and terminate them if you got administrative Access.
the more complicate way will be to get the processes which lock the exe file saved in the Network Folder. as you may have noted, the exe cannot be deleted or renamed while it is running on some PC in your Network so the Server knows that someone got a handle to that file. it should also know which Client machine this handle was created on. however AFAIK this is far more complicated than my first Suggestion.
-
Oct 23rd, 2015, 08:19 AM
#4
Thread Starter
Fanatic Member
Re: Get remote users
Originally Posted by digitalShaman
the easiest will be that your application logs machinename and username to a table whenever it is started and removes the entry when it is shut down. Add a datetime to the record as well so you can filter out the ones where the app crashed or was shot down via tskmgr and the entry was therefore left over. to shut down the remote process you can use WMI where you can list processes running on a remote machine and terminate them if you got administrative Access.
Lets start with how to log the users and machine names.
I've tried a For next loop on environmental.username.count but for some reason the count was 7.
So obviously I do not have a clue how to do this on a remote machine. Can you please give me some guidance.
Life is about making some things happen, not waiting around for something to happen.
-
Oct 27th, 2015, 06:16 PM
#5
Thread Starter
Fanatic Member
Re: Get remote users
Ok so I got a table set up and it does whats it's supposed to. Add a User and Machine name on Login and deletes them on exit.
Now for the WMI terminate thing.
WOW, I have tried allot and can't seem to come up with anything that works for remote.
I use the WMICodeCreator to come up with this, but I get The RPC server is unavailable. (Exception from HRESULT: 0x800706BA), Was getting a Application was blocked by Group Policy but for some reason that went away, go figure.
anyway here is the code. Hope someone can help.
Code:
Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click
Try
Dim connection As New ConnectionOptions
connection.Username = userNameBox.Text
connection.Password = passwordBox.Text
connection.Authority = "ntlmdomain:CITY-ALO"
Dim scope As New ManagementScope("\\*\root\CIMV2", connection)
scope.Connect()
Dim classInstance As New ManagementObject(scope, _
New ManagementPath("Select * from Win32_Process Where Name = " & "notepad.exe"), _
Nothing)
' Obtain [in] parameters for the method
Dim inParams As ManagementBaseObject = _
classInstance.GetMethodParameters("Terminate")
' Add the input parameters.
' Execute the method and obtain the return values.
Dim outParams As ManagementBaseObject = _
classInstance.InvokeMethod("Terminate", inParams, Nothing)
' List outParams
Console.WriteLine("Out parameters:")
Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
Close()
Catch err As ManagementException
MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
Catch unauthorizedErr As System.UnauthorizedAccessException
MessageBox.Show("Connection error (user name or password might be incorrect): " & unauthorizedErr.Message)
End Try
End Sub
Life is about making some things happen, not waiting around for something to happen.
-
Oct 28th, 2015, 02:27 AM
#6
Re: Get remote users
it's been a while since i was doing WMI stuff and that was with VB6, so there might be a more elegant way to do this. however this worked for me:
Code:
Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim connection As New ConnectionOptions
connection.Username = "***"
connection.Password = "***"
connection.Authority = "ntlmdomain:***"
Dim scope As New ManagementScope("\\COMPUTERNAME\root\CIMV2", connection)
scope.Connect()
Dim searcher As New ManagementObjectSearcher(scope, New ObjectQuery("Select * from Win32_Process WHERE Name='Notepad.exe'"))
For Each classInstance As ManagementObject In searcher.Get
Dim inParams As ManagementBaseObject = _
classInstance.GetMethodParameters("Terminate")
Dim outParams As ManagementBaseObject = _
classInstance.InvokeMethod("Terminate", inParams, Nothing)
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
Catch unauthorizedErr As System.UnauthorizedAccessException
MessageBox.Show("Connection error (user name or password might be incorrect): " & unauthorizedErr.Message)
End Try
End Sub
it did shut down Notepad on a remote Computer. the first Problem i spotted with your code is that you got to use a compurtername when you create your managementScope, an asterisk is not valid here. You would take the machinename from your database table and insert it where my code says "COPUTERNAME". The second issue is that a ManagementPath is not a WMI Query, so i changed that to use an ManagementObjectSearcher.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|