在com可见dll中重载方法 [英] Overloading a method in com visible dll

查看:102
本文介绍了在com可见dll中重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个COM可见的dll,并且试图重载一个方法。

I am creating a COM visible dll and I was trying to overload a method.

所以基本上这段代码是:

So basically this code:

[ComVisible(true)]
[ProgId("TAF.TextLog")]
[Guid("af3f89ed-4732-4367-a222-2a95b8b75659")]
public class TextLog
{
    String _logFilePath;

    public TextLog()
    {
    }

    [ComVisible(true)]
    public void Create(string filePath)
    {
        String path = Path.GetDirectoryName(filePath);
        if (Directory.Exists(path))
        {
            _logFilePath = filePath;
        }

    [ComVisible(true)]
    public void Write(string message)
    {
        WriteMessage(null, message, AlertMsg.MsgTypes.Info);
    }

    [ComVisible(true)]
    public void Write(string title, string message, AlertMsg.MsgTypes messageType)
    {
        WriteMessage(title, message, messageType);
    }

    private void WriteMessage(string title, string message, AlertMsg.MsgTypes messageType)
    {
        using (StreamWriter file = new StreamWriter(_logFilePath, true))
        {
            if (title == null)
                file.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss}\t{1}", DateTime.Now, message));
            else
                file.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss}\t{1}\t{2}\t{3}", DateTime.Now, title, message, messageType));
        }
    }
}

看起来像这样是不可能的然而。如果我从调用程序中调用.Write(顺便说一句,这是一个非常简单的VBSCript),我会收到一个错误,提示我参数不正确。

Looks like this is not possible however. If I call .Write from the calling program (which is a very simple VBSCript by the way), i get an error that my parameters are not correct.

这是调用VBscript代码:

This is the calling VBscript code:

Set myObj = CreateObject("TAF.TextLog")
myObj.Create("C:\temp\textlog.txt")
myObj.Write "title", "test message 1", 1

如果我在dll中只有一个.Write方法,它可以正常工作。有人可以告诉我在dll中是否甚至可以进行这样的重载吗?

If I have only one .Write method in the dll it works fine. Can someone tell me if overloading like this is even possible in a dll?

推荐答案

COM不支持成员重载,每个重载都支持名称​​必须是唯一的。 IDispatch的不可避免的副作用:: GetIDsOfNames()。脚本解释器用来将脚本代码中使用的写入转换为dispid的功能。该方法仍然存在,无法获取GetIDsOfNames()以返回其dispid。类型库导出器通过重命名重载方法解决了这个问题,它将是 Write_2()

COM does not have support for member overloads, each name must be unique. An inevitable side-effect of IDispatch::GetIDsOfNames(). The function that the script interpreter uses to translate "Write" as used in the scripting code to a dispid. The method still exists, there's just no way to get GetIDsOfNames() to return its dispid. The type library exporter solves this problem by renaming the overloaded method, it will be Write_2().

使用后期绑定afaik时,没有解决办法,必须避免过载。

No workaround for that when you use late-binding afaik, you must avoid overloads.

这篇关于在com可见dll中重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆