CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2000
    Location
    Indore (MP) INDIA.
    Posts
    356

    Thumbs down Adding a method to the IDL without using the Wizard

    Hi,

    I am trying to add a method in a COM Interface. I know trhere is an "Add Method" wizard but it is not working due to some unknown problem (probably because the project was a VC 6.0 project which is now ported to VC 7.1).

    My requirement was that, I want to add the method in the COM object and access it through VB. I can use VS 7.1 or VS 8.0.

    I tried the following steps:
    1. Add the method in the IDL file with a new id: // (Already exposed 11 methods in the IDL)
    Code:
    [id(12), helpstring("method Sub")] HRESULT Sub([in] BSTR sSource, [in] BSTR sDestination, [in] VARIANT* objOptions, [in] VARIANT* objHandler, [out,retval] BSTR* pVal);
    2. Add a declaration in the header file of the COM class:
    Code:
    public:
       STDMETHOD(Sub)(BSTR sSource, BSTR sDestination, VARIANT* objOptions, VARIANT* objHandler, BSTR* pVal);
    3. Add the method implementation to the CPP file:
    Code:
       STDMETHODIMP CMyComClass::Sub(BSTR sSource, BSTR sDestination, VARIANT* objOptions, VARIANT* objHandler, BSTR* pVal)
       {
          MessageBoxW(0, L"Inside CMyComClass::Sub()", L"On Call", 0) ;
          return S_OK;
       }
    I Built the project, registered the COM DLL and observed that the new method I have added are not Visible (or rather to say not exposed). Neither a VB based client not a .NET application can see the newly added "Sub" method. Using the "OLE View" application I opened the newly generated TypeLib, but surprised that the TypeLib contains the newly added method but the registered DLL doesn't expose them.

    Confusion !! :-(

    Can some one through Light on this and suggest how to add a method that can be visible to VB/.NET clients ?

    Thanks.

    *(Vipul)( ) ;
    (*Vipul)() ;

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Wink Re: Adding a method to the IDL without using the Wizard

    Quote Originally Posted by VipulPathak
    Can some one through Light on this and suggest how to add a method that can be visible to VB/.NET clients ?
    For a COM-method (actually methods of an interface) to be made available to automation clients like VB, the interface the contains the method should inherit from IDispatch, and the COM-Class should feature dual-interfaces.

    Example:
    Code:
    interface ISampleInterface : IDispatch{
    	[id(1), helpstring("method Initialize")] HRESULT Initialize([in] BSTR bstrName);
    };
    Similarly, the COM Class needs to implement IDispatch. All this is easy when the COM Class is created using an ATL Wizard where the wizard would typically make the COM Class inherit from the IDispatch implementation class.

    Example:
    Code:
    class ATL_NO_VTABLE CSampleComClass :
    	public CComObjectRootEx<CComSingleThreadModel>,
    	public CComCoClass<CSampleComClass, &CLSID_SampleComClass>,
    	public IDispatchImpl<ISampleInterface, &IID_ISampleInterface, &LIBID_SampleLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
    {
    
    DECLARE_REGISTRY_RESOURCEID(IDR_SAMPLEINTERFACE)
    
    
    BEGIN_COM_MAP(CProductNode)
    	COM_INTERFACE_ENTRY(ISampleInterface)
    	COM_INTERFACE_ENTRY(IDispatch)
    END_COM_MAP()
    
    public:
    	STDMETHOD(Initialize)(BSTR bstrName);
    };
    Last but not the least, here are samples on invoking a C++ COM Server's methods from a VB Client for you to decipher the steps from.
    Last edited by Siddhartha; February 27th, 2007 at 06:28 AM.

  3. #3
    Join Date
    May 2000
    Location
    Indore (MP) INDIA.
    Posts
    356

    Exclamation Re: Adding a method to the IDL without using the Wizard

    Thanks Siddaharth,

    My COM DLL is already in use by VB and .NET clients. The problem I am facing is actually that the new methods that I have add to the COM DLL are not exposed to VB/.NET clients (probably not exposed to any body).

    My IDL interface defination confirms to what you have suggested:
    Code:
      interface IMyComClass : IDispatch
      {
      }
    Even after updating .IDL, .CPP and .H file for new methods, my new methods are still not exposed.

    Regards,

    *(Vipul)( ) ;
    (*Vipul)() ;

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Adding a method to the IDL without using the Wizard

    Another change I would recommend is that when you need to send a VARIANT as an [in]-parameter, it should not be a VARIANT* rather only a VARIANT.

    Use a VARIANT* only when a VARIANT object needs to be an [out]-param.

    Last but not the least, how are you sure that the newly added member is not available in VB? Are you sure it is not Visual Studio that is failing in displaying this to you? Try re-registering the COM Server...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured