我正在爲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’是空或不是對象「。