注册COM DLL在VBA中工作,但不在VBScript中 [英] Registered COM DLL works in VBA but not in VBScript

查看:302
本文介绍了注册COM DLL在VBA中工作,但不在VBScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在VS2012上用ATL创建了一个基本的COM DLL。它被称为testCOM.dll,它只有一个类叫做CSimpleObj。 CSimpleObj只有一种方法称为addValues,它添加​​了两个值。



我已经使用Windows 7 64位注册了该DLL。在VBA中,我手动添加对DLL的引用,下面的代码正常工作

  Dim Obj As New testCOMLib.SimpleObj 
MsgBox Obj.addValues(1,2)

它给出了一个数字3的消息。 / p>

现在,如果我运行的vbs包括:

  Dim Obj 
Set Obj = CreateObject(testCOMLib.SimpleObj)

它不断给出错误,无法创建对象。但是,如果我使用CreateObject方法的Excel.Application ProgID(作为示例),则可以正常工作。



我认为注册DLL有一个问题。我已经检查了注册表,COM和类型库的密钥已经存在了。



我该怎么办?

解决方案

有三个基本要求可以让您的COM服务器从VBScript中使用:




  • 您需要实现IDispatch,因此支持晚期绑定。 ATL通常不是一个问题,它在使用向导时默认实现。


  • 您的注册表脚本(.rgs)必须将ProgId写入注册表。值得注意的是,您没有报告找到它(查找 HKCR\testCOMLib.SimpleObj 与Regedit.exe进行验证)。其CLSID子项必须与注册表中的CLSID匹配,因此COM可以找到您的DLL。 ATL简单对象向导有一个陷阱,它在输入短名称时填写所有字段。除了ProgID字段。所以很容易忘记。


  • 在64位版本的Windows上,您将默认执行64位版本的cscript.exe。它将无法找到您的32位COM服务器。您需要构建x64版本的服务器,否则您需要使用位于c:\windows\syswow64的32位版本的cscript.exe。通常不是VBA的问题,因为它往往是从32位版本的Office使用。




SysInternals 'Process Monitor实用程序对于诊断这些问题非常有用。您将看到cscript.exe在注册表中搜索您的服务器注册的密钥。可能没有找到它们,但是您对实际的错误信息不够明确。


I have created a basic COM DLL with ATL on VS2012. It's called testCOM.dll and it only has one class called CSimpleObj. CSimpleObj has just one method called addValues which adds two values.

I've registered the DLL with Windows 7 64-bit. In VBA, I manually add a reference to the DLL and the below code works properly

Dim Obj As New testCOMLib.SimpleObj
MsgBox Obj.addValues(1,2)

And it give a message with number 3.

Now If I run the vbs which includes:

Dim Obj
Set Obj = CreateObject("testCOMLib.SimpleObj")

It constantly gives an error and is not able to create the object. But if I use the "Excel.Application" ProgID (as an example) for the CreateObject method, it works fine.

I think there is an issue with registering the DLL. I've checked the registry, and the keys for the COM and the type library is already there.

What should I do?

解决方案

There are three basic requirements to get your COM server to be usable from VBScript:

  • You need to implement IDispatch so late binding is supported. Not usually a problem with ATL, it implements it by default when you use the wizards.

  • Your registry script (.rgs) must write the ProgId to the registry. Notable is that you didn't report finding it (look for HKCR\testCOMLib.SimpleObj with Regedit.exe to verify). Its CLSID subkey must match the CLSID in the registry so COM can find your DLL. The ATL Simple Object wizard has a trap, it fills in all the fields when you type the short name. Except for the ProgID field. So very easy to forget.

  • On the 64-bit version of Windows, you'll execute the 64-bit version of cscript.exe by default. It will not be able to find your 32-bit COM server. You'll need to either build the x64 version of your server or you'll need to use the 32-bit version of cscript.exe, located in c:\windows\syswow64. Not usually a problem with VBA since it tends to be used from a 32-bit version of Office.

The SysInternals' Process Monitor utility is very useful to diagnose these kind of problems. You'll see cscript.exe searching the registry for the keys that your server registered. And probably not finding them, you were however not explicit enough about actual error message you got.

这篇关于注册COM DLL在VBA中工作,但不在VBScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆