为 System.Net.NetworkInformation.Ping 创建 COM 可调用包装器
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些可能会有所帮助:
注册我的 .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.