当调用 COM/ActiveX 方法时,Matlab 中 NULL 的等价物是什么?

发布于 2024-08-22 00:33:39 字数 1293 浏览 4 评论 0原文

我维护一个可以通过 COM 自动化的程序。一般来说,客户使用 VBS 来编写脚本,但我们有一些客户使用 Matlab 的 ActiveX 支持,并且在使用 NULL 参数调用 COM 对象方法时遇到问题。

他们询问如何在 Matlab 中做到这一点 - 我一直在搜索 Mathworks 的 COM/ActiveX 文档 已经研究了一天左右,但无法弄清楚。

他们的示例代码可能如下所示:

function do_something()
   OurAppInstance = actxserver('Foo.Application');
   OurAppInstance.Method('Hello', NULL)
end

其中 NULL 是在另一种语言中,我们会写 NULLnilNothing,或者,当然,传入一个对象。问题是这是可选的(在大多数情况下,这些方法都作为可选参数实现,但不是全部) - 这些方法期望经常获得 NULL。

他们告诉我他们已经尝试过 [] (从我的阅读来看这似乎是最有可能的)以及 ''Nothing“无”Null0。我不知道其中有多少是有效的 Matlab 关键字 - 当然在这种情况下没有一个有效。

有人可以帮忙吗? Matlab 将空指针/对象用作 COM 方法参数的语法是什么?

更新:感谢迄今为止的所有回复!不幸的是,所有答案似乎都不起作用,甚至 libpointer 也不起作用。在所有情况下,错误都是相同的:

错误:类型不匹配,参数 2

COM 类型库中的此参数在 RIDL 中描述为:

    HRESULT _stdcall OurMethod([in] BSTR strParamOne, [in, optional] OurCoClass* oParamTwo, [out, retval] VARIANT_BOOL* bResult);

所涉及的 coclass 实现了从 IDispatch 派生的单个接口。

I maintain a program which can be automated via COM. Generally customers use VBS to do their scripting, but we have a couple of customers who use Matlab's ActiveX support and are having trouble calling COM object methods with a NULL parameter.

They've asked how they do this in Matlab - and I've been scouring Mathworks' COM/ActiveX documentation for a day or so now and can't figure it out.

Their example code might look something like this:

function do_something()
   OurAppInstance = actxserver('Foo.Application');
   OurAppInstance.Method('Hello', NULL)
end

where NULL is where in another language, we'd write NULL or nil or Nothing, or, of course, pass in an object. The problem is this is optional (and these are implemented as optional parameters in most, but not all, cases) - these methods expect to get NULL quite often.

They tell me they've tried [] (which from my reading seemed the most likely) as well as '', Nothing, 'Nothing', None, Null, and 0. I have no idea how many of those are even valid Matlab keywords - certainly none work in this case.

Can anyone help? What's Matlab's syntax for a null pointer / object for use as a COM method parameter?

Update: Thanks for all the replies so far! Unfortunately, none of the answers seem to work, not even libpointer. The error is the same in all cases:

Error: Type mismatch, argument 2

This parameter in the COM type library is described in RIDL as:

    HRESULT _stdcall OurMethod([in] BSTR strParamOne, [in, optional] OurCoClass* oParamTwo, [out, retval] VARIANT_BOOL* bResult);

The coclass in question implements a single interface descending from IDispatch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆离笙 2024-08-29 00:33:39

在与 Matlab 技术支持人员交谈后,我在这里回答我自己的问题:没有相当于 Nothing 的东西,Matlab 不支持这一点。

详细信息:Matlab 确实支持可选参数,但不支持传入变体 NULL 指针(实际上,为了准确遵循 VB 的 Nothing 工作方式,我认为是 VT_EMPTY 变体),无论是作为可选参数还是不是。有关于一些空/指针类型的文档,其中很多在我的问题或各种答案中都提到过,但这些似乎无法通过它们的 COM 支持来使用。

Matlab 支持人员为我提供了一种解决方法,使用他们创建的 COM DLL 和 Excel 创建一个可以在脚本中传递的虚拟无对象。我还没有设法让这个解决方法/黑客工作,即使不幸的是,我也可能无法重新分发它。但是,如果您遇到同样的问题,此描述至少可以为您提供一个起点!

编辑

有可能这篇旧新事物博客文章可能相关。 (我不再访问有问题的源代码,或访问 Matlab,以刷新我的记忆或进行测试。)

简而言之,对于 IUnknown (或派生的)参数,您需要一个 [unique] 属性使它们合法地为 NULL。上述声明要求 Matlab 创建或传递一个 VT_EMPTY 变体,但它无法做到这一点。也许添加 [unique] 可能会提示 Matlab 引擎传入 NULL 指针(或包含 NULL 指针的变体),相反 - 假设它能够做到这一点,这是猜测。

这都是猜测,因为这段代码及其复杂性已经落后我好几年了。不过,我希望它对未来的读者有所帮助。

I'm answering my own question here, after talking to Matlab tech support: There is no equivalent of Nothing, and Matlab does not support this.

In detail: Matlab does support optional arguments, but does not support passing in variant NULL pointers (actually, to follow exactly how VB's Nothing works, a VT_EMPTY variant, I think) whether as an optional argument or not. There is documentation about some null / pointerish types, a lot of which is mentioned in my question or in various answers, but these don't seem to be useable with their COM support.

I was given a workaround by Matlab support using a COM DLL they created and Excel to create a dummy nothing object that could be passed around in scripts. I haven't managed to get this workaround / hack working, and even if I had unfortunately I probably could not redistribute it. However, if you encounter the same problem this description might give you a starting point at least!

Edit

It is possible this Old New Thing blog post may be related. (I no longer work with access to the problematic source code, or access to Matlab, to refresh my memory or to test.)

Briefly, for IUnknown (or derived) parameters, you need a [unique] attribute for them to legally be NULL. The above declaration required Matlab create or pass in a VT_EMPTY variant, which it couldn't do. Perhaps adding [unique] may have prompted the Matlab engine to pass in a NULL pointer (or variant containing a NULL pointer), instead - assuming it was able to do that, which is guesswork.

This is all speculation since this code and the intricacies of it are several years behind me at this point. However, I hope it helps any future reader.

游魂 2024-08-29 00:33:39

mathworks 文档中,您可以使用 libpointer 函数:

p = libpointer;

然后 p 将是一个 NULL 指针。请参阅该页面了解更多详细信息。

另请参阅:有关 libpointer 的更多信息

From the mathworks documentation, you can use the libpointer function:

p = libpointer;

and then p will be a NULL pointer. See that page for more details.

See also: more information about libpointer.

夏日落 2024-08-29 00:33:39

Peter 的答案应该可行,但您可能想要尝试的是 NaN,这是 Matlab 通常使用的 NULL 值。

Peter's answer should work, but something you might want to try is NaN, which is what Matlab ususally uses as a NULL value.

随遇而安 2024-08-29 00:33:39

除了使用 [] 和 libpointer(如 Peter 建议)之外,您还可以尝试 {}。

In addition to using [] and libpointer (as suggested by Peter), you can also try {}.

时光礼记 2024-08-29 00:33:39

对于 VB 中需要 Nothing 参数的内容,正确的答案是以某种方式获取具有 VT_EMPTY 变体类型的 COM/ActiveX Variant >。 (请参阅 MSDN 文档,其中引用了封送行为对于 Visual Basic Nothing

MATLAB 可能使用空数组 ([]) 执行此操作,但我不确定......所以纯粹在 MATLAB 中可能无法实现。尽管有人可以轻松编写一个小型 COM 库,其目的是创建带有 VT_EMPTY 的 Variant。

但是,如果参数具有[可选] 属性,并且您希望将该可选参数留空,则不应该这样做。请参阅有关变体的 COM/ActiveX 文档,其中在 VT_EMPTY 下显示:

VT_EMPTY:未指定值。如果自动化方法的可选参数留空,则不要传递 VT_EMPTY 类型的 VARIANT。相反,传递一个 VT_ERROR 类型且值为 DISP_E_PARAMNOTFOUND 的 VARIANT。

Matlab应该(但可能没有)提供创建这些对象的方法(“无”和“可选空白”),以便您可以与 COM 对象正确交互。

The correct answer for something in VB that is expecting a Nothing argument, is to somehow get a COM/ActiveX Variant which has a variant type of VT_EMPTY. (see MSDN docs which reference marshaling behavior for Visual Basic Nothing)

MATLAB may do this with the empty array ([]), but I'm not sure.... so it may not be possible purely in MATLAB. Although someone could easily write a tiny COM library whose purpose is to create a Variant with VT_EMPTY.

But if the argument has the [optional] atttribute, and you want to leave that optional argument blank, you should not do this. See the COM/ActiveX docs on Variants which say under VT_EMPTY:

VT_EMPTY: No value was specified. If an optional argument to an Automation method is left blank, do not pass a VARIANT of type VT_EMPTY. Instead, pass a VARIANT of type VT_ERROR with a value of DISP_E_PARAMNOTFOUND.

Matlab should (but probably does not) provide methods to create these objects (a "nothing" and an "optional blank") so you can interface correctly with COM objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文