簡體   English   中英

為COM Interop公開dll

[英]Expose dll for COM Interop

我以為我知道怎么做,但顯然不是這樣,我會感激一些幫助! 我無法讓我的dll注冊,所以我可以在VBS或其他地方實例化它。

我編寫了以下示例類,選中“Make assembly COM Visible”,選中“Register for COM Interop”,然后構建它。 當我嘗試從VBS實例化它時,我得到“Activex組件無法創建對象”錯誤。

這是類代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Smurf
{
    public class Pants
    {
        public string Explode(bool Loud)
        {
            string result;
            if (Loud)
                result = "BANG";
            else
                result = "pop";
            return result;
        }
    }
}

......這是VBS:

Dim a

Set a = CreateObject("Smurf.Pants")

msgbox("ok")

我還需要做什么?

謝謝 :)

[編輯]

忘了提,在第一次失敗后我嘗試了REGSVR32和REGASM - 沒有幫助!

[/編輯]

請注意,當我嘗試REGSVR32時,我收到以下消息:

模塊“C:... \\ Smurf.dll”已加載,但未找到入口點DllRegisterServer。 確保“C:... \\ Smurf.dll”是有效的DLL或OCX文件,然后重試。

這有多大幫助?

這是代碼的最新版本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Smurf
{
    [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
    public interface IPants
    {
        [DispId(1)]
        string Explode(bool Loud);
    }

    [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IPantsEvents
    {
        string Explode(bool Loud);
    }

    [ComVisible(true)]
    [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(IPantsEvents))]
    public class Pants : IPants
    {
        public Pants() { }

        [ComVisible(true)]
        [ComRegisterFunction()]
        public static void DllRegisterServer(string key) { }
        [ComVisible(true)]
        [ComUnregisterFunction()]
        public static void DllUnregisterServer(string key) { }

        [ComVisible(true)]
        public string Explode(bool Loud)
        {
            string result;
            if (Loud)
                result = "BANG";
            else
                result = "pop";
            return result;
        }
    }
}

這里可能會有一些不同的東西。 首先,您需要在提升的命令提示符下使用帶有/ codebase / tlb開關的regasm工具(假設是Windows Vista,7或Windows Server 2008)。 就像是:

regasm "Path to Smurf.dll" /codebase /tlb

使用regasm注冊dll后,您應該可以使用VBS,VBA或VB6調用它。

我能夠使用VBA的早期綁定和后期綁定來調用Explode方法。 但是,當我嘗試使用VBScript時,我收到了“ActiveX無法像你那樣創建對象錯誤”。

我正在使用Windows 7 64位運行,我記得在編譯為32位dll並在64位操作系統上運行時會出現問題。 一時興起,我啟動了一個命令提示符並輸入:

C:\Windows\SysWow64\CScript.exe "Path to VBScript"

結果是腳本正確運行並在屏幕上顯示“Pop”。

這是我使用的簡化的C#代碼以及VBScript文件的內容。

namespace Smurf
{
    [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
    public interface IPants
    {
        string Explode(bool Loud);
    }

    [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
     InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IPantsEvents
    {
        string Explode(bool Loud);
    }

    [ComVisible(true)]
    [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces(typeof(IPantsEvents))]
    public class Pants : IPants
    {

        [ComVisible(true)]
        public string Explode(bool Loud)
        {
            string result;
            if (Loud)
                result = "BANG";
            else
                result = "pop";
            return result;
        }
    }
}

VBScript中:

Dim x 
Set x = CreateObject("Smurf.Pants")
MsgBox (x.Explode(False))
Set x = Nothing

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM