Howto: declare a C function that registers a callback in IDL 
Author Message
 Howto: declare a C function that registers a callback in IDL

Hi,

I would like to wrap a C DLL in a type library for use in VB. Some of
the functions register callbacks. I have not been able to wrap the
callback registering functions in the type library. The following code
gives the error "Can't find dll entry point fnCTestCB in CTest.dll", in
VB when I try to call fnCTestCB. In the IDL file I tried void*, long,
and the fnptr typedef, nothing seems to work.

Thanks

CTest.h -------

#include "windows.h"

typedef void (__stdcall* fnptr)(int i);

int WINAPI fnCTestCB(fnptr cb);

TypeLib.idl ----------

#define CDTAPI __stdcall

// C DLL Test Interface
[
uuid(29FB2B91-F89B-4776-A4B9-67B67C95ABFB),
helpstring("C DLL Test Interface"),
lcid (0x00000000),
version(1.0)
]

library CDLLTest
{
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");

    [
    uuid(41072DE3-C829-4813-A4A5-6BDC84399133),
    helpstring("C DLL Test Functions"),
    dllname("CTest.dll")
    ]
    module CDTFunctions
    {
      [
      entry("fnCTestCB"),
      helpstring("Function that registers a callback.")
      ]
      int CDTAPI fnCTestCB(void* cb);
    }

Quote:
};

VB: mdoule1.bas -------

Public Function vbCTestCB(ByVal n As Long)
      Debug.Print "In VB callback n=" & n
End Function

VB: Form1.frm ---------

Private Sub Command1_Click()
      Call fnCTestCB(AddressOf vbCTestCB)
End Sub



Sat, 07 May 2005 10:42:52 GMT  
 Howto: declare a C function that registers a callback in IDL

Add extern "c" to your function declaration

kp

Quote:
>-----Original Message-----
>Hi,

>I would like to wrap a C DLL in a type library for use in
VB. Some of
>the functions register callbacks. I have not been able to
wrap the
>callback registering functions in the type library. The
following code
>gives the error "Can't find dll entry point fnCTestCB in
CTest.dll", in
>VB when I try to call fnCTestCB. In the IDL file I tried
void*, long,
>and the fnptr typedef, nothing seems to work.

>Thanks

>CTest.h -------

>#include "windows.h"

>typedef void (__stdcall* fnptr)(int i);

>int WINAPI fnCTestCB(fnptr cb);

>TypeLib.idl ----------

>#define CDTAPI __stdcall

>// C DLL Test Interface
>[
>uuid(29FB2B91-F89B-4776-A4B9-67B67C95ABFB),
>helpstring("C DLL Test Interface"),
>lcid (0x00000000),
>version(1.0)
>]

>library CDLLTest
>{
>    importlib("stdole32.tlb");
>    importlib("stdole2.tlb");

>    [
>    uuid(41072DE3-C829-4813-A4A5-6BDC84399133),
>    helpstring("C DLL Test Functions"),
>    dllname("CTest.dll")
>    ]
>    module CDTFunctions
>    {
>      [
>      entry("fnCTestCB"),
>      helpstring("Function that registers a callback.")
>      ]
>      int CDTAPI fnCTestCB(void* cb);
>    }
>};

>VB: mdoule1.bas -------

>Public Function vbCTestCB(ByVal n As Long)
>      Debug.Print "In VB callback n=" & n
>End Function

>VB: Form1.frm ---------

>Private Sub Command1_Click()
>      Call fnCTestCB(AddressOf vbCTestCB)
>End Sub

>.



Sun, 08 May 2005 02:28:29 GMT  
 Howto: declare a C function that registers a callback in IDL

Quote:

> Add extern "c" to your function declaration

Thanks, but that doesn't solve the problem, the functions are exported
through a .def file, sorry I should have included the .def file in the
original post, I have included it below (note that usage of the ";"
comment has been correctly added/removed depending on my test). In my
recent tests, I definitely seem to have a registry problem, although I
can't pin it down, there is *very* little support for unregistering a
.tlb file from microsoft. I rolled my own, using the ITypeLib APIs,
which seems to work on the surface but I'm still getting the error even
when I remove fnCTestCB from both the C DLL and the .idl.

<rant>
COM is great for what it does but, it's dependency on the registry makes
it extremely volatile, MS should really give more support. This whole
thing came about because of the difference in struct alignment in VB and
C/C++, VB has to 4-byte while C/C++ defaults to 8-byte, this requires VB
applications to hack into it's type defintions with padding Byte arrays
or use a TypeLib. The TypeLib solutions seems the right way to go, but
it is turning into a small nightmare to wrap the whole thing and not
just the type defintions. I am surprised that MS has not given more
information on how to wrap a C DLL in a TypeLib, since that seems to be
the most reliable way to interface to C DLLs.

About this VB problem:
1) View dependencies doesn't see fnCTestCB
2) OLE/COM Viewer doesn't see fnCTestCB
3) VB returns ""Can't find dll entry point fnCTestCB in CTest.dll" even
when the function does not exist.
</rant>

Thanks

CTest.def -------
; DEF file

EXPORTS
        fnCTest
        ;fnCTestCB



Sun, 08 May 2005 13:03:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to declare a callback function for use by a DLL

2. how do i declare callback functions

3. How to declare callback

4. Declaring static document class with callbacks

5. How to declare callback

6. Declaring static document class with callbacks

7. How to declare callback

8. BC:howto declare interrupt returning value ?

9. Howto make a custom callback ?

10. Newbie: separate big .cs file into small .cs files

11. Register Callbacks How??

12. Register a callback in a DLL

 

 
Powered by phpBB® Forum Software