Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. COM
  4. ATL Problems ??? Ask for help !

ATL Problems ??? Ask for help !

Scheduled Pinned Locked Moved COM
helptutorialquestionc++com
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leesen
    wrote on last edited by
    #1

    Hi, all In Atl project ,how can I return the user define type? For example , I have define a Struct ,and I want to return the Struct type in a function, and I plan to utilize the return Struct type in VB through the com. What should I do ? I have try some ways ,but failed. Another similar problem. I have two ATL Object in my Atl project , Teacher and Student . And student is a private property of Teacher ,now I want to construct a function in Teacher to return the Student property .How should I do ? Sincerely ask for help. Who can tell me how to do , or give me some similar source code which resovle the above problems . I'll appreciate it . Regards.

    V 1 Reply Last reply
    0
    • L Leesen

      Hi, all In Atl project ,how can I return the user define type? For example , I have define a Struct ,and I want to return the Struct type in a function, and I plan to utilize the return Struct type in VB through the com. What should I do ? I have try some ways ,but failed. Another similar problem. I have two ATL Object in my Atl project , Teacher and Student . And student is a private property of Teacher ,now I want to construct a function in Teacher to return the Student property .How should I do ? Sincerely ask for help. Who can tell me how to do , or give me some similar source code which resovle the above problems . I'll appreciate it . Regards.

      V Offline
      V Offline
      Vi2
      wrote on last edited by
      #2

      Q2. You can pass the IStudent interface as well as any other data from ITeacher interface. Q1. In IDL-file

      typedef [uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX), helpstring("Help")]
      struct _SomeStruct
      {
      int i;
      BSTR str;
      ...
      } SomeStruct;

      [...,dual,...]
      interface IO : IDispatch
      {
      [id(1)] HRESULT M1([in] SomeStruct ss);
      [id(2)] HRESULT M2([in,out] SomeStruct *ss);
      [id(3)] HRESULT M3([out,retval] SomeStruct* *ss);
      [id(4)] HRESULT M4([in,out] SAFEARRAY(SomeStruct) * ssbs);
      };

      With best wishes, Vita

      L 1 Reply Last reply
      0
      • V Vi2

        Q2. You can pass the IStudent interface as well as any other data from ITeacher interface. Q1. In IDL-file

        typedef [uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX), helpstring("Help")]
        struct _SomeStruct
        {
        int i;
        BSTR str;
        ...
        } SomeStruct;

        [...,dual,...]
        interface IO : IDispatch
        {
        [id(1)] HRESULT M1([in] SomeStruct ss);
        [id(2)] HRESULT M2([in,out] SomeStruct *ss);
        [id(3)] HRESULT M3([out,retval] SomeStruct* *ss);
        [id(4)] HRESULT M4([in,out] SAFEARRAY(SomeStruct) * ssbs);
        };

        With best wishes, Vita

        L Offline
        L Offline
        Leesen
        wrote on last edited by
        #3

        thank you . Q1 has been resovled following your suggestion. but Q2 remail abcuse. I mean I can not construct the CStudent object in CTeacher. Details as follows. In the construction function of the CTeacher : CTeacher() { IStudent * m_student =new CStudent(); return; } There are some compiler errors. It says that the CStudent cannot instantiate abstract class due to following members:QueryInterface,AddRef,Release. How should I do to resolve the problem. thx. Regards.

        V 1 Reply Last reply
        0
        • L Leesen

          thank you . Q1 has been resovled following your suggestion. but Q2 remail abcuse. I mean I can not construct the CStudent object in CTeacher. Details as follows. In the construction function of the CTeacher : CTeacher() { IStudent * m_student =new CStudent(); return; } There are some compiler errors. It says that the CStudent cannot instantiate abstract class due to following members:QueryInterface,AddRef,Release. How should I do to resolve the problem. thx. Regards.

          V Offline
          V Offline
          Vi2
          wrote on last edited by
          #4

          If you are in ATL, you have several ways to do it.

          // CTeacher has a member: IStudent * m_student
          // or it is better: CComPtr<IStudent> m_student

          1. With the class factory of CStudent.
            if( FAILED(CStudent::_CreatorClass::CreateInstance(NULL,IID_IStudent, (void**)&m_student)) )
            return E_FAIL;

          2. With the static method of the generic ATL CComObject.
            CComObject<CStudent>* pCStudent;
            if( FAILED(pCStudent->CreateInstance(&pCStudent)) || FAILED(pCStudent->QueryInterface(&m_student)) )
            return E_FAIL;

          3. With the original COM method.
            if( FAILED(CoCreateInstance(CLSID_Student,NULL,CLSCTX_ALL,IID_IStudent, (void**)&m_student)) )
            return E_FAIL;

          With best wishes, Vita

          L 1 Reply Last reply
          0
          • V Vi2

            If you are in ATL, you have several ways to do it.

            // CTeacher has a member: IStudent * m_student
            // or it is better: CComPtr<IStudent> m_student

            1. With the class factory of CStudent.
              if( FAILED(CStudent::_CreatorClass::CreateInstance(NULL,IID_IStudent, (void**)&m_student)) )
              return E_FAIL;

            2. With the static method of the generic ATL CComObject.
              CComObject<CStudent>* pCStudent;
              if( FAILED(pCStudent->CreateInstance(&pCStudent)) || FAILED(pCStudent->QueryInterface(&m_student)) )
              return E_FAIL;

            3. With the original COM method.
              if( FAILED(CoCreateInstance(CLSID_Student,NULL,CLSCTX_ALL,IID_IStudent, (void**)&m_student)) )
              return E_FAIL;

            With best wishes, Vita

            L Offline
            L Offline
            Leesen
            wrote on last edited by
            #5

            Hi, all Some problems about ATL Com: 1.I have create two ATL Object: CStudent and CTeacher . The simple source code listed as follow: (some code auto generated by ATL have been skiped) class CStudent { private: long m_age; public: CStudent():m_age(10) { } //...other code auto generated by ATL }; class CTeacher { private: IStudent* m_aStudent; public: CTeacher() { //create an instance of the com CoCreateInstance(CLSID_Student,NULL,CLSCTX_ALL,IID_IStudent, (void**)&m_aStudent); } //...other code auto generated by ATL } 2.Then I add a "IStudent * OneStudent" property for ITeacher ,to access the m_aStudent. The implemention of the property are listed as below: STDMETHODIMP CTeacher::get_OneStudent(IStudent *pVal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here *pVal = * m_aStudent; return S_OK; } STDMETHODIMP CTeacher::put_OneStudent(IStudent *newVal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here *m_aStudent = *newVal ; return S_OK; } 3.Now I type some VB code to test the com ,but some compiler errors occurrs. VB Code: Dim s As Student Dim t As New Teacher s = t.OneStudent the last sentence "s = t.OneStudent" have compiler errors. Errors:function or interface marked as restricted ,or the function use an Automation type not supported in Visual Basic I guess the errors caused by the type dismatch between IStudnet and CStudent.But I did not know how to resolve it . So who can tell me how to return the m_aStudent that can be supproted in vb?? thx. Regards.

            P 1 Reply Last reply
            0
            • L Leesen

              Hi, all Some problems about ATL Com: 1.I have create two ATL Object: CStudent and CTeacher . The simple source code listed as follow: (some code auto generated by ATL have been skiped) class CStudent { private: long m_age; public: CStudent():m_age(10) { } //...other code auto generated by ATL }; class CTeacher { private: IStudent* m_aStudent; public: CTeacher() { //create an instance of the com CoCreateInstance(CLSID_Student,NULL,CLSCTX_ALL,IID_IStudent, (void**)&m_aStudent); } //...other code auto generated by ATL } 2.Then I add a "IStudent * OneStudent" property for ITeacher ,to access the m_aStudent. The implemention of the property are listed as below: STDMETHODIMP CTeacher::get_OneStudent(IStudent *pVal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here *pVal = * m_aStudent; return S_OK; } STDMETHODIMP CTeacher::put_OneStudent(IStudent *newVal) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here *m_aStudent = *newVal ; return S_OK; } 3.Now I type some VB code to test the com ,but some compiler errors occurrs. VB Code: Dim s As Student Dim t As New Teacher s = t.OneStudent the last sentence "s = t.OneStudent" have compiler errors. Errors:function or interface marked as restricted ,or the function use an Automation type not supported in Visual Basic I guess the errors caused by the type dismatch between IStudnet and CStudent.But I did not know how to resolve it . So who can tell me how to return the m_aStudent that can be supproted in vb?? thx. Regards.

              P Offline
              P Offline
              Philippe Mori
              wrote on last edited by
              #6

              I think that you should implement get_OneStudent that way (and the IDL updated appropriatly):

              STDMETHODIMP CTeacher::get_OneStudent(IStudent **pVal)
              {
              AFX_MANAGE_STATE(AfxGetStaticModuleState())

              // TODO: Add your implementation code here
              *pVal = m_aStudent;
              if (*pVal) {
              (*pVal)->AddRef();
              }
              return S_OK;
              }

              You cannot copy the interface (nothing will be copied since the interface has no data in itself) and output parameter must be a [out]pointer to what you want to returns (you want to returns a IStudent * so you must use IStudent **). Philippe Mori

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups