Calling int f(int (*f)(int)) like function in DLL from VB 
Author Message
 Calling int f(int (*f)(int)) like function in DLL from VB

Hi,

I use VC++'s Win32 DLL Wizard to create a DLL with these things:

// IN .CPP
int (*f1)(int);
LIBRARY_API int Start(int (*f)(int))
{
    f1 = f;

Quote:
}

// IN .H
#ifdef LIBRARY_EXPORTS
#define LIBRARY_API __declspec(dllexport)
#else
#define LIBRARY_API __declspec(dllimport)
#endif
LIBRARY_API int Start(int (*f)(int));

Then In VB, I write

Declare Function MyStart Lib "e:\my waste\library\debug\library.dll" Alias
"Start" _
    (ByVal p As Long) As Long

Function MyProc(ByVal x As Long) As Long
...
End Function

and I call
x = MyStart (AddressOf MyProc)

Then VB says it cannot find the entry point. I try adding a DEF file to the
VC project, this time, VB says the calling convention is bad. What should I
do? Furturemore, since Win32 DLL wizard didn't create a DEF file for me, is
it really unnecessary?

--
Wang Weijun
wangwj at taslon dot com | wangwj at iname dot com



Sat, 24 Nov 2001 03:00:00 GMT  
 Calling int f(int (*f)(int)) like function in DLL from VB

Quote:

>Hi,

>I use VC++'s Win32 DLL Wizard to create a DLL with these things:

>// IN .CPP
>int (*f1)(int);

VB uses __stdcall calling convention only.

This is pointing to a default calling convention function (__cdecl).
The pointer you are assigning to it is a VB function, therefore
__stdcall.

Change the declaration to:
   LIBRARY_API int (*f1)(int);

Then change
    #define LIBRARY_API __declspec(dllimport)

to
    #define LIBRARY_API __declspec(dllimport) __stdcall

(I think that's right; I still use DEF files to avoid name
mangling)

<snip>

Quote:
>Then VB says it cannot find the entry point.

Right. The names are mangled. You have to either use
a DEF file or reference the decorated name in the Alias
clause of your function declaration in VB. Either way
works fine.

Quote:
>I try adding a DEF file to the
>VC project, this time, VB says the calling convention is bad.

Because it is. If you use the DEF file to avoid the name,
change LIBRARY_API to just __stdcall.  You really only
want to use either __declespec( dllimport | dllexport )
or the DEF file, not both.

But for interaction with VB, __stdcall is mandatory.

Quote:
>What should I
>do? Furturemore, since Win32 DLL wizard didn't create a DEF file for me, is
>it really unnecessary?

It's unnecessary unless you need to change the default behavior
of the compiler (like name decoration).
Quote:
>--
>Wang Weijun
>wangwj at taslon dot com | wangwj at iname dot com



Sat, 24 Nov 2001 03:00:00 GMT  
 Calling int f(int (*f)(int)) like function in DLL from VB
Thank you, Ron

It works fine now (in VB and VC). I simply assign all function I use to
__stdcall in the DLL and keep the DEF file.

--
Wang Weijun
wangwj at taslon dot com | wangwj at iname dot com



Sun, 25 Nov 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. int func(int) versus int func(int *) efficacy.

2. (int/int) != int

3. How to do int func(int size,int matrix[][size])

4. extern int foo(int); vs int foo(int);

5. My short int likes 0xffff9001

6. newbie: int->byte[]->int?

7. int fchdir(int fildes);

8. *unsigned int = unsigned int bit: 1;

9. int **a vs. int *a[]

10. Diff between int a[] and int*a?

11. unsigned int and int

12. int promotion to unsigned int

 

 
Powered by phpBB® Forum Software