COM/ATL App Method Return/Pass defined types

Alright, I am not by any means an expert C++ programmer, so please bare with me.

I have created a com object that will be implemented within C#, the reason for this com object is to create a persistent object of another library. My situation is this. I have the complete com object created, but would like to add additional supporting features, i.e.:

An object that is defined within the C++ com object, such as a struct containing an int, a wchar_t*, and a boolean value. I would like to be able to utilize the object type from within C# to store the object rather than have a new object type in C#. I have seen this done in many cases, but just have no clue where to find such direction for supporting this.

I would also like the come object (Item)'s methods of:

typedef struct MyItemType
{
int myItn;
wchar_t* myChar;
BOOL myBool;
};

[propget, id(1)] HRESULT Item([out, retval] MyItemType* pVal)
STDMETHOD(get_Item)(MyItemType* pVal);

to be able to be implemented. As it stands right now, I have created a struct with in the header file of my C++ com object, and am using this. But i get the following error.

Error 1 error MIDL2025 : syntax error : expecting a type specification near "MyItemType"



Answer this question

COM/ATL App Method Return/Pass defined types

  • Ravi Santha

    Thank you for your reply, I apologize I guess I wasn't quite clear enough.  I am trying to create the com object in C++ and call/use it from with in C#.

    You state to place the struct inside the IDL, where woudl this be specifically, inside the class the interface the...

    Also, I am aware that COM is dead, however, what would another method be   I am open to suggestions.


  • Tryst

    I don't remember this staff well(remember COM is dead:)) and defiantly have no idea if c# can do it, but I would do the following:
    1. define struct inside of IDL
    2. Define method of the object ([propget, id(1)] HRESULT Item([out, retval] MyItemType** pVal))
    3. export the tlb into C# and implement the interface (very iffy with struct and such)
    4. rest is easy if you #import from you cpp it will generate ready to use header files.

    It might be easier if you had to use c++ com object from c#


  • Vladimir Chtepa

    to unswer 1 question yes in the beginig of IDL (

    "struct [MIDL]" -

    http://msdn2.microsoft.com/en-us/library/aa367271.aspx

     article in MSDN describes it nicely)

     

    the second way is mixed mode code where __gc class XXX wraps native unmanaged counterpart as a member, creating wrapper around unmanged code something like

    public __gc class WrapperClassForDotNet : public IDisposable

    {

    public:

          WrapperClassForDotNet(){m_old = new MyOldCPPClassIWantToReuse();}

          void Dispose()

          {

                Dispose(true);

                GC::SuppressFinalize(this);

          }

     

     

          __property int get_SampleValAsProperty()

          {

                if(!m_old)

                      return 0;

                return static_cast<int>(m_old->SampleVal());

          }

     

    protected:

          virtual void Dispose(bool disposing)

          {

                if(m_old)

                {

                      delete m_old;

                      m_old = 0;

                }

     

          }

    private:

          MyOldCPPClassIWantToReuse* m_old;

     

    };

    We tried the second way in our App. It is incredibly difficult to debug and troubleshoot, but it seems to work

     


  • COM/ATL App Method Return/Pass defined types