无法从 VBA 实例化用 C# 编写的 COM 对象(VB6 ok)

0

本文介绍了无法从 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14