Search code examples
visual-studio-2010mfcactivexocxvisual-studio-6

How to add method to MFC-ActiveX


The question seems to be stupid since there are many explanations in internet, that describe how to add a new method that can be called by users of the resulting OCX later. Unfortunately it does not work for me.

I have a MFC-based ActiveX-control project that was created with Visual Studio 6 and was imported to VS2010. There I have NO class view where I could use the Wizard with to add a method (the class view tab pane is there but it is empty). The existing code also does not provide any callable methods until now so that I simply could copy them.

So: how can I enable/invoke the class view generation in VS2010 to use the Wizard?

And as soon as it works: What type should such a method be to be externally visible? From what I learned the Wizard asks for some type...


Solution

  • To add a method to your ActiveX control you have to follow the folliwng steps:

    1. Declare the function in the header file.

    e.g.

    public:
        int Connect(int timeout);
    

    2. Add the definition in the CPP file.

        int CSLWebLinkCtrl::Connect(int timeout)
        
        // Your logic here.
        
        return 0;
    }
    

    3. Expose your methods in the .idl file

    [id(4), helpstring("method Connect")] int Connect(int timeout);
    

    Hope it will help you. :)