views:

34

answers:

1

I have a .Net asm with several interfaces and classes exposed to COM using the [ComVisible(true)] attribute. I generate a tlb, then reference this in my StdAdx file within a C++ COM component. What's odd is that for some reason, even though the very basic intellisense (VS6 for C++) is able to see my properties and methods, I get compiler errors stating that they are not a member. For example:

[Guid("88E58BE4-E0CB-4d1b-9553-A5431E7A0BEA")]
[ComVisible(true)]
public interface ISupplierPayment : IBusinessObjectPersist
{
   String Comment
    {
        get;
        set;
    }

And in the generated tlh in c++:

struct __declspec(uuid("e94bd31e-327c-33c8-8a55-b693ccf1ed96"))
struct __declspec(uuid("e94bd31e-327c-33c8-8a55-b693ccf1ed96"))

ISupplierPayment : IDispatch
{
    //
    // Raw methods provided by interface
    //

    virtual HRESULT __stdcall get_Comment (
        BSTR * pRetVal ) = 0;

And finally the error when trying to use this in code:

D:\MR...File.cpp(647) : error C2039: 'Comment' : is not a member of 'ISupplierPayment' d:\mr...projectdir\release\TheDotNetClasses.tlh(758) : see declaration of 'ISupplierPayment'

Any ideas what I should look at next? if it's in the tlh and intellisense recognises it and it's there in OLEView on the tlb, I'm not sure what could possibly be wrong.. thanks in advance for taking a look

UPDATE Further example of related issue: C#

    [Guid("3BE93D52-86B7-42e6-BAE6-29037F6BC9C4")]
    [ComVisible(true)]
public interface IDataStoreFactory
{
        void TestMethod(String test);

C++ TLH

struct __declspec(uuid("3be93d52-86b7-42e6-bae6-29037f6bc9c4"))
IDataStoreFactory : IDispatch
{
    //
    // Raw methods provided by interface
    //

    virtual HRESULT __stdcall TestMethod (
        BSTR dataStoreAssembly ) = 0;
            void TestMethod(String test);

C++ method call

spDataStoreFactory->TestMethod("test");

C++ compile error

'TestMethod' : cannot convert parameter 1 from 'char [5]' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Huh!? it's not a short it's a BSTR... very confused now

A: 

You didn't show the calls generating the error message. I suppose you used the property name directly.

You need to use the method get_Comment instead of simply Comment property. The generated tlh refers to that method. Did you used raw_interfaces_only attribute of the #import directive?

Later edit about BSTR:

BSTR is typedef for wchar_t*. So use spDataStoreFactory->TestMethod( SysAllocString(L"test"));

Cătălin Pitiș
Hi - thanks, an interesting point. Actually I should give more info here. This used to work. I have consolidated about 25 asm into 1 because of several circular refs which meant making changes was a paint and could only be done using nant and a cache of previous dlls. This seems to have worked in the main except for this one area. I've done a further test since then which perplexes me even more (have appended above):
MRAH
Sorry - didn't answer you query on the raw interfaces attrib:#import "M:\\Type Libraries\\MYASM.tlb" no_namespace raw_interfaces_only
MRAH
raw_interfaces_only generates get_xx and set_xx methods for the properties. Remove that attribute.
Cătălin Pitiș
Ah thanks, I'll try that. Suddenly realised what that issue with convert char to short was (my C++ experience spans many years and even fewer hours...). It's because it's set a unicode..
MRAH
I removed the raw_interfaces_only from the #import and still have the same errors - however if I now use get_xx instead of just the property name it does get past these errors. This used to work (with the old dlls) without requiring the property prefix. What causes this prefix to be required? (the raw_interfaces_only attrib?)
MRAH
Hey thanks!! my problem is solved! I had two imports in the stdafx.h for that tlb one with and one without the raw_interfaces_only attribute. As you said the removal of this attribute means that I don't have to user get_xx. Not only this but methods no longer need to take the return value as argument etc and also collection indexers work with square brackets. This has solved a lot of problems for me, thanks very much again.
MRAH
I am glad I could help you. It's a long time since I played with COMs :)
Cătălin Pitiș