COM 方法调用在 C#、VB.NET 中失败,但在 Python 中有效

发布于 2024-09-27 00:52:28 字数 1064 浏览 4 评论 0原文

我在尝试使用 COM 库时遇到问题。当我调用特定方法并将其传递给 null 时,我收到 ArgumentException。我在 C# 项目和 VB.NET 项目中都发生过这种情况(我使用 Nothing 调用该方法),其中我从 Visual Studio 2008 中的“COM”列表中添加了库引用。当我调用Python 中的相同方法,传入 None,该方法按预期工作,没有错误。据我了解,Python 通过 DCOM 与 COM 库交互(我对这意味着什么只有最模糊的概念),而当我在 C#/VB.NET 项目中引用 COM 库时,我可能会直接使用 COM 库。是否发生了某些事情,导致我传递的参数在到达 COM 库之前就被搞砸了?我不确定这里发生了什么。我最近将 COM 库更新到了新版本,所以我想知道是否在某个地方遇到了版本冲突,从而导致了异常。我从 C# 和 VB.NET 项目中删除了对 COM 库的所有引用,删除了所有 binobj 目录,然后重新添加了引用。这确实导致 obj 中显示的 Interop.MyCOMLibrary.dll 文件具有今天的日期,而不是我看到的旧日期。

我拥有的 COM 库的唯一文档描述了该方法,如下:

Public Function AddItem( _
   ByVal ItemData As Variant _
) As Object

我现在正在尝试搜索有关 Variant 参数的问题。

编辑:因此,虽然 Type.Missing 和其他解决方案可以让我顺利通过该方法调用,但尝试从该方法结果中读取返回项目的某些字符串属性在:

System.Runtime.InteropServices.COMException:项目不存在。

这是另一个例子,它在 Python 中工作,但在 C# 中抛出异常,所以我猜测 DCOM 与 COM 相比有更多的奇怪之处。或者这可能是一个线程问题,因为我正在使用 MSTest 进行测试。

I'm having trouble with a COM library I'm trying to use. I get an ArgumentException when I call a particular method and pass it null. This happens to me in both a C# project and a VB.NET project (I call the method using Nothing) where I added the library reference from the "COM" list in Visual Studio 2008. When I call this same method in Python, passing in None, the method works as expected with no errors. It's my understanding that Python interacts with the COM library through DCOM (and I have only the fuzziest notion what that means) whereas I might be using the COM library directly when I reference it in my C#/VB.NET projects. Could something be happening that would cause the parameter I pass to get screwed up before it gets to the COM library? I'm not sure what's happening here. I recently updated the COM library to a newer version, so I wondered if perhaps I was getting version conflicts in there somewhere, causing the exception. I removed all references to the COM library from my C# and VB.NET projects, deleted all bin and obj directories, and re-added the reference. This did cause the Interop.MyCOMLibrary.dll file that shows up in obj to have today's date instead of the older date I had been seeing.

The only documentation I have for the COM library describes the method as follows:

Public Function AddItem( _
   ByVal ItemData As Variant _
) As Object

I'm trying to search around for issues regarding Variant parameters now.

Edit: so while Type.Missing and the other solutions work for getting me past that method call without error, trying to read certain string properties on the returned item from that method result in:

System.Runtime.InteropServices.COMException: Item does not exist.

This is another example where it worked in Python but is throwing an exception in C#, so I'm guessing more DCOM-versus-COM weirdness. Or perhaps this is a threading issue, since I'm using MSTest to test.

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

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

发布评论

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

评论(4

夢归不見 2024-10-04 00:52:28

是的,null 不太可能是正确的。有几个变体表示“无数据”,例如 VT_EMPTY、VT_ERROR、VT_NULL。首先传递 Type.Missing。

Yes, null is not likely to be correct. There are a couple of Variants that indicate "no data", like VT_EMPTY, VT_ERROR, VT_NULL. Start by passing Type.Missing.

北方的韩爷 2024-10-04 00:52:28

您是否尝试使用 Reflection.Missing 而不是 Nothing/null ?

Did you try using Reflection.Missing instead of Nothing/null?

べ繥欢鉨o。 2024-10-04 00:52:28

我在这篇文章中读到了关于从 VB 到 VB.NET 的内容,它说使用 < .NET 中的 code>object 因为 Variant 不存在。我之前尝试过传递 default(object),但没有成功,但我尝试传递 new object() 作为参数并且成功了——没有抛出异常!我不明白的是,当对象的实例应该是可选参数时,为什么必须传递对象的实例。为什么 null 没有以某种方式转换为 new object()

在 StackOverflow 上输入这个问题让我在思考了两天之后得到了这个答案,因为我必须从文档中复制/粘贴确切的方法描述。就在那时,我注意到参数是 Variant 而不是 Object 。 :)

I read in this article about going from VB to VB.NET and it said to use object in .NET since Variant doesn't exist. I had previously tried passing default(object), which didn't work, but I tried passing new object() as the parameter and that worked--no exception was thrown! What I don't understand is why an instance of an object has to be passed when that's supposed to be an optional parameter. Why does null not get translated somehow to new object()?

Typing up this question on StackOverflow is what led me to this answer after two days of puzzling over it, because I had to copy/paste the exact method description from documentation. That's when I noticed Variant instead of Object for the parameter. :)

a√萤火虫的光℡ 2024-10-04 00:52:28

您可以尝试使用 System.DBNull.Value 作为参数吗?

今天我不得不艰难地找到这一点。
在尝试了答案中的所有建议后,其中一些会导致抛出参数异常,而在其他情况下,对 AddItem 的调用会默默地返回 null

我所做的是在 Visual Studio 解决方案中创建一个新的 VB 项目,其中一个类具有一个 Shared 方法,该方法为我执行了调用,打算将 VB 中的 Null 作为参数传递。编译器抱怨不再使用 Null,并建议改用 System.DBNull.Value。这成功了,正如我后来注意到的,System.DBNull.Value 在 C# 项目中也有效。

Could you try using System.DBNull.Value as an argument?

I had to find this the hard way today.
After trying all the suggestions in the answers, some of them caused an Argument Exception to be thrown, and in other cases, the call to AddItem silently returned null.

What I did was create a new VB project in the Visual Studio Solution, with a single class with a single Shared method that did the call for me, intending to pass Null from VB as an argument. The compiler complained that Null is no longer used, and suggested to use System.DBNull.Value instead. This did the trick, and as I later noticed, System.DBNull.Value worked in the C# project as well.

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