Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

Passing variants from VBA to C# COM object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ruslan Popov - 01 Nov 2005 13:05 GMT
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.

Signature

Best regards,
Ruslan Popov

Ruslan Popov - 01 Nov 2005 13:49 GMT
One extra note: I use ClassInterfaceType AutoDual. With late binding, at
least the indexer property works all right.

Signature

Best regards,
Ruslan Popov

Wei-Dong XU [MSFT] - 02 Nov 2005 07:28 GMT
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.
Ruslan Popov - 02 Nov 2005 08:49 GMT
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.

Signature

Best regards,
Ruslan Popov

Ken Halter - 02 Nov 2005 17:52 GMT
> Hi,
>
> Currently I am finding one support professional for you on this issue. If
> any update, we will reply here at the first time.

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

Signature

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

Ruslan Popov - 02 Nov 2005 20:30 GMT
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.

Signature

Best regards,
Ruslan Popov

Ruslan Popov - 02 Nov 2005 23:07 GMT
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.

Signature

Best regards,
Ruslan Popov

"Peter Huang" [MSFT] - 03 Nov 2005 06:36 GMT
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

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.