Search code examples
c#.netcomdcomvariant

COM method call fails in C#, VB.NET, but works in Python


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.


Solution

  • 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.