CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Dec 1999
    Posts
    7

    Function Pointers

    I'm trying to call some functions from an external DLL by using pointers. AddressOf only works for functions within my VB project. What else can I use?

    Alison


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Function Pointers

    why can't you use VB's Declare statement for declaring the DLL function and call it via the Call statement?


  3. #3
    Join Date
    Dec 1999
    Posts
    7

    Re: Function Pointers

    Because I receive the name of the function as a variable. It's from rewriting an old batch file system that creates the function names from a database.


  4. #4
    Join Date
    Apr 1999
    Location
    Netherlands
    Posts
    181

    Re: Function Pointers

    A long time ago someone posted code to call functions from pointers, but it crashes...

    declarations:

    private Declare Function CallFunctionFromPointerWithNoParams Lib "user32" Alias "CallWindowProcA" (byval lpfnFunctionPointer as Long) as Long
    private Declare Function CallFunctionFromPointerWith1Param Lib "user32" Alias "CallWindowProcA" (byval lpfnFunctionPointer as Long, Param1AndOnly as Any) as Long
    private Declare Function CallFunctionFromPointerWith2Params Lib "user32" Alias "CallWindowProcA" (byval lpfnFunctionPointer as Long, Param1 as Any, Param2 as Any) as Long
    private Declare Function CallFunctionFromPointerWith3Params Lib "user32" Alias "CallWindowProcA" (byval lpfnFunctionPointer as Long, Param1 as Any, Param2 as Any, Param3 as Any) as Long



    Sorry I don't know who it wrote...
    It's the callwindowproc modified (since that's officialy declared with one paramater and ... so you can add as much params as you wish).
    As a test, this is what I have...
    in a module:

    public Sub BlahBlah()
    MsgBox "Testje"
    End Sub



    call it like:

    private Sub Command1_Click()
    Call testing(AddressOf BlahBlah)
    End Sub
    private Sub testing(byref lp as Long)
    CallFunctionFromPointerWithNoParams (lp)
    End Sub



    The msgbox is shown, but when the blahblah sub ends, it crashes...
    (NT4 - Sp5, VB6-SP3). Anyone get this working?

    Crazy D @ Work :-)

  5. #5
    Join Date
    May 1999
    Posts
    3,332

    Re: Function Pointers

    very cool!

    here is a version that doesn't break (or should I say DIDN't break on my machine).



    public Sub blahblah(byval hwnd as Long, _
    byval lMsg as Long, _
    byval wParam as Long, _
    byval lParam as Long)
    MsgBox "test"
    End Sub

    private Declare Function _
    callindirect Lib "user32" _
    Alias "CallWindowProcA" ( _
    byval lpfnFunctionPointer as Long, _
    hwnd as Any, _
    byval lMsg as Long, _
    byval wParam as Long, _
    byval lParam as Long) as Long

    private Sub Command1_Click()
    Call testing(AddressOf blahblah)
    End Sub
    private Sub testing(byref lp as Long)
    callindirect lp, byval 0&, 0&, 0&, 0&
    End Sub






    IMHO the other versions do not work.
    There's an article on MSDN that explains why.


  6. #6
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Function Pointers

    Like it !

    I wonder just how stable it is....


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  7. #7
    Join Date
    May 1999
    Posts
    3,332

    Re: Function Pointers

    well: no risk, no fun :-)

    OTOH that function (CallWindowProc) was obiously not designed for what it was used in those samples.
    I'd never use that stuff in a real application.

    Have you experienced any crashes, yet?


  8. #8
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Function Pointers

    I've had no crashes at all - I'll try it on Win98 later tonight if I get a chance. As CallWindowProc is a pretty standard windows routine, I don't see any reason why you couldn't use it in a real app (but theres no way you'd get me doing it ;-)



    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  9. #9
    Join Date
    Dec 1999
    Posts
    7

    Re: Function Pointers

    But you're still only looking at a sub from within your VB project. I need to get a function from an external DLL.




  10. #10
    Join Date
    Apr 1999
    Location
    Netherlands
    Posts
    181

    Re: Function Pointers

    akelleher: yes, it's in the same project, but it's a start :-)

    Chris: me neither,especially not since I found out the objptr function and I understand implements now... with that I can do what I want :-)

    Crazy D @ Work :-)

  11. #11
    Join Date
    May 1999
    Posts
    3,332

    Re: Function Pointers

    to get the address of a function in a DLL use the LoadLibrary and GetProcAddress APIs, then try passing the returned address to CallWindowProc.


  12. #12
    Join Date
    Dec 1999
    Posts
    7

    Re: Function Pointers

    Cheers, I'll give it a go!


  13. #13
    Guest

    Re: Function Pointers

    Thanks for that. It works lovely.

    akelleher


  14. #14
    Join Date
    Jun 2000
    Posts
    46

    Re: Function Pointers

    Beautiful! But how if my BlahBlah need 5 parameters?


  15. #15
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Function Pointers

    It is always great to read from you, now as at that time.

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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