无法在 64 位应用程序内的 VBA 中实例化使用 Visual Studio 2008 构建的 COM 库
我正在尝试构建一个可以使用 64 位 excel 2010 从 VBA 注册和使用的对象库。我正在使用 Dev Studio 2008。我认为这是一个 64 位问题,因为我很确定当我尝试使用它时这会起作用我以前版本的 Excel (XP)。我已在“程序集信息信息”对话框中单击“使 COM 可见”,并在构建标记中单击“注册 COM 互操作”。当我将目标类型设置为 x64 时,我根本无法在引用对话框中看到该库。如果我选择“任何 CPU”,我可以注册它,但当我尝试实例化它时,出现“无法创建 Active X 对象”错误。
namespace Tester
{
[Guid("D6F88E95-8A27-4ae6-B6DE-0542A0FC7039")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface _Numbers
{
int GetDay();
int GetMonth();
int GetYear();
int DayOfYear();
}
[Guid("13FE32AD-4BF8-495f-AB4D-6C61BD463EA4")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Tester.Numbers")]
public class Numbers : _Numbers
{
public Numbers(){}
public int GetDay()
{
return(DateTime.Today.Day);
}
public int GetMonth()
{
return(DateTime.Today.Month);
}
public int GetYear()
{
return(DateTime.Today.Year);
}
public int DayOfYear()
{
return(DateTime.Now.DayOfYear);
}
}
}
I am trying to build an object library which can be registered and used from VBA using 64 bit excel 2010. I am using Dev Studio 2008. I think this is a 64 bit issue as I am pretty sure this worked when I tried it with with my previous version of Excel (XP). I have clicked "Make COM visible" in the Assembly info Information dialog and "Register for COM interop" in the build tag. When I set the target type to x64 I can't see the library in the references dialog at all. I if I select Any CPU I can register it but I get a "Can't create Active X object" error when I try to instantiate it.
namespace Tester
{
[Guid("D6F88E95-8A27-4ae6-B6DE-0542A0FC7039")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface _Numbers
{
int GetDay();
int GetMonth();
int GetYear();
int DayOfYear();
}
[Guid("13FE32AD-4BF8-495f-AB4D-6C61BD463EA4")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Tester.Numbers")]
public class Numbers : _Numbers
{
public Numbers(){}
public int GetDay()
{
return(DateTime.Today.Day);
}
public int GetMonth()
{
return(DateTime.Today.Month);
}
public int GetYear()
{
return(DateTime.Today.Year);
}
public int DayOfYear()
{
return(DateTime.Now.DayOfYear);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 Visual Studio 是 32 位进程,因此它将错误地运行 32 位版本的 regasm.exe 以注册您的 64 位程序集。当然,该版本的
regasm.exe
将写入注册表的 32 位部分,因此这是行不通的。解决该问题的一种方法是自己在目标程序集上运行 64 位版本的
regasm.exe
(例如,在构建后步骤中)。您需要使用/tlb
选项导出其类型库,并且还需要传递/codebase
选项,因为程序集不驻留在广汽集团:Since Visual Studio is a 32-bit process, it will erroneously run the 32-bit version of regasm.exe in order to register your 64-bit assembly. Of course, that version of
regasm.exe
will write to the 32-bit portion of the registry, so that won't work.One way to solve the problem would be to run the 64-bit version of
regasm.exe
on the target assembly yourself (e.g. in a post-build step). You'll need to export its type library using the/tlb
option, and you'll also need to pass the/codebase
option since the assembly doesn't reside in the GAC: