Groups | Blog | Home
all groups > dotnet interop > november 2005 >

dotnet interop : Passing variants from VBA to C# COM object


Ruslan Popov
11/1/2005 4:05:01 AM
Hello,

Is there a way to pass variant parameters from VBA to a C# COM object?
Specifically, I'd like a class to have an indexer property to store different
types from VBA - numbers, strings and dates. In C# I should be able to detect
what actual type is passed. So, the type should be variant. Here's how I
declare the property:

public object this[string name]
{
get
{
if (_propertiesTable.ContainsKey(name))
return _propertiesTable[name];
else
return null;
}
set
{
if (_propertiesTable.ContainsKey(name))
_propertiesTable.Remove(name);
_propertiesTable.Add(name, value);
}
}

That is, is is declared as type "object". However, VBA expects the parameter
of the property to be an object. E.g. the following produces a run-time error:

myServer.Item("test") = 5

In the same way, the following function expects the second parameter in VBA
to be a reference to an object, not variant:

public void SetProperty(string name, object value)

I tried to use the MarshalAs attribute, but had no success.

--
Best regards,
Ruslan Popov
11/1/2005 4:49:04 AM
One extra note: I use ClassInterfaceType AutoDual. With late binding, at
least the indexer property works all right.

--
Best regards,
Ruslan Popov
11/1/2005 11:49:03 PM
Thanks. Here's some extra info. In the type library, the methods of interest
look like the following:

[id(00000000), propget]
HRESULT Item(
[in] BSTR name,
[out, retval] VARIANT* pRetVal);

[id(00000000), propputref]
HRESULT Item(
[in] BSTR name,
[in] VARIANT pRetVal);

[id(0x6002000d)]
HRESULT SetProperty(
[in] BSTR name,
[in] VARIANT value);

It looks like VBA does not treat parameters defined as "[in] VARIANT" like
variants. That's surprising. It may be possible to define the parameter as
VBA variant by mean of MarshalAs(short) attribute. I'm going to figure that
out.

--
Best regards,
v-wdxu NO[at]SPAM online.microsoft.com
11/2/2005 6:28:12 AM

Hi,

Currently I am finding one support professional for you on this issue. If
any update, we will reply here at the first time.

Best Regards,
Wei-Dong XU
Microsoft Product Support Services
This posting is provided "AS IS" with no warranties, and confers no rights.
It is my pleasure to be of assistance.
Ken Halter
11/2/2005 8:52:02 AM
[quoted text, click to view]

<rant mode on>

I must say.... answers like this are worse than no answer at all. I've seen
dozens of these "I am finding someone for you" posts from MS employees and
not once have I seen any follow ups. How about this for a policy. Don't
waste people's time unless you already have the answer the "support
professional" gave you. At least, that way, people won't assume the question
is already in the process of actually getting a decent answer.

</rant mode off>

--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..

Ruslan Popov
11/2/2005 11:30:03 AM
Well, what I noticed, is the following. There're so cool guys in Microsoft
who know all the internals, who actually develop all the low-level stuff
(e.g. marshalling between COM and .NET). I saw some such guys in blogs. But
they apparently don't visit these newsgroups. Because when some more or less
serious question is asked here, it is often difficult to get a relevant
answer.

So, it looks to me sometimes that it's easier to find an appropriate person
to ask a question by oneself than the same for the guys who manage these
newsgroups.

I'm not a frequent visitor of the NG, though, so I judge by what I find in
the newsgroup search engines.

--
Best regards,
Ruslan Popov
11/2/2005 2:07:02 PM
Hi Wei-Dong,

I've figured out the problem. Thanks Thomas Quinn
(http://blogs.msdn.com/tq/default.aspx).

It's the fault of tlbexp that marks my property with "propputref". The IDL
should be changed to propput, then recompiled by midl. Then it should work
ok. I'm impressed by getting help from Thomas so fast, he investigated that
in just hours. Thanks for your help, too.

--
Best regards,
v-phuang NO[at]SPAM online.microsoft.com (
11/3/2005 5:36:55 AM
Hi

Thanks for your quickly reply and knowledge sharing!
Also thanks for Ken's feedback, I think that is just a process to notify
the community that we have been aware of the post.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button