[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

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
0
yossikally
Asked:
yossikally
  • 3
  • 3
1 Solution
 
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
 
jkrCommented:
That is what I described above. For that you would not need a 'new', yet the limitations apply.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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
 
jkrCommented:
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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now