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
yossikallyAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Ah, you are referring to the COM way - I was more thinking about http://support.microsoft.com/kb/106553 ("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 http://msdn2.microsoft.com/en-us/library/aa367088.aspx - "MIDL Language Reference"), so that concept is not available.
0
 
jkrCommented:
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

?DoSomething@CFoo@@CAHHH@Z

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 http://msdn2.microsoft.com/en-us/library/dt232c9t(vs.80).aspx ("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.)
0
 
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
kuku.StaticFunc

skipping the line that is commented out.  Is it possible to declare a C++ ATL object such that the above code will work
0
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.

 
jkrCommented:
That is what I described above. For that you would not need a 'new', yet the limitations apply.
0
 
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?
0
 
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
0
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.