Click to See Complete Forum and Search --> : Function Pointers


akelleher
January 21st, 2000, 06:47 AM
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

Lothar Haensler
January 21st, 2000, 06:48 AM
why can't you use VB's Declare statement for declaring the DLL function and call it via the Call statement?

akelleher
January 21st, 2000, 06:55 AM
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.

Crazy D @ Work
January 21st, 2000, 07:20 AM
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 :-)

Lothar Haensler
January 21st, 2000, 08:34 AM
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.

Chris Eastwood
January 21st, 2000, 08:56 AM
Like it !

I wonder just how stable it is....


Chris Eastwood

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

Lothar Haensler
January 21st, 2000, 09:01 AM
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?

Chris Eastwood
January 21st, 2000, 09:04 AM
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

akelleher
January 21st, 2000, 09:10 AM
But you're still only looking at a sub from within your VB project. I need to get a function from an external DLL.

Crazy D @ Work
January 21st, 2000, 09:18 AM
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 :-)

Lothar Haensler
January 21st, 2000, 09:28 AM
to get the address of a function in a DLL use the LoadLibrary and GetProcAddress APIs, then try passing the returned address to CallWindowProc.

akelleher
January 21st, 2000, 09:33 AM
Cheers, I'll give it a go!

January 21st, 2000, 02:38 PM
Thanks for that. It works lovely.

akelleher

WizardLee
April 3rd, 2001, 12:32 AM
Beautiful! But how if my BlahBlah need 5 parameters?

Cimperiali
April 3rd, 2001, 01:26 AM
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.