CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: DLL Callbacks

  1. #1
    Join Date
    Sep 1999
    Location
    CA
    Posts
    83

    DLL Callbacks

    I was wondering if someone could point me in the direction of preferably a working example or some good reference material for a Visual Basic Callback function to a C dll. I've read through some articles in MSDN but can't get one to work correctly. As a test I just wanted to make VB executable that on a button click would call a DLL function that in turn would call a VB function with an integer parameter specified by the DLL.

    Here is the complete code (minus the .def file from my test dll)

    typedef int (*LPCALLBACK)(long percent);

    int __stdcall MyCFunction(LPCALLBACK lp)
    {
    lp(5);
    return 0;
    }

    and here is the code from my test VB module
    Option Explicit
    Public Declare Function MyCFunctionLib _
    "testdll" (ByVal lngFnPtr As Long) As Long

    Public Function how(a As Integer)
    MsgBox a
    End Function

    and here is the code from my main test form:

    Private Sub Command1_Click()
    Call MyCFunction(AddressOf how)
    End Sub

    When I run the executable the messagebox always comes up with -3028 and then I get an object variable or block with not set error. Can anyone help me please =). Thanks in advance. This is all in Visual Studio 6 btw.



  2. #2
    Join Date
    Sep 1999
    Location
    Istanbul
    Posts
    85

    Re: DLL Callbacks

    Using C/C++ or VC++ wrotten DLLs in VB explains in my example at: http://www.tair.freeservers.com
    dllWorks Project.


  3. #3
    Join Date
    Sep 1999
    Location
    CA
    Posts
    83

    Re: DLL Callbacks

    I checked your link, and maybe I am missing something, but I didn't find anything about callbacks in the code. As of right now, I have gotten other DLLs to work with my program, but I just want to know how to specifically do a callback correctly. I know that I am passing the function (via addressof) in my code, because that function does get called, however the DLL is not passing parameters to this function correctly. Thanks for your assistance.


  4. #4
    Guest

    Re: DLL Callbacks

    OK, try this one more time...

    In your C function you declared your callback as returning an int. But your VB Declaration returns nothing.
    Also, keep in mind the difference between int in C and Integer in VB. The first is 4 bytes wide, the latter is 2 bytes wide. I prefer to stick to Long (long) which is the same width in both - avoids confusion...

    Also, ByVal is the preferred way to pass variables around.
    If you're sending a pointer (that would allow the function to change the value) then use ByRef.

    Try this VB declaration...

    Public Function how(Byval a As Long) as long


    ERX


  5. #5
    Join Date
    Apr 2000
    Location
    Houston, TX, USA
    Posts
    199

    Re: DLL Callbacks

    Another point to consider is that the how() function MUST reside in a module. AddresOf will not work for any other function or sub placed any where else. Great huh?

    Tim Cartwright 'Will write code for food.
    Information Systems
    Splitrock Services Inc.
    Tim C.
    //Will write code for food

  6. #6
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: DLL Callbacks

    "Another point to consider is that the how() function MUST reside in a module. AddresOf will not work for any other function or sub placed any where else. Great huh?"

    Uhmm well, at least, VB is object oriented at that part.... since a form is a class, you can't use a function inside it for callback., just like in C++ where a function in a class can't be used for callbacks (ok, it can, but takes a huge pain to do so....)

    Crazy D :-)
    "One ring rules them all"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured