Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing a VBA class that implements an ATL interface, to an ATL method
    primarykey
    data
    text
    <p><strong>Edit :</strong></p> <p>I have an ATL simple object MyATLObject, having only one method :</p> <pre><code>STDMETHODIMP CMyATLObject::EVAL( DOUBLE* x, long AddressOfFunc ) { *x = toto&lt;FCTPTR&gt;(*((FCTPTR) AddressOfFunc)) ; return S_OK; } </code></pre> <p>where toto is a template function defined by</p> <pre><code>#pragma once template&lt;typename F&gt; double toto( F f ) { return f(0.0) ; } </code></pre> <p>and where FCTPTR is defined as</p> <p>typedef double (__stdcall * FCTPTR)( double );</p> <p>My ATL method consume double x for storing the result of toto, and long AddressOfFunc which is the AddressOf of a VBA function.</p> <p>This passing VBA function to ATL methods by Address. it is quite awful. How would it be able to do the same - passing VBA functions - to ATL method otherwise, for instance by defining an interface on the ATL side ?</p> <p>Thank you very much.</p> <p>//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////</p> <p><strong>First version of my question :</strong></p> <p>I am actually doing the following : creating an ATL/COM project, adding a "simple ATL object" called MyATLObject to it with :</p> <p>1) in the idl file :</p> <pre><code>interface IMyATLObject : IDispatch{ [id(1), helpstring("method EVAL")] HRESULT EVAL(DOUBLE* x, long AddressOfFunc) ; }; </code></pre> <p>2 in the MyATLObject.h file :</p> <p>an</p> <pre><code>#include "MyStupidEvaluator.h" </code></pre> <p>and a</p> <pre><code>typedef double (__stdcall * FCTPTR)( double ); </code></pre> <p>outside the class, and in the class </p> <pre><code>public: STDMETHOD(EVAL)(DOUBLE* x, long AddressOfFunc) ; </code></pre> <p>and finally in the MyATLObject.cpp file :</p> <pre><code>STDMETHODIMP CMyATLObject::EVAL( DOUBLE* x, long AddressOfFunc ) { *x = toto&lt;FCTPTR&gt;(*((FCTPTR) AddressOfFunc)) ; return S_OK; } </code></pre> <p>MyStupidEvaluator.h contains :</p> <pre><code>#pragma once template&lt;typename F&gt; double toto( F f ) { return f(0.0) ; } </code></pre> <p>This is just a template function that returns the value at zero of any function object, that is a typename (or a class) admitting a () operator.</p> <p>Now, this gives to me, after compilation, a dll.</p> <p>That's all for the c++ part. Now, I'm referencing this dll in VBA and in VBA :</p> <p>a) I define a function</p> <pre><code>Public Function F(ByVal x As Double) As Double F = x * x - 2 * x + 1 End Function </code></pre> <p>b) and a sub</p> <pre><code> Public Sub Draft1() Dim ENGINE As MyTestProjectLib.MyATLObject Set ENGINE = New MyTestProjectLib.MyATLObject Dim x As Double x = 0# Call ENGINE.EVAL(x, AddressOf F) Sheets("Test1").Range("Value_at_0").Offset(0, 0).Value = x End Sub </code></pre> <p>c) Executing this sub will give to me, in the range V"alue_at_0" (just a cell here) of the sheet "Test1" the value at 0.0 of the function F.</p> <p>I'm always working like this : the toto template function is trivial here (evaluation at 0.0) but in general it could be a numerical integration routine, or a root finding routine for which c++ is needed for quickness ; I'm alwas passing VBA functions through their addresses, thanks to the AddressOf VBA keyword : this gives me a long that I pass to my ATL method EVAL, I convert this long in function pointer (FCTPTR) p, and I apply my routine "toto" to *p, which gives a double to me, that I pass back to my ATL method.</p> <p>That's cool, everything works, but... I find this awful !!! And really inelegant. That's why I would like to do the following :</p> <p>design "somehow" an interface on the ATL side, a</p> <pre><code>interface IMyFunction : IDispatch </code></pre> <p>that would be implemented (through VBA's implements) in VBA by a VBA class VBAClass1, and pass an instance of that VBA class to a new version of my former VBA method for performing the evaluation at 0.0.</p> <p>My problem is : I'm not an expert at all in ATL, nor in interface creating : I've succeeded in doing real stupid/trivial things in interface creating, ok, but I really don't see how to design the interface doing what I want to do, that is :</p> <p>an interface in ATL (which will by definition have only methods, by ATL construction !) that would have "something" playing the role of a member of a class, member keeping track of a VBA function still to be passed to him through an ATL method.</p> <p>Any help would be greatly appreciated, folks.</p> <p>It's my first time here, so sorry per advance if the message is not in the perfect/optimal form.</p> <p>Thank you in advance.</p> <p>Raph</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload