使用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
这完全一样的code称为从VBA在Excel中提供了
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.
当我叫 CorBindToCurrentRunTime
从VBA,试图强行加载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"
问题解决了!