Thread: remote execution COM DCOM?

  1. #1
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66

    remote execution COM DCOM?

    hi there,

    i want to know how to get started with a very basic, simple form of distributed computing.

    lets say i have two machines connected on a network. i want one machine to be a 'master' machine, and one to be a slave.

    from the 'master' machine i want to kick off two seperate processes [so not a distributed process, but two independent ones], lets call them myProcess1.exe and myProcess2.exe, one on the master machine and one on the slave machine. these are console based executables that just run from the command line.

    so i know how to run one process on the 'master' machine by using a batch file to call 'myProcess1.exe', but the question is, how do i get the master to call myProcess2.exe' on the other 'slave' machine? is it possible? and does that have anything to do with COM/DCOM...

    i'm not really sure where to get started with this... so any help would be great,

    thanks,
    dug.
    "take the long road.... and walk it."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and does that have anything to do with COM/DCOM...
    I think you could use those for this purpose, but it's not necessary to use them.

    The first thing you need is to get a copy of myProcess2.exe onto the remote machine.

    Then you need some kind of server program running on the slave machine, which can listen for remote instructions. One of those remote instructions (which the master will send) is "run executable.exe".

    There's lots of information in the sticky threads of this forum to help with the low level send and receive over a network.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You may want to check out the DispHelper COM Helper Library. In the wmi.cpp sample file (or wmi.c if you prefer C) you will find a function called StartProcess that can be used to start a process on the local or remote computer. It takes a computer name and a program path. You must have administrator priviliges on the remote computer.

    Another option is Sysinternals' PsExec.

  4. #4
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    thanks wmi.cpp's function StartProcess looks like what i'm talking about... i'll give that a try and let you know what happens!

    thanks alot for your help guys!
    dug.
    "take the long road.... and walk it."

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I'm not sure this will be useful as it is a vbscript rather than C/C++. However, it does exactly what you want. It copies a selected file to the remote computer, executes a command line and returns the result. Copy the code to a text file and name it StartRemote.vbs.
    Code:
    '/* ---------------------------------------------------------------- */'
    Sub PrepareRemote(szComputer, szFile, szCmdLine)
    
    	Set fso = CreateObject("Scripting.FileSystemObject")
    
    	szRemotePath = "\\" & szComputer & "\c$\___temp___\" 
    
    	If (Not fso.FolderExists(szRemotePath)) Then
    		Set f = fso.CreateFolder(szRemotePath)
    		f.Attributes = f.Attributes Or 2 Or 4 'Hidden and System
    	End If
    
    	If (szFile <> "" And _
    	    Not fso.FileExists(szRemotePath & fso.GetFileName(szFile))) Then
    		  fso.CopyFile szFile, szRemotePath & fso.GetFileName(szFile)
    	End If
    
    	Set strm = fso.CreateTextFile(szRemotePath & "run.bat", True)
    
    	strm.WriteLine "CALL " & szCmdLine
    
    	strm.WriteLine "ECHO %ERRORLEVEL% > C:\___temp___\result.txt"
    
    	strm.Close
    End Sub
    
    
    '/* ---------------------------------------------------------------- */'
    Function ReadResult(szComputer, bCleanup)
    
    	Set fso = CreateObject("Scripting.FileSystemObject")
    
    	szRemotePath = "\\" & szComputer & "\c$\___temp___"
    
    	Set strm = fso.OpenTextFile(szRemotePath & "\result.txt")
    
    	ReadResult = strm.ReadAll()
    
    	strm.Close
    
    	If (bCleanup) Then fso.DeleteFolder(szRemotePath)
    End Function
    
    
    '/* ---------------------------------------------------------------- */'
    Function StartProcess(szComputer, szCmdLine)
    
    	szWmiStr = "winmgmts:{impersonationLevel=impersonate}!\\" & _
    	              szComputer & "\root\cimv2"
    
    	szWmiStr = szWmiStr & ":Win32_Process"
    
    	Set wmiProcess = GetObject(szWmiStr)
    
    	nResult = wmiProcess.Create(szCmdLine,,,pid)
    
    	If (nResult <> 0) Then
    		Err.Raise nResult, "Win32_Process::Create", _
    		          "Failed to create remote process."
    	End If
    
    	StartProcess = pid
    End Function
    
    
    '/* ---------------------------------------------------------------- */'
    Sub WaitForTermination(szComputer, pid)
    
    	szWmiStr = "winmgmts:{impersonationLevel=impersonate}!\\" & _
    	              szComputer & "\root\cimv2"
    	
    	Set wmiSvc = GetObject(szWmiStr)
    
    	Set wmiEventSrc = wmiSvc.ExecNotificationQuery( _
    	           "SELECT * FROM __InstanceDeletionEvent WITHIN 1 " & _
    	           "WHERE TargetInstance ISA 'Win32_Process' AND "   & _
    	           "TargetInstance.ProcessId=" & pid)
    
    	wmiEventSrc.NextEvent
    End Sub
    
    
    '/* ---------------------------------------------------------------- */'
    Function StartRemoteProcess(szComputer, szFilePath, szCmdLine, bCleanup)
    
    	Call PrepareRemote(szComputer, szFilePath, szCmdLine)
    
    	pid = StartProcess(szComputer, "C:\___temp___\run.bat")
    
    	Call WaitForTermination(szComputer, pid)
    
    	StartRemoteProcess = ReadResult(szComputer, bCleanup)
    End Function
    
    '' Sample usages (shows pushing out windows updates).
    
    ' Vulnerability in the Microsoft Jet Database Engine Could Allow Code Execution (837001)
    'MsgBox "The result of running the remote program was " & _
    '       StartRemoteProcess("remote_computer", "C:\Documents and Settings\UONE\Desktop\Windows2000-KB837001-x86-ENU.EXE", _
    '       "C:\___temp___\Windows2000-KB837001-x86-ENU.EXE /passive /quiet /norestart /o", False)
    
    ' Cumulative Update for Microsoft RPC/DCOM (828741)
    'MsgBox "The result of running the remote program was " & _
    '       StartRemoteProcess("remote_computer", "C:\Documents and Settings\UONE\Desktop\Windows2000-KB828741-x86-ENU.EXE", _
    '       "C:\___temp___\Windows2000-KB828741-x86-ENU.EXE /passive /quiet /norestart /o", False)
    Note: Both this and the DispHelper based solution will only run on Windows 2000/XP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Remote debug not working in Visual Studio 2003
    By Bassquake in forum Tech Board
    Replies: 12
    Last Post: 08-22-2008, 12:11 PM
  3. Howto make own application for remote control handling
    By s-men in forum Windows Programming
    Replies: 16
    Last Post: 08-16-2008, 04:22 PM
  4. Remote thread problem
    By RubbeR DuckY in forum C++ Programming
    Replies: 6
    Last Post: 08-08-2006, 12:24 PM
  5. DCOM Server - Memory keeps growing
    By freestyle in forum Windows Programming
    Replies: 5
    Last Post: 10-25-2002, 11:00 AM