为System.Net.NetworkInformation.Ping创建COM可调用包装器

时间:2010-12-21 13:52:50

标签: c# javascript .net com com-interop

我正在为System.Net.NetworkInformation.Ping类创建一个COM可调用包装器,以便在WSH脚本中使用。我已经把它编译好了,但是当从WSH调用它时它不起作用。我对COM不太熟悉,如果错误很明显,请原谅我。

以下是我的程序集中的代码:

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"));

上面给出了''ping1'为空或不是对象“。

1 个答案:

答案 0 :(得分:0)

这些可能有所帮助:

Registering my .net assembly for COM interoperability doesn't expose my methods

http://www.csharphelp.com/2006/08/building-com-objects-in-c/

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comsourceinterfacesattribute.aspx

另外,您是否可以提供有关如何指定ProgID以及如何注册控件的信息 - 然后可以使用更相关的建议更新答案。