VB6 中的 createObject 在运行 exe 时失败
我有一个 VB6 程序,它尝试运行用 C# 编写的 DLL。
该 DLL 有一个 COM 接口,因此我可以使用“CreateObject”在其中创建一个类的对象。
问题是,当我从 VB6 IDE 运行它时,它运行良好,但当我创建一个 EXE 并尝试运行它时,它会抛出异常:
“自动化错误。系统找不到指定的文件(-2147024894)。”
为什么会发生这种情况,我该如何解决?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 IDE 中的“项目”、“引用”,并查看哪个 dll 或 ocx 文件属于您使用 CreateObject 引用的对象(对象管理器也可能有助于查找)。
编译 exe 时,该 dll 文件也必须可用。通常,您需要使用 regsvr32.exe 注册它。
Look at Project, References in the IDE and look which dll or ocx file belongs to the object you are referencing with CreateObject (the Object Manager might also help to find out).
This dll file must be available when the exe is compiled, too. Usually, you need to have it registered with regsvr32.exe.
我用来解决此类问题的技术是在 Visual Basic 6 中打开“添加引用”对话框。我滚动可用 COM 库的列表,查看是否列出了有问题的 DLL。如果是,那么 CreateObject 应该可以工作,您应该能够分配它执行变体变体并使用后期绑定来访问它的成员。
此外,尝试暂时设置对变量的引用,而不是使用 CreateObject,而是使用 = New 并查看它会提供哪些错误消息(如果有)。一般来说,我发现它们比 CreateObject 抛出的信息更丰富。
最后,如果您发布选择使用 CreateObject 而不是设置引用的原因,将会有所帮助。如果 DLL 是程序将持续使用的已知对象,则应设置引用并通常使用早期绑定。
最后,该错误可能是由于 C# COM DLL 的依赖性而不是 DLL 本身引起的。例如,如果我要获取一个 Com 库并正确注册它,但它依赖于 COM 库 Widget2000 并且未注册,那么它将抛出自动化错误。特别是如果您在已安装的环境中而不是在编译它的环境中测试 EXE。
例如,假设我有一个用 VB6 编写的 CAD 程序,并且有以 MyCAD 开头的源代码树。 exe 位于 MyCAD/MainEXE 中,形状库位于 MyCAD/ShapeLibrary 中。我运行IDE一切正常。然后我进行设置并转到我的测试机并安装它,但在创建 shapelibrary 时出现错误。
我要做的第一件事是检查 MainEXE 是否会直接从源代码树的 MainEXE 目录中运行。该测试将消除这是安装问题还是 IDE 与编译版本的怪癖。然后我会查看设置并查看哪些内容未注册。另请查看 C# 库的源代码或库的设置,看看它需要哪些依赖项。由于它是一个编译的 COM DLL,因此您应该能够使用依赖项遍历器工具来查看它需要哪些 COM 引用。最后确保安装了正确版本的 .NET Framework。
A technique I use to figure issues of this type is to open the add reference dialog in Visual Basic 6. I scroll the list of available COM Libraries and see if the problem DLL is listed. If it is then CreateObject should work, you should be able to assign it do a variant variant and use late binding to access it's members.
In addition try temporally set a reference to the variable and instead of using CreateObject use the = New and see what error messages, if any, it gives you. Generally I found them to be more informative then the ones thrown by CreateObject.
Finally it would help if you post the reason why you are choosing to use CreateObject instead of setting of a reference. If the DLL is a known object that will be continually used by the program then a reference should be set and early binding generally used.
Finally it may be that the error is resulting from a dependency of the C# COM DLL not the DLL itself. If for example I was to take a Com Library and properly register it but it relies on the COM Library Widget2000 and it NOT registered then it will throw the automation error. Especially if you are testing the EXE in it's installed environment and not the environment in which you complied it.
For example suppose I have a CAD program written in VB6 and I have source tree that begins with MyCAD. THe exe is in MyCAD/MainEXE and the shape library is in MyCAD/ShapeLibrary. I run the IDE everything is fine. Then I make my setup and goto my test machine and install it and it error on the creation of shapelibrary.
The first thing I would do it check if MainEXE will run straight out of the MainEXE directory of my source tree. That test will eliminate whether it is a install issue or a quirk of the IDE vs complied version. Then I would look at the setup and see what not being registered. Also look at either the source for the C# library or the setup for the library and see what dependencies it needs. Since it a complied COM DLL you should be able to use a dependency walker tool to see what COM references it needs. Finally make sure the correct version of the .NET framework is installed.
如果您在测试计算机上编译 C# DLL - 请确保您已勾选 注册 COM 互操作设置。如果您不在同一台计算机上进行编译,则需要运行 RegAsm 带有 /codebase 选项。
If you are compiling the C# DLL on your test machine - make sure you have ticked the register for COM Interop setting. If you are not compiling on the same machine you need to run RegAsm with the /codebase option.
尝试将其编译为安装程序,并在安装程序包的编译中包含您使用的dll/com,以便您使用的dll/com将包含在您的exe的编译中..,并将其安装在Windows中,而不是只需复制过去即可。
try compiling it as an installer and include the dll/com that you use in the compilation of the installer package so that the dll/com that you use will be include in the compilation of your exe.., and install it in the windows not just copy past it.