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

发布于 2024-10-08 18:54:19 字数 2140 浏览 1 评论 0原文

我正在为 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' 为 null 或不是对象”。

I'm working on creating a COM callable wrapper for the System.Net.NetworkInformation.Ping class for use in WSH scripts. I've gotten it to compile, but it's not working when called from WSH. I'm not very familiar with COM, so please forgive me if the error is obvious.

Here is the code in my assembly:

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() { }
}

}

Here is the code I'm using to call the assembly in WSH:

var ping1 = new ActiveXObject("net.digital_traffic.interop.wrapper.Ping");

WScript.Echo(ping1.Send("127.0.0.1"));

The above gives me "'ping1' is null or not an object".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2024-10-15 18:54:19

这些可能会有所帮助:

注册我的 .net 程序集COM 互操作性不会公开我的方法

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

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

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

These may help:

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

Also, can you give information about how you're specifying your ProgID, and how you're registering your control - can then update the answer with more relevant suggestions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文