• <small id='aEc0W'></small><noframes id='aEc0W'>

      <legend id='aEc0W'><style id='aEc0W'><dir id='aEc0W'><q id='aEc0W'></q></dir></style></legend>

          <bdo id='aEc0W'></bdo><ul id='aEc0W'></ul>
        <i id='aEc0W'><tr id='aEc0W'><dt id='aEc0W'><q id='aEc0W'><span id='aEc0W'><b id='aEc0W'><form id='aEc0W'><ins id='aEc0W'></ins><ul id='aEc0W'></ul><sub id='aEc0W'></sub></form><legend id='aEc0W'></legend><bdo id='aEc0W'><pre id='aEc0W'><center id='aEc0W'></center></pre></bdo></b><th id='aEc0W'></th></span></q></dt></tr></i><div id='aEc0W'><tfoot id='aEc0W'></tfoot><dl id='aEc0W'><fieldset id='aEc0W'></fieldset></dl></div>
        <tfoot id='aEc0W'></tfoot>

        为 COM 互操作公开 dll

        时间:2023-07-26

        <tfoot id='DA2Ex'></tfoot>
        <legend id='DA2Ex'><style id='DA2Ex'><dir id='DA2Ex'><q id='DA2Ex'></q></dir></style></legend>
          <bdo id='DA2Ex'></bdo><ul id='DA2Ex'></ul>
            • <i id='DA2Ex'><tr id='DA2Ex'><dt id='DA2Ex'><q id='DA2Ex'><span id='DA2Ex'><b id='DA2Ex'><form id='DA2Ex'><ins id='DA2Ex'></ins><ul id='DA2Ex'></ul><sub id='DA2Ex'></sub></form><legend id='DA2Ex'></legend><bdo id='DA2Ex'><pre id='DA2Ex'><center id='DA2Ex'></center></pre></bdo></b><th id='DA2Ex'></th></span></q></dt></tr></i><div id='DA2Ex'><tfoot id='DA2Ex'></tfoot><dl id='DA2Ex'><fieldset id='DA2Ex'></fieldset></dl></div>

                <tbody id='DA2Ex'></tbody>

                <small id='DA2Ex'></small><noframes id='DA2Ex'>

                  本文介绍了为 COM 互操作公开 dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我以为我知道如何做到这一点,但显然不知道,所以我很感激一些帮助!我无法注册我的 dll,因此我可以在 VBS 或其他地方实例化它.

                  I thought I knew how to do this, but obviously not so I'd appreciate some help! I can't get my dll to register so I can instantiate it in a VBS, or elsewhere.

                  我编写了以下示例类,选中使程序集 COM 可见",选中注册 COM 互操作",然后构建它.当我尝试从 VBS 实例化它时,出现Activex 组件无法创建对象"错误.

                  I wrote the following sample class, checked "Make assembly COM Visible", checked "Register for COM Interop", then built it. When I try to instantiate it from VBS I get the "Activex component can't create object" error.

                  这是课程代码:

                  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 - 没有帮助!

                  Forgot to mention, after the first failure I tried REGSVR32 and REGASM - no help!

                  [/编辑]

                  请注意,当我尝试 REGSVR32 时,我会收到以下消息:

                  Note that when I try REGSVR32, I get this message:

                  模块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;
                          }
                      }
                  }
                  

                  推荐答案

                  这里可能有一些不同的东西在起作用.首先,您需要在提升的命令提示符下使用 regasm 工具和/codebase/tlb 开关(假设是 Windows Vista、7 或 Windows Server 2008).比如:

                  There could be a few different things at play here. First, you'll want to use the regasm tool with the /codebase /tlb switch from an elevated command prompt (assuming Windows Vista, 7 or Windows Server 2008). Something like:

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

                  一旦您使用 regasm 注册了 dll,您应该能够使用 VBS、VBA 或 VB6 调用它.

                  Once you have registered the dll using regasm you should be able to invoke it using VBS, VBA or VB6.

                  我能够使用 VBA 中的早期绑定和后期绑定来调用 Explode 方法.但是,当我尝试使用 VBScript 时,我收到ActiveX 无法像您一样创建对象错误".

                  I was able to use early binding and late binding from VBA to call the Explode method. However, when I tried from VBScript I received the "ActiveX can't create object error as you did."

                  我在 Windows 7 64 位上运行,我记得在编译为 32 位 dll 并在 64 位操作系统上运行它们时,这可能会导致问题.一时兴起,我启动了命令提示符并输入:

                  I'm running on Windows 7 64 bit, and I recalled that this can cause problems when compiling to 32 bit dlls and running them on 64 bit operating systems. On a whim, I fired up a command prompt and entered:

                  C:WindowsSysWow64CScript.exe "Path to VBScript"
                  

                  结果是脚本正确运行并在屏幕上显示Pop".

                  The result was that the script ran correctly and displayed "Pop" on screen.

                  这是我使用的稍微简化的 C# 代码以及 VBScript 文件的内容.

                  Here's the somewhat simplified C# code I used as well as the contents of the VBScript file.

                  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
                  

                  这篇关于为 COM 互操作公开 dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:生成 Word 文档时检索 COM 类工厂的组件错误 下一篇:如何将暴露给 COM 互操作的 .NET 对象标记为单线程?

                  相关文章

                1. <legend id='AR1qI'><style id='AR1qI'><dir id='AR1qI'><q id='AR1qI'></q></dir></style></legend>
                  <tfoot id='AR1qI'></tfoot>

                    <bdo id='AR1qI'></bdo><ul id='AR1qI'></ul>
                  1. <small id='AR1qI'></small><noframes id='AR1qI'>

                  2. <i id='AR1qI'><tr id='AR1qI'><dt id='AR1qI'><q id='AR1qI'><span id='AR1qI'><b id='AR1qI'><form id='AR1qI'><ins id='AR1qI'></ins><ul id='AR1qI'></ul><sub id='AR1qI'></sub></form><legend id='AR1qI'></legend><bdo id='AR1qI'><pre id='AR1qI'><center id='AR1qI'></center></pre></bdo></b><th id='AR1qI'></th></span></q></dt></tr></i><div id='AR1qI'><tfoot id='AR1qI'></tfoot><dl id='AR1qI'><fieldset id='AR1qI'></fieldset></dl></div>