.Net and VBA (Excel) Interop issue -2147024894 (80070002)

好久没有写博客了

 

这次的问题是,.net下的dll如何在COM中使用。 

本机(win7)开发的时候, COM下,可以直接使用,一点问题也没有,但是导出dll到其他机器(windows xp, win 2003, win 7等)却一直存在如题所示的问题。

原因是在本机开发的时候,Visual Studio 会自动往注册表写一个:

HKEY_CLASSES_ROOT\CLSID\{71F5F31E-D86B-4FF1-89CA-BFCC3B8117BA}\InprocServer32\CodeBase=<path to your assembly>


但是其他机器却没有这个,咋办呢?

解决办法如下:

1. 如果你以前注册过,先unregister old dll

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>RegAsm.exe /unregister ./ClientDLL
/TransferFile.dll
Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.3053
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.

Types un-registered successfully

 

2. 然后再次注册:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>RegAsm.exe /codebase ./ClientDLL/T
ransferFile.dll
Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.3053
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.

Types registered successfully

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>

注意这里:在使用/codebase前, 你的dll必须是已经singed的,什么是signed?看下图:

 

最好,测试:

可以在Excel下,然后Alt+f11直接调出开发界面, 

测试代码:

Public Sub TestApp()
Dim obj As New TransferFile.TransferFile
Dim result As Boolean
result = False
result = obj.UploadFile("c:\xp_test.txt", "xp_test.txt", "D:\Test")
Debug.Print result
End Sub

 

搞定。

这里还有另外一种办法,那就是:

把DLL放到GAC里面。 然后重新注册。 这样注册的时候就可以无需/codebase了。

 

参考文章:http://social.msdn.microsoft.com/Forums/nl-NL/clr/thread/65dc0c4a-a426-41a0-9e80-c7de37b10c73

 
posted @ 2012-04-03 17:25  无尽思绪  阅读(2343)  评论(0编辑  收藏  举报