Hi,

I've created a C DLL that is being used by a VB EXE. The VB code passes the
addressof a function to the C DLL. The C DLL later on calls the function in
the VB Code; If the function has no parameters it works, however when I
change the function to add a parameter the following debug error is thrown:
"The value of ESP was not properly saved across a function call..."

My code is as follows:
C DLL:
static void (* TestEvent)(bool c);

//setting the callback function
EXPORT BOOL __stdcall SetCallBack(void (* __stdcall TestCallBack)(bool c))
{
TestEvent = TestCallBack;

//testing the callback
TestEvent(true);
}




VB CODE:

'in a form
public sub Form_Load()
SetCallBack(AddressOf TestCB)
end sub

'In a public module
Public Declare Function SetCallBack Lib "Test.dll" (ByVal CallBack As Long)
As Boolean

Public Sub TestCB(ByVal c As Boolean)
Debug.Print "gotcha"
End Sub

Re: Callback Function in C DLL to VB EXE by Rodrigo

Rodrigo
Wed Jul 27 06:08:04 CDT 2005

Correct the declaration for TextEvent and SetCallBack must be:

void (__stdcall * TestEvent)(bool c);
EXPORT BOOL __stdcall SetCallBack(void ( __stdcall* TestCallBack)(bool c))
{
}

You have the * at a wrong place.

Or better (more clear for me)

void (CALLBACK * TestEvent)(bool c);
EXPORT BOOL WINAPI SetCallBack(void ( CALLBACK * TestCallBack)(bool c))
{
}

CALLBACK and WINAPI are defined in windef.h

--
Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org



Re: Callback Function in C DLL to VB EXE by Alex

Alex
Wed Jul 27 06:21:55 CDT 2005

Rodrigo Corral [MVP] wrote:
> Correct the declaration for TextEvent and SetCallBack
> must be:
>
> void (CALLBACK * TestEvent)(bool c);
> EXPORT BOOL WINAPI SetCallBack(void ( CALLBACK *
> TestCallBack)(bool c)) {
> }

How is VB Boolean type converted to bool (_Bool?) type?
Shouldn't it be VARIANT?



Re: Callback Function in C DLL to VB EXE by Joshua

Joshua
Wed Jul 27 07:01:16 CDT 2005

Thanks

Josh

"Rodrigo Corral [MVP]" <corral_glez@hotmail.com> wrote in message
news:eBer3tpkFHA.3568@tk2msftngp13.phx.gbl...
> Correct the declaration for TextEvent and SetCallBack must be:
>
> void (__stdcall * TestEvent)(bool c);
> EXPORT BOOL __stdcall SetCallBack(void ( __stdcall* TestCallBack)(bool c))
> {
> }
>
> You have the * at a wrong place.
>
> Or better (more clear for me)
>
> void (CALLBACK * TestEvent)(bool c);
> EXPORT BOOL WINAPI SetCallBack(void ( CALLBACK * TestCallBack)(bool c))
> {
> }
>
> CALLBACK and WINAPI are defined in windef.h
>
> --
> Un saludo
> Rodrigo Corral González [MVP]
>
> FAQ de microsoft.public.es.vc++
> http://rcorral.mvps.org
>
>