Are there static functions in ATL/COM

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
Who is Participating?
jkrConnect With a Mentor Commented:
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.
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.)
yossikallyAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

That is what I described above. For that you would not need a 'new', yet the limitations apply.
yossikallyAuthor Commented:
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?
yossikallyAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.