我正在为System.Net.NetworkInformation.Ping类创建一个COM可调用包装器,以用于WSH脚本。我已经完成了它的编译工作,但是从WSH调用时它不起作用。我对COM不是很熟悉,所以如果错误很明显,请原谅我。为System.Net.NetworkInformation.Ping创建COM Callable Wrapper
这里是我的汇编代码:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.NetworkInformation;
[assembly: AssemblyVersion("0.0.1.0")]
namespace net.digital_traffic.interop.wrapper
{
[ComVisible(true)]
public interface IPing
{
//void OnPingCompleted(PingCompletedEventArgs e);
PingReply Send(IPAddress address);
PingReply Send(String hostNameOrAddress);
PingReply Send(IPAddress address, Int32 timeout);
PingReply Send(String hostNameOrAddress, Int32 timeout);
PingReply Send(IPAddress address, Int32 timeout, Byte[] buffer);
PingReply Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer);
PingReply Send(IPAddress hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options);
PingReply Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options);
/*
void SendAsync(IPAddress address, Object userToken);
void SendAsync(String hostNameOrAddress, Object userToken);
void SendAsync(IPAddress address, Int32 timeout, Object userToken);
void SendAsync(String hostNameOrAddress, Int32 timeout, Object userToken);
void SendAsync(IPAddress address, Int32 timeout, Byte[] buffer, Object userToken);
void SendAsync(String hostNameOrAddress, Int32 timeout, Byte[] buffer, Object userToken);
void SendAsync(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options, Object userToken);
void SendAsync(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options, Object userToken);
void SendAsyncCancel();
*/
}
[ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
public class Ping : System.Net.NetworkInformation.Ping, IPing
{
public Ping() : base() { }
}
}
这里是我使用调用WSH汇编代码:
var ping1 = new ActiveXObject("net.digital_traffic.interop.wrapper.Ping");
WScript.Echo(ping1.Send("127.0.0.1"));
上面给我“‘平1’是空或不是对象“。