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.