当调用 COM/ActiveX 方法时,Matlab 中 NULL 的等价物是什么?
我维护一个可以通过 COM 自动化的程序。一般来说,客户使用 VBS 来编写脚本,但我们有一些客户使用 Matlab 的 ActiveX 支持,并且在使用 NULL 参数调用 COM 对象方法时遇到问题。
他们询问如何在 Matlab 中做到这一点 - 我一直在搜索 Mathworks 的 COM/ActiveX 文档 已经研究了一天左右,但无法弄清楚。
他们的示例代码可能如下所示:
function do_something()
OurAppInstance = actxserver('Foo.Application');
OurAppInstance.Method('Hello', NULL)
end
其中 NULL
是在另一种语言中,我们会写 NULL
或 nil
或 Nothing
,或者,当然,传入一个对象。问题是这是可选的(在大多数情况下,这些方法都作为可选参数实现,但不是全部) - 这些方法期望经常获得 NULL。
他们告诉我他们已经尝试过 []
(从我的阅读来看这似乎是最有可能的)以及 ''
、Nothing
、“无”
、无
、Null
和0
。我不知道其中有多少是有效的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在与 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, aVT_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 beNULL
. The above declaration required Matlab create or pass in aVT_EMPTY
variant, which it couldn't do. Perhaps adding [unique] may have prompted the Matlab engine to pass in aNULL
pointer (or variant containing aNULL
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.
在 mathworks 文档中,您可以使用
libpointer
函数:然后
p
将是一个NULL
指针。请参阅该页面了解更多详细信息。另请参阅:有关 libpointer 的更多信息。
From the mathworks documentation, you can use the
libpointer
function:and then
p
will be aNULL
pointer. See that page for more details.See also: more information about libpointer.
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 aNULL
value.除了使用 [] 和 libpointer(如 Peter 建议)之外,您还可以尝试 {}。
In addition to using [] and libpointer (as suggested by Peter), you can also try {}.
对于 VB 中需要
Nothing
参数的内容,正确的答案是以某种方式获取具有VT_EMPTY
变体类型的 COM/ActiveXVariant
>。 (请参阅 MSDN 文档,其中引用了封送行为对于 Visual BasicNothing
)MATLAB 可能使用空数组 (
[]
) 执行此操作,但我不确定......所以纯粹在 MATLAB 中可能无法实现。尽管有人可以轻松编写一个小型 COM 库,其目的是创建带有 VT_EMPTY 的 Variant。但是,如果参数具有
[可选]
属性,并且您希望将该可选参数留空,则不应该这样做。请参阅有关变体的 COM/ActiveX 文档,其中在 VT_EMPTY 下显示:Matlab应该(但可能没有)提供创建这些对象的方法(“无”和“可选空白”),以便您可以与 COM 对象正确交互。
The correct answer for something in VB that is expecting a
Nothing
argument, is to somehow get a COM/ActiveXVariant
which has a variant type ofVT_EMPTY
. (see MSDN docs which reference marshaling behavior for Visual BasicNothing
)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: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.