Specifying __stdcall for function pointer as parameter

Discussion in 'C++' started by Ed, May 1, 2004.

  1. Ed

    Ed Guest

    Suppose I want a function a pointer to which is going to
    be passed to another function to use __stdcall conventions.
    That is, I want the call to cbFunc() in tryCB3() to be compiled
    for __stdcall calling conventions. What is the syntax?
    I tried placing __stdcall in front of the (*cbFunc) but it is rejected
    by the compiler.

    long __stdcall tryCB3(long n, long (*cbFunc)(long m))
    {
    long r;
    r = cbFunc(n); // call back
    return r;
    }

    As background, the tryCB3() is going to be placed in a DLL
    and called from Visual Basic, and the callback function is
    going to be written in VB. The documentation indicates that
    the call back function must use stdcall, but does not
    tell be how to make the VC++ compiler make it that way.
    The following is the VB code:


    Private Declare Function tryCB3 Lib "SparkExUtilsDll.dll" _
    (ByVal n As Long, ByVal theFunct As Any) As Long

    Public Function theCBFunc3(ByVal m As Long) As Long
    Debug.Print "In theCBFunc3"
    theCBFunc3 = m * m
    End Function

    Public Function tryCBDrv3()
    Dim n As Long
    Dim q As Long
    n = 2
    q = tryCB3(2, AddressOf theCBFunc3)
    Debug.Print "n= " & CStr(n)
    Debug.Print "q= " & CStr(q)

    End Function

    When I step through tryCB3Drv it calls tryCB3, and then steps through
    theCBFunc3, all working as expected, but then crashes with a C++
    error dialog saying the ESP was not preserved through a function call etc.

    Note that the it works without crashing if the callback function
    has no arguments, so it must be some problem with the way VBA is setting
    up the argument to theCBFunc3, vs. the way it's declared in C++.

    Any ideas?

    TIA

    Ed
     
    Ed, May 1, 2004
    #1
    1. Advertisements

  2. __stdcall is not part of standard C++. You should ask questions like this on
    a more appropriate newsgroup.

    However the convention is actually the same as other declarations in C++.

    __stdcall long (*cbFunc)(long m))

    would mean that you are returning a __stdcall long, which is obviously
    nonsense.

    long (__stdcall *cbFunc)(long m))

    means that you are pointing at a __stdcall function, which is what you want.
    Remember C++ declarations are 'inside out'

    If I got this wrong then apologies, but you really should ask Windows
    programming questions on a Windows programming group because that's where
    the Window programming experts hang out. Obvious really, try
    next time.

    john
     
    John Harrison, May 2, 2004
    #2
    1. Advertisements

  3. Ed

    Old Wolf Guest

    This is system-specific, check your compiler's documentation
    on __stdcall, it should have some examples, but
    long __stdcall (*cbFunc)(long m)
    should be correct. Make sure that the function prototype
    matches the declaration too. Probably best would be something like:

    typedef long __stdcall (*CallBackFunc)(long);
    then
    long __stdcall tryCB3(long n, CallBackFunc m)

    If you still have trouble, try putting the _stdcall inside
    the brackets (either before or after the *)
     
    Old Wolf, May 3, 2004
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.