Are there static functions in ATL/COM

Posted on 2007-07-28
Last Modified: 2013-12-03
Is it possible to have static methods declared and defined within an ATL object, so that VB client  won't have to create the object but call the method directly as I do within C++ MyClass::MyStaticMethod() without having to call 'new' on MyClass.

If there is, I would appreciate a link to an example
Question by:yossikally
    LVL 86

    Expert Comment

    You can always add static members to any classes, including ATL, but the use is quite limited. Calling them fom VB is like calling any other regular function, all you need to do is keeping the C++ name mangling in mind - e.g.

    class __declspec(dllexport) CFoo {

    static int DoSomething(int,int);


    int CFoo::DoSomething(int a,int b) {

      return a + b;

    is exported as


    which you would have to use when calling it from VB. Chances are that '?' will be illegal in a 'Dim' statement. What you can do now is using a .def file to map the names, see ("Calling DLL Functions from Visual Basic Applications")

    To sum that up: You can do that, yet the use is limited to the same scope as with "regular" functions (no 'this' pointer etc.)

    Author Comment

    I meant a static function defined within an ATL/COM object, so that in VB I would do something like

    dim kuku as Obj1
    'set kuku = new Obj1

    skipping the line that is commented out.  Is it possible to declare a C++ ATL object such that the above code will work
    LVL 86

    Expert Comment

    That is what I described above. For that you would not need a 'new', yet the limitations apply.

    Author Comment

    Bu in V you can only call a method that has been described in the IDL, not only at the class level.  Now, how do you decalre a static function at the IDL?

    Author Comment

    But in VB you can only call a method that has been described in the IDL, not only at the class level.  Now, how do you decalre a static function at the IDL
    LVL 86

    Accepted Solution

    Ah, you are referring to the COM way - I was more thinking about ("How To Write C DLLs and Call Them from Visual Basic"), i.e.

       ' Enter the following Declare as one, single line:
       Declare Sub getdiskinfo Lib "c:\dllartic\diskinfo.dll" (ByVal mydrive As String, ByVal myvolume As String, free As Long)

    IDL does not know the 'static' attribute (see - "MIDL Language Reference"), so that concept is not available.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now