GetObject和VB6 ActiveX exe [英] GetObject and VB6 ActiveX exe

查看:262
本文介绍了GetObject和VB6 ActiveX exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB6 有关GetObject的帮助说你不能使用GetObject获得对使用Visual Basic创建的类的引用(最后一句!)。我的VB6 GUI公开的对象作为ActiveX的EXE,供其他组件操纵。我想要其他组件连接到已经运行的GUI,而不是启动一个新的exe实例。如果你使用这种语法,我发现使用GetObject是有效的:

The VB6 help on GetObject says "You can't use GetObject to obtain a reference to a class created with Visual Basic" (the very last sentence!). My VB6 GUI exposes objects as an ActiveX exe, for other components to manipulate. I want the other components to connect to the GUI that's already running, rather than start a new instance of the exe. I've found using GetObject does work, if you use this syntax:

Set myobj = GetObject("", "ProjectName.ClassName")

它让我担心的是,帮助说这不应该工作,虽然我做了有点测试,到目前为止还没有发现任何问题。任何COM专家在那里谁可以告诉我是否会遇到问题下线?我可以用CreateObject吗?

It worries me that the help says this shouldn't work, although I have done quite a bit of testing and haven't found any problems so far. Any COM experts out there who can tell me whether I going to run into problems down the line? And would I be OK with CreateObject anyway?

ActiveX exe设置是:线程池只有一个线程。该类有MultiUse实例化。这些设置可能足以阻止CreateObject启动一个新的exe实例。是正确的吗?

The ActiveX exe settings are: thread pool with only one thread. The class has MultiUse instancing. It's possible these settings are enough to prevent CreateObject starting a new instance of the exe anyway. Is that correct?

推荐答案

文档很混乱,但是正确。您引用的MSDN页面有助于解释为什么您的 GetObject 调用不会引发错误:

The documentation is confusing, but correct. The MSDN page you reference helps to explain why your GetObject call doesn't throw an error:


如果 路径名 [第一个参数]是零长度字符串
( GetObject 返回指定类型的新对象
实例。如果省略
pathname参数,
GetObject 返回指定类型的当前活动的
对象。如果没有指定类型的
对象存在,则
会出现错误。

If pathname [the first argument] is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

GetObject "", "ProjectName.ClassName

实际上等效于

CreateObject "ProjectName.ClassName"

也就是说,传递一个空字符串到 GetObject 使它像 CreateObject 一样操作,这意味着它将创建一个新实例类,而不是返回一个引用到已经在运行的实例。

That is to say, passing an empty string to the first parameter of GetObject makes it operate exactly like CreateObject, which means it will create a new instance of the class, rather than returning a reference to an already-running instance.

回到MSDN摘录,它提到省略 GetObject 的第一个参数将会导致 GetObject 返回对已经运行的实例的引用(如果存在),这样的调用将如下所示:

Going back to the MSDN excerpt, it mentions that omitting the first argument to GetObject altogether will cause GetObject to return a reference to an already-running instance, if one exists. Such a call would look like this:

GetObject , "ProjectName.ClassName" 'Note nothing at all is passed for the first argument'

但是,如果尝试这样做,您将立即得到运行时错误。这是文档所指的用例,当它表示 GetObject 不能与VB6创建的类一起使用。

However, if you try to do this, you will immediately get a run-time error. This is the use-case that the documentation is referring to when it says that GetObject doesn't work with classes created with VB6.

这不起作用的原因是因为 GetObject 执行它的魔法。当省略第一个参数时,它将通过查询运行对象表(ROT)(一个包含运行COM对象的机器范围查找表)来尝试返回现有对象实例。问题是,对象必须通过创建它们以便其他进程访问的进程显式地注册在运行对象表中 - VB6运行时不会在ROT中注册ActiveX EXE类,因此 GetObject 无法检索对已经运行的实例的引用。

The reason this doesn't work is due to how GetObject performs its magic. When the first parameter is omitted, it tries to return an existing object instance by consulting the Running Object Table (ROT), a machine-wide lookup table that contains running COM objects. The problem is that objects have to be explicitly registered in the Running Object Table by the process that creates them in order to be accessible to other processes - the VB6 runtime doesn't register ActiveX EXE classes in the ROT, so GetObject has no way to retrieve a reference to an already-running instance.

这篇关于GetObject和VB6 ActiveX exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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