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
Printable View
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
why can't you use VB's Declare statement for declaring the DLL function and call it via the Call statement?
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.
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 :-)
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.
Like it !
I wonder just how stable it is....
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
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?
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
But you're still only looking at a sub from within your VB project. I need to get a function from an external DLL.
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 :-)
to get the address of a function in a DLL use the LoadLibrary and GetProcAddress APIs, then try passing the returned address to CallWindowProc.
Cheers, I'll give it a go!
Thanks for that. It works lovely.
akelleher
Beautiful! But how if my BlahBlah need 5 parameters?
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.