|
-
January 21st, 2000, 07:47 AM
#1
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
-
January 21st, 2000, 07:48 AM
#2
Re: Function Pointers
why can't you use VB's Declare statement for declaring the DLL function and call it via the Call statement?
-
January 21st, 2000, 07:55 AM
#3
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.
-
January 21st, 2000, 08:20 AM
#4
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 :-)
-
January 21st, 2000, 09:34 AM
#5
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.
-
January 21st, 2000, 09:56 AM
#6
Re: Function Pointers
Like it !
I wonder just how stable it is....
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
-
January 21st, 2000, 10:01 AM
#7
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?
-
January 21st, 2000, 10:04 AM
#8
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
-
January 21st, 2000, 10:10 AM
#9
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.
-
January 21st, 2000, 10:18 AM
#10
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 :-)
-
January 21st, 2000, 10:28 AM
#11
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.
-
January 21st, 2000, 10:33 AM
#12
Re: Function Pointers
Cheers, I'll give it a go!
-
January 21st, 2000, 03:38 PM
#13
Re: Function Pointers
Thanks for that. It works lovely.
akelleher
-
April 3rd, 2001, 12:32 AM
#14
Re: Function Pointers
Beautiful! But how if my BlahBlah need 5 parameters?
-
April 3rd, 2001, 01:26 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|