Click to See Complete Forum and Search --> : compA to tell compB to start/exit program using Vb.net and WMI
remya1000
05-10-2007, 01:36 PM
i need computer A to tell computer B to start or Exit a program. and computer B should do Exit or start while computer A gives start or exit direction.
i'm using VB.NET and WMI. and i'm new to WMI. i dont have anyidea how computer A and connect to computer B.
any idea how can i do this.
if you have any idea please help me.
thanks in advance
Ribeyed
05-10-2007, 02:33 PM
Hi,
create a MangemntClass object for the WMI class and then explore it using properties such as ManagementClass.Methods, ManagementClass.Properties, MethodData.InParameters and MethodData.OutParameters.
You would need to add a reference to System.Management.dll and he import the System.Management namespace.
Not sure it this is what you are wanting.
This may only help you with part of the solution. You may have to combine this with some socket programming. You would need to create a TCP listener and TCP Client using the TCPListener and TCPClient class to open a TCP/IP socket from which you can pass commands etc from PC to PC.
remya1000
05-11-2007, 10:15 AM
i'm tring to start a calculator in a remote machine from my system. and the codes i tried is this.
Module Module1
Sub Main()
Dim retValue As String
retValue = RunCommand("calc.exe", "MachineName", "MachineName\UserID", "Password")
Console.WriteLine(retValue)
End Sub
Function RunCommand(ByVal strCommand As String, ByVal strMachineName As String, ByVal strUserName As String, ByVal strPassword As String) As String
Dim options As New System.Management.ConnectionOptions
options.Username = strUserName
options.Password = strPassword
options.Impersonation = Management.ImpersonationLevel.Impersonate
options.Authentication = Management.AuthenticationLevel.PacketPrivacy
Dim path As New System.Management.ManagementPath("\\" & strMachineName & "\root\cimv2:Win32_Process")
Dim scope As New System.Management.ManagementScope(path, options)
scope.Connect()
Dim opt As New System.Management.ObjectGetOptions
Dim classInstance As New System.Management.ManagementClass(scope, path, opt)
Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters("Create")
inParams("CommandLine") = strCommand
' Execute the method and obtain the return values.
Dim outParams As System.Management.ManagementBaseObject = classInstance.InvokeMethod("Create", inParams, Nothing)
Return "ReturnValue:" & outParams("returnValue") & " Process ID: {0}" & outParams("processId")
End Function
End Module
******while i tried to connect to some machines this error occurs.
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in system.management.dll
Additional information: Access is denied.
*******and to someother machines and to the local machine this error occurs.
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'UInt32' to type 'String' is not valid.
and my local machine the calculator is poping up and this above error occurs. but while connecting to remote machine, to some machine access is denied to some other the above error occurs and calc is not popping up too.
and one have anyidea what's wrong with my codes... please help me.
thanks in advance.
remya1000
05-11-2007, 12:59 PM
i'm able to terminate the program from remote machine's(the program is opened manually). but while trying to open the program, the program is opened in the background in task manager. but i need to pop up the program.
and i commented this line
Return "ReturnValue:" & outParams("returnValue") & " Process ID: {0}" & outParams("processId")
so the error gone now. but need to display the calculator in front. not in taskmanager.
anyidea how to make the calculator display in screen. if u have anyidea please let me know.
thanks in advance
Ribeyed
05-11-2007, 02:45 PM
HI,
have you tried this?
retValue.Visible = True
remya1000
05-14-2007, 09:39 AM
i just tried that code
retValue.Visible = True
but retValue is a string. so visible is not a member of string. so not able to give visible inside that.
any other ideas.... if you have let me know
thanks for your advice.
and thanks in advance.
Ribeyed
05-14-2007, 01:48 PM
hI,
OK i see so it is. Just tried to run your code without success. However the error i am getting is more likely to be a configuration problem more than a coding problem.
Here is the error i got:
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
I check that the services required were running and i double checked the credentials were right so not sure what that can be.
still looking for an answer...
Ribeyed
05-14-2007, 02:12 PM
what are you planning on doing with the calculator once you have the application openned?
remya1000
05-14-2007, 07:20 PM
actually that error
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
occurs b'coz ur computer have firewall.
while i tried to connect to computer having firewall, this error comes. but the computer doesn't have the firewall will open the calculator in taskmanager.
actually opening and closing calculator is an eg:. first i will try to open and close calculator. if its working i need to change the calculator exe and need to place a program.exe. so that from my system i can open that program in remote machine and while i need to close that program i need to close that from my machine. and once that program is opened, then as normal i need to run that program in remote machine. just start and close i need to do from my system.
like in startup page the program is opened automatically. and after that it will do all its functions in that machine.
likewise as just i will send open exe from my machine and that program will work in remote machine. and when i need to close, i will send close exe from my machine and that program will get closed in remote machine.
if you have any idea how to proceed, please let me know.
thanks in advance
remya1000
05-15-2007, 10:26 AM
i got this help from internet.
Dim procStart As ManagementClass = New ManagementClass("Win32_ProcessStartup")
Dim ps As ManagementObject = procStart.CreateInstance
ps("ShowWindow") = 1 'SW_NORMAL --- Activates and displays a window.
inParams("ProcessStartupInformation") = ps
http://msdn2.microsoft.com/en-us/library/aa394375.aspx
and i tried this code in my codes like this...
Module Module1
Sub Main()
Dim retValue As String
retValue = RunCommand("calc.exe", "MachineName", "MachineName\UserID", "Password")
Console.WriteLine(retValue)
End Sub
Function RunCommand(ByVal strCommand As String, ByVal strMachineName As String, _
ByVal strUserName As String, ByVal strPassword As String) As String
Dim options As New System.Management.ConnectionOptions
options.Username = strUserName
options.Password = strPassword
options.Impersonation = Management.ImpersonationLevel.Impersonate
options.Authentication = Management.AuthenticationLevel.PacketPrivacy
Dim path As New System.Management.ManagementPath("\\" & strMachineName & "\root\cimv2:Win32_Process")
Dim scope As New System.Management.ManagementScope(path, options)
Dim procStart As ManagementClass = New ManagementClass("Win32_ProcessStartup")
Dim ps As ManagementObject = procStart.CreateInstance
ps("ShowWindow") = 1
scope.Connect()
Dim opt As New System.Management.ObjectGetOptions
Dim classInstance As New System.Management.ManagementClass(scope, path, opt)
Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters("Create")
inParams("CommandLine") = strCommand
inParams("ProcessStartupInformation") = ps
' Execute the method and obtain the return values.
Dim outParams As System.Management.ManagementBaseObject = classInstance.InvokeMethod("Create", inParams, Nothing)
End Function
End Module
is this the way we need to add Win32_ProcessStartup inside my program.
sorry for disturbing you again. actually i dont have much idea about WMI. i'm new to this. that's why.
if any thing wrong in this codes, please let me know. while running its not creating any errors, but not displaying the calculator in windows. that's why...
if you have nay idea please let me know....
thanks in advance and thanks for your help....
remya1000
05-15-2007, 11:36 AM
still the calculator is opening inside the taskmanager, not pop up in windows. can you have any idea what's wrong in my codes. if you have please let me know how can i display calculator as pop up window in remote machine.
remya1000
05-15-2007, 01:35 PM
i just tried this code too... in a button click. but it too opening calculator in taskmanger insted of popup. if you have any idea please let me know.
Dim processHandle As Integer
Dim connection As New ConnectionOptions
connection.Username = "aa"
connection.Password = "aa"
Dim strMachineName As String
strMachineName = txtMachinename.Text
Dim scope As New ManagementScope("\\" & strMachineName & "\root\CIMV2", connection)
scope.Connect()
Dim processClass = New ManagementClass("Win32_Process")
processClass.Scope = scope
Dim inParams = processClass.GetMethodParameters("Create")
Dim startup = New ManagementClass("WIN32_ProcessStartup")
Dim ps As ManagementObject = startup.CreateInstance
Const SW_NORMAL = 1
ps("ShowWindow") = SW_NORMAL 'SW_NORMAL --- Activates and displays a window.
startup.Scope = scope
inParams("CommandLine") = "calc.exe"
inParams("ProcessStartupInformation") = startup
Dim outParams = processClass.InvokeMethod("Create", inParams, Nothing)
thanks in advance
remya1000
05-16-2007, 02:35 PM
anyidea what's wrong with my codes.
any idea how can i display calculator in remote machine from my machine.
if you have anyidea or anything wrong in my code please let me know about that.
or any other way i can display it other than using WMI.
Actually no idea how to proceed that's why. i'm new to this WMI and all.
Thanks in advance