remote.vb Imports SystemPublic Class ServiceClass Inherits MarshalByRefObject Private m_starttime As DateTime Public Sub New() Console.WriteLine("ServiceClass created without constructor. Instance hash is " & Me.GetHashCode().ToString()) m_starttime = DateTime.Now End Sub Overrides Protected Sub Finalize() Console.WriteLine("I'm being collected after " & (New TimeSpan(DateTime.Now.Ticks - m_starttime.Ticks)).ToString() & " seconds.") MyBase.Finalize() End Sub Public Function GetServerTime() As DateTime Console.WriteLine("Time requested by a client.") Return DateTime.Now End Function
Public Sub StartProcess() System.Diagnostics.Process.Start("C:\abc.exe") End Sub Public ReadOnly Property InstanceHash() As Integer Get Return Me.GetHashCode() End Get End Property End Class Server.vb Imports System Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.HttpPublic Class ServerProcess <MTAThread()> _ Public Shared Sub Main() Dim channel As New HttpChannel(8080) ChannelServices.RegisterChannel(channel) Dim object1 As New ServiceClass() ' Creates the single instance of ServiceClass. All clients ' will use this instance. Dim ref1 As ObjRef = RemotingServices.Marshal(object1, "object1uri") Console.WriteLine("ObjRef.URI: " & ref1.URI) Console.WriteLine("Running. Press Enter to end publication.") Console.ReadLine() ' This unregisters the object from publication, but leaves ' the channel listening. RemotingServices.Disconnect(object1) Console.WriteLine() Console.WriteLine("Disconnected the object. Client now receives a RemotingException.") Console.WriteLine("Press Enter to unregister the channel.") Console.ReadLine() ' At this point, the ServerClass object still exists. The server ' could republish it. ' This unregisters the channel, but leaves the application ' domain running. ChannelServices.UnregisterChannel(channel) Console.WriteLine("Unregistered the channel. Client now receives a WebException.") ' The ServerClass object still exists. The server could ' reregister the channel and republish the object. Console.WriteLine("The host application domain is still running. Press Enter to stop the process.") Console.ReadLine() ' The ServiceClass object's Finalize method writes a message to ' the console. A single object will almost always succeed in ' running its Finalize method before the Console is finalized; ' in a larger application, you could ensure that all objects ' finalize before the application ends by calling the garbage ' collector and waiting. GC.Collect() GC.WaitForPendingFinalizers() End Sub
End Class Client.vb Imports System Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Tcp Imports System.Runtime.Remoting.Channels.HttpPublic Class ClientProcess <MTAThread()> _ Public Shared Sub Main()
Dim channel As New HttpChannel(0) ChannelServices.RegisterChannel(channel) ' Registers the remote class. (This could be done with a ' configuration file instead of a direct call.) RemotingConfiguration.RegisterWellKnownClientType(Type.GetType("ServiceClass, remote"), "http://localhost:8080/object1uri") ' Instead of creating a new object, this obtains a reference ' to the server's single instance of the ServiceClass object. Dim object1 As ServiceClass = New ServiceClass() Try Console.WriteLine("ServerTime: " & object1.GetServerTime()) object1.StartProcess() Catch ex As Exception Console.WriteLine("Exception of type: " & ex.GetType.ToString & " occurred.") Console.WriteLine("Details: " & ex.Message) End Try End Sub ' Main End Class ' ClientProcess
To: pupo, 非常感谢您这么热心的帮忙,毕竟像您这样的人已经不多了。我想我已经理解你的意思了,就是说在目标机器上预先放置一个demon进程,这样就可以让它来运行所给目录下的文件了。但是,我这里可能还需要从本地机器向目标机器上COPY文件,这个恐怕就不能通过这样的方法来实现了,大概WMI是个比较通用的方法。
public class RemoteStartClass: System.MarshalByRefObject { public RemoteStartClass() {
}
public void CopyFile(byte[] fileByte) { //这里根据客户端带入的参数生成文件 } public void RunExeFile() { System.Diagnostics.Process.Start("notepad.exe"); } }
http://chs.gotdotnet.com/quickstart/howto/doc/ProcessDemo.aspx然后有remoting基础的话应该就知道怎么做了http://chs.gotdotnet.com/quickstart/howto/doc/Remoting/mainfeatures.aspx
Imports SystemPublic Class ServiceClass
Inherits MarshalByRefObject Private m_starttime As DateTime Public Sub New()
Console.WriteLine("ServiceClass created without constructor. Instance hash is " & Me.GetHashCode().ToString())
m_starttime = DateTime.Now
End Sub Overrides Protected Sub Finalize()
Console.WriteLine("I'm being collected after " & (New TimeSpan(DateTime.Now.Ticks - m_starttime.Ticks)).ToString() & " seconds.")
MyBase.Finalize()
End Sub Public Function GetServerTime() As DateTime
Console.WriteLine("Time requested by a client.")
Return DateTime.Now
End Function
Public Sub StartProcess()
System.Diagnostics.Process.Start("C:\abc.exe")
End Sub Public ReadOnly Property InstanceHash() As Integer
Get
Return Me.GetHashCode()
End Get
End Property End Class
Server.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.HttpPublic Class ServerProcess
<MTAThread()> _
Public Shared Sub Main() Dim channel As New HttpChannel(8080)
ChannelServices.RegisterChannel(channel) Dim object1 As New ServiceClass() ' Creates the single instance of ServiceClass. All clients
' will use this instance.
Dim ref1 As ObjRef = RemotingServices.Marshal(object1, "object1uri")
Console.WriteLine("ObjRef.URI: " & ref1.URI) Console.WriteLine("Running. Press Enter to end publication.")
Console.ReadLine() ' This unregisters the object from publication, but leaves
' the channel listening.
RemotingServices.Disconnect(object1)
Console.WriteLine()
Console.WriteLine("Disconnected the object. Client now receives a RemotingException.")
Console.WriteLine("Press Enter to unregister the channel.")
Console.ReadLine()
' At this point, the ServerClass object still exists. The server
' could republish it. ' This unregisters the channel, but leaves the application
' domain running.
ChannelServices.UnregisterChannel(channel)
Console.WriteLine("Unregistered the channel. Client now receives a WebException.")
' The ServerClass object still exists. The server could
' reregister the channel and republish the object.
Console.WriteLine("The host application domain is still running. Press Enter to stop the process.")
Console.ReadLine() ' The ServiceClass object's Finalize method writes a message to
' the console. A single object will almost always succeed in
' running its Finalize method before the Console is finalized;
' in a larger application, you could ensure that all objects
' finalize before the application ends by calling the garbage
' collector and waiting.
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Class
Client.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.HttpPublic Class ClientProcess
<MTAThread()> _
Public Shared Sub Main()
Dim channel As New HttpChannel(0)
ChannelServices.RegisterChannel(channel) ' Registers the remote class. (This could be done with a
' configuration file instead of a direct call.)
RemotingConfiguration.RegisterWellKnownClientType(Type.GetType("ServiceClass, remote"), "http://localhost:8080/object1uri") ' Instead of creating a new object, this obtains a reference
' to the server's single instance of the ServiceClass object.
Dim object1 As ServiceClass = New ServiceClass() Try
Console.WriteLine("ServerTime: " & object1.GetServerTime())
object1.StartProcess()
Catch ex As Exception
Console.WriteLine("Exception of type: " & ex.GetType.ToString & " occurred.")
Console.WriteLine("Details: " & ex.Message)
End Try End Sub ' Main
End Class ' ClientProcess
然后再运行!!
options.Username = "user";
options.Password = "password";
options.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope(
@"\\remoteMachine\root\cimv2", options);
scope.Connect();
ManagementPath path = new ManagementPath("Win32_Process");
ManagementClass process = new ManagementClass(
scope, path, new ObjectGetOptions());
process.InvokeMethod("Create",
new object[] {"notepad.exe", null, null, 0}));
编译和运行该示例 在命令提示处键入以下命令:
vbc -t:library remote.vb vbc -r:System.Runtime.Remoting.dll -r:remote.dll server.vb vbc -r:System.Runtime.Remoting.dll -r:remote.dll client.vb 打开两个指向同一目录的命令提示。在其中一个键入 server。在另一个键入 client。
若要分步取消发布该可远程处理对象,请在服务器命令提示下按 ENTER 键并重新运行客户端以观察为不同步骤引发的不同异常。该应用程序可在单台计算机上运行或通过网络运行。如果要在网络上运行该应用程序,必须用远程计算机的名称替换客户端配置中的“localhost”。
2、在解决方案中添加类库项目 RemoteStartClass (作为远程对象),将默认Class1.cs删除,新建个类 RemoteStartClass.cs
using System;namespace RemoteStart
{
/// <summary>
/// RemoteStartClass 的摘要说明。
/// </summary>
public class RemoteStartClass: System.MarshalByRefObject
{
public RemoteStartClass()
{
} public void RunExeFile()
{
System.Diagnostics.Process.Start("notepad.exe");
}
}
}3、在解决方案中添加应用程序项目 RemoteStartServer (作为服务器端程序)
添加对项目RemoteStartClass及System.Runtime.Remoting的引用
Form1.cs 如下
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
namespace RemoteStart
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(208, 181);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
RemoteStartClass rsc = null;
HttpChannel channel = null;
private void Form1_Load(object sender, System.EventArgs e)
{
channel = new HttpChannel(10000);
ChannelServices.RegisterChannel(channel); rsc = new RemoteStartClass();
ObjRef rscref = RemotingServices.Marshal(rsc, "RemoteStartUri") ; } private void Form1_Closed(object sender, System.EventArgs e)
{
RemotingServices.Disconnect(rsc);
ChannelServices.UnregisterChannel(channel);
}
}
}
添加对项目RemoteStartClass及System.Runtime.Remoting的引用
Form1.cs 如下
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;namespace RemoteStart
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 23);
this.button1.TabIndex = 0;
this.button1.Text = "启动远程程序";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(184, 165);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} RemoteStartClass rsc = null;
HttpChannel channel = null; private void button1_Click(object sender, System.EventArgs e)
{
try
{
rsc.RunExeFile();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
} private void Form1_Load(object sender, System.EventArgs e)
{
channel = new HttpChannel(0);
ChannelServices.RegisterChannel(channel); //URI 替换为相应服务器名(或IP)及端口即可,端口对应RemoteStartServer中注册的端口
RemotingConfiguration.RegisterWellKnownClientType(Type.GetType("RemoteStart.RemoteStartClass, RemoteStartClass"), "http://LFY:10000/RemoteStartUri"); rsc = new RemoteStartClass();
} private void Form1_Closed(object sender, System.EventArgs e)
{
ChannelServices.UnregisterChannel(channel);
}
}
}编译后调试
将RemoteStartClass.dll,RemoteStartServer.exe部署在服务器端,RemoteStartClass.dll,RemoteStartClient.exe部署在客户端,
服务器端运行RemoteStartServer启动服务后,客户端运行RemoteStartClient后点击“运行远程程序”按纽,服务器将会打开一个记事本
非常感谢您这么热心的帮忙,毕竟像您这样的人已经不多了。我想我已经理解你的意思了,就是说在目标机器上预先放置一个demon进程,这样就可以让它来运行所给目录下的文件了。但是,我这里可能还需要从本地机器向目标机器上COPY文件,这个恐怕就不能通过这样的方法来实现了,大概WMI是个比较通用的方法。
{
public RemoteStartClass()
{
}
public void CopyFile(byte[] fileByte)
{
//这里根据客户端带入的参数生成文件
} public void RunExeFile()
{
System.Diagnostics.Process.Start("notepad.exe");
}
}