|
-
Apr 18th, 2002, 08:54 AM
#1
Thread Starter
Fanatic Member
AddressOf problem
HI.
I am using AddressOf thinggy (dont really know why thats the problem) in a line of code.
I am getting an error when debugging. ARE there any restrictions.
like.. shouldnt be used in a form.... or public memberss..
I am asking because.. it works in one context. and not another.
Seahag
I will post code if necessary
-
Apr 18th, 2002, 08:59 AM
#2
Without seeing your code, or knowing the exact nature of your error, I can only respond with a text book definition of AddressOf and its use. If this is insufficient, post your code and we'll have a look. 
AddressOf procedurename
The required procedure name specifies the procedure whose address is to be passed. It must represent a procedure in a standard module in the project in which the call is made.
Remarks
When a procedure name appears in an argument list, usually the procedure is evaluated, and the address of the procedure’s return value is passed. AddressOf permits the address of the procedure to be passed to a Windows API function in adynamic-link library (DLL), rather passing the procedure's return value. The API function can then use the address to call the Basic procedure, a process known as a callback. The AddressOf operator appears only in the call to the API procedure.
Although you can use AddressOf to pass procedure pointers among Basic procedures, you can't call a function through such a pointer from within Basic. This means, for example, that aclass written in Basic can't make a callback to its controller using such a pointer. When using AddressOf to pass a procedure pointer among procedures within Basic, theparameter of the called procedure must be typed As Long.
Warning Using AddressOf may cause unpredictable results if you don't completely understand the concept of function callbacks. You must understand how the Basic portion of the callback works, and also the code of the DLL into which you are passing your function address. Debugging such interactions is difficult since the program runs in the same process as thedevelopment environment. In some cases, systematic debugging may not be possible.
Note You can create your own call-back function prototypes in DLLs compiled with Microsoft Visual C++ (or similar tools). To work with AddressOf, your prototype must use the __stdcall calling convention. The default calling convention (__cdecl) will not work with AddressOf.
Since the caller of a callback is not within your program, it is important that an error in the callback procedure not be propagated back to the caller. You can accomplish this by placing the On Error Resume Next statement at the beginning of the callback procedure.
Last edited by Hack; Apr 18th, 2002 at 10:01 AM.
-
Apr 18th, 2002, 09:06 AM
#3
Thread Starter
Fanatic Member
OK
This type of coding confuses me because
I am just cutting and pasting..
The problem is with a timer function..
I cut and pasted this....
VB Code:
Option Explicit
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private mlngTimerID As Long
Public Sub TimerCallBack(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
'TIMER FUNCTION .. DONT KNOW HOW IT WORK..JUTS THAT I KNOW IT DOES
Call MailMon ' calls the mail sub routine NOT INCLUDED
End Sub
Public Sub starttimer(lngInterval As Long) 'SUB TO GET EVERYTHING GOING.
mlngTimerID = SetTimer(0, 0, lngInterval, AddressOf TimerCallBack)
End Sub
Public Sub StopTimer() 'RUN THIS TO STOP TIMER*********************
KillTimer 0, mlngTimerID
End Sub
Sub startstimer() ' START THIS TO START TIMER**********************
starttimer (60000) ' milliseconds u figure it out
End Sub
I tyred to make it work with something else.. which looks like this.
VB Code:
Option Explicit
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private mlngTimerID As Long
Private emailAddress As String
Public Sub TimerCallBack(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
'TIMER FUNCTION
Dim myOLApp As New Outlook.Application
Dim myOLItem As Outlook.mailItem
Set myOLItem = myOLApp.CreateItem(olMailItem)
With myOLItem
.To = emailAddress
.Subject = "Time Untill Close"
.Body = "This is the time: " & Time & " So theres " & 60 - Minute(Now) & " Minutes Left to work"
.Send
End With
End Sub
Public Sub cmdStartTimer_Click()
Dim lngInterval As Long
lngInterval = Interval(cbinterval.Text, txttime.Text)
emailAddress = txtemailAddress.Text
If emailAddress = vbNullString Then Exit Sub
If lngInterval = Null Then Exit Sub
mlngTimerID = SetTimer(0, 0, lngInterval, AddressOf TimerCallBack)
MsgBox "Timer set!!"
End Sub
Private Sub cmdStopTimer_Click()
KillTimer 0, mlngTimerID
End Sub
Private Function Interval(intvl As String, length As Long) As Long
If intvl = "min" Then
Interval = length * 60000
ElseIf intvl = "hour" Then
Interval = length * 3600000
ElseIf intvl = "day" Then
Interval = length * 86400000
Else
MsgBox "something went wrong with calc interval"
End If
End Function
It doesnt like the addressof this right at the start of the procedure.
Sorry for all the code..
Thanks
-
Apr 18th, 2002, 09:43 AM
#4
Thread Starter
Fanatic Member
Eekk..
Thnks hack. Didn`t see the bolded text
K.. is there a way i can have this code reside in a form.
I guess the problem is the timer function by having to put it in a
module. Is there a workaround?
Seahag
-
Apr 18th, 2002, 09:48 AM
#5
AddressOf does not support call backs to functions or procedures on a Form. ONLY a module (in my opinion, one of its serious drawbacks. )
-
Apr 18th, 2002, 09:56 AM
#6
Thread Starter
Fanatic Member
ok so its just not me..
1. Whats a call back.. (prob a better question for later)
2. Is there a better timer other than a control. I am using this code in outlook. the controls are kinda clumsy. and gettickcount HOGGS the processor(i think)
Any solutions?
-
Apr 18th, 2002, 10:06 AM
#7
A call back is exactly what it sounds like.
It is a procedure that makes a "call back" to a function or procedure to process certain events.
(This is the "5 cent" definition, but if you boil down all the technical stuff with the official definition, this is essentially what you get. )
-
Apr 18th, 2002, 10:16 AM
#8
Thread Starter
Fanatic Member
K better question..
Why did this person do that? Whats the advantage of a call back.
I just ran across this code.. Itseems to be very similar.. but with no callback
VB Code:
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
End Sub
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
MsgBox "hi"
End Sub
Thanks for your help Hack.
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
|