• <tfoot id='5hWs0'></tfoot>

      <small id='5hWs0'></small><noframes id='5hWs0'>

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

        <legend id='5hWs0'><style id='5hWs0'><dir id='5hWs0'><q id='5hWs0'></q></dir></style></legend>
      2. 无法从 VBA 实例化用 C# 编写的 COM 对象(VB6 ok)

        时间:2023-07-25

            <bdo id='ZYQny'></bdo><ul id='ZYQny'></ul>

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

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

                    <tbody id='ZYQny'></tbody>
                  <tfoot id='ZYQny'></tfoot>
                  本文介绍了无法从 VBA 实例化用 C# 编写的 COM 对象(VB6 ok)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 VS 2008,这是我的 COM 对象

                  Using VS 2008, here is my COM object

                  using System;
                  using System.Collections.Generic;
                  using System.Text;
                  using System.Runtime.InteropServices;
                  using System.Windows.Forms;
                  
                  namespace TestCom
                  {    
                      [Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E")]
                      [ClassInterface(ClassInterfaceType.AutoDual)]
                      [ProgId("Test9.COMINT")]
                      public class TestComClass  
                      { 
                          public void Init(string userid, string password)
                          {
                              MessageBox.Show(string.Format("{0}/{1}", userid, password));
                          }       
                      }
                  }
                  

                  如果我按如下方式构建并在生产机器上注册它

                  If I build this and register it on a production machine as follows

                  REGASM /CODEBASE TESTCOM.DLL
                  

                  从一个简单的 VB6 应用程序可以正常工作

                  From a simple VB6 app this works fine

                  Private Sub Form_Load()
                    Dim o As Object
                    Set o = CreateObject("Test9.COMINT")
                    o.Init "A", "B" 
                  End Sub
                  

                  在 Excel 中从 VBA 调用的完全相同的代码给出了

                  This exact same code called from VBA in Excel gives

                  自动化错误"(0x80131700)

                  "automation error" (0x80131700)

                  在开发机器上一切正常,只是在只安装了 .NET 和 MS Office 的生产机器上不行.

                  Everything works fine on a development machine, just not on a production machine with just .NET and MS Office installed.

                  我认为这与在 Excel 下运行时未正确初始化的 .NET 框架有关.如果我使用 Filemon,我可以看到它四处寻找 MSCORWKS.DLL.当我从 VBScript 调用同一个对象时,它发现 MSCorwks.dll 很好.

                  I think this is something to do with the .NET framework not being initialized properly, when running under Excel. If I use Filemon I can see it skip around looking for MSCORWKS.DLL. When I call the same object from VBScript, it finds MSCorwks.dll fine.

                  当我从 VBA 调用 CorBindToCurrentRunTime 以尝试强制加载 CLR 时,有趣的是,我得到的 HRESULT (0x80131700) 与执行 CreateObject() 在 VBA 中.

                  When I called CorBindToCurrentRunTime from VBA to try to forcibly load the CLR, interestingly I get the exact same HRESULT (0x80131700) as when I do CreateObject() in VBA.

                  因此我认为这是一个框架初始化问题.

                  Therefore I think it is a framework initialization issue.

                  推荐答案

                  我将回答我自己的问题,希望能帮其他人省去我刚刚忍受的乏味苦差事.

                  I'm going to answer my own question, hopefully to spare others the hours of tedious drudgery I have just endured.

                  如果你得到这个,它因为基于 .NET 的 COM 程序集找不到 .NET 框架

                  If you get this, it is because the .NET based COM assembly can't find the .NET framework

                  解决方案很简单.创建一个包含以下内容的文件

                  The solution is simple. Create a file containing the following

                  <?xml version="1.0"?>
                  <configuration>
                    <startup>
                     <supportedRuntime version="v2.0.50727"/>
                    </startup>
                  </configuration>
                  

                  将其命名为Excel.Exe.Config"并将其放在与EXCEL.EXE"相同的目录中

                  Call it "Excel.Exe.Config" and place it in the same directory as "EXCEL.EXE"

                  问题解决了!

                  这篇关于无法从 VBA 实例化用 C# 编写的 COM 对象(VB6 ok)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:有没有人成功地将免注册 COM 与 .NET 组件一起使用? 下一篇:*.tlb 文件是否曾在运行时使用过?

                  相关文章

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

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

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

                      <bdo id='BdCB0'></bdo><ul id='BdCB0'></ul>