Results 1 to 8 of 8

Thread: AddressOf problem

  1. #1

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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.

  3. #3

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    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:
    1. Option Explicit
    2. Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    3. Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    4. Private mlngTimerID As Long
    5.  
    6.  
    7. Public Sub TimerCallBack(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    8. 'TIMER FUNCTION .. DONT KNOW HOW IT WORK..JUTS THAT I KNOW IT DOES
    9. Call MailMon ' calls the mail sub routine NOT INCLUDED
    10. End Sub
    11.  
    12. Public Sub starttimer(lngInterval As Long) 'SUB TO GET EVERYTHING GOING.
    13. mlngTimerID = SetTimer(0, 0, lngInterval, AddressOf TimerCallBack)
    14. End Sub
    15.  
    16.  
    17. Public Sub StopTimer() 'RUN THIS TO STOP TIMER*********************
    18.     KillTimer 0, mlngTimerID
    19. End Sub
    20. Sub startstimer() ' START THIS TO START TIMER**********************
    21. starttimer (60000) ' milliseconds u figure it out
    22. End Sub
    I tyred to make it work with something else.. which looks like this.
    VB Code:
    1. Option Explicit
    2. Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    3. Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    4. Private mlngTimerID As Long
    5. Private emailAddress As String
    6.  
    7.  
    8. Public Sub TimerCallBack(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    9. 'TIMER FUNCTION
    10.  Dim myOLApp As New Outlook.Application
    11.     Dim myOLItem As Outlook.mailItem
    12.     Set myOLItem = myOLApp.CreateItem(olMailItem)
    13.     With myOLItem
    14.     .To = emailAddress
    15.         .Subject = "Time Untill Close"
    16.         .Body = "This is the time: " & Time & " So theres " & 60 - Minute(Now) & " Minutes Left to work"
    17.         .Send
    18.     End With
    19. End Sub
    20.  
    21. Public Sub cmdStartTimer_Click()
    22. Dim lngInterval As Long
    23.     lngInterval = Interval(cbinterval.Text, txttime.Text)
    24.     emailAddress = txtemailAddress.Text
    25.         If emailAddress = vbNullString Then Exit Sub
    26.         If lngInterval = Null Then Exit Sub
    27.     mlngTimerID = SetTimer(0, 0, lngInterval, AddressOf TimerCallBack)
    28. MsgBox "Timer set!!"
    29. End Sub
    30.  
    31. Private Sub cmdStopTimer_Click()
    32.  KillTimer 0, mlngTimerID
    33. End Sub
    34. Private Function Interval(intvl As String, length As Long) As Long
    35. If intvl = "min" Then
    36.     Interval = length * 60000
    37. ElseIf intvl = "hour" Then
    38.     Interval = length * 3600000
    39. ElseIf intvl = "day" Then
    40.     Interval = length * 86400000
    41. Else
    42. MsgBox "something went wrong with calc interval"
    43. End If
    44. End Function
    It doesnt like the addressof this right at the start of the procedure.



    Sorry for all the code..

    Thanks

  4. #4

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    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

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    AddressOf does not support call backs to functions or procedures on a Form. ONLY a module (in my opinion, one of its serious drawbacks. )

  6. #6

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    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?

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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. )

  8. #8

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    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:
    1. Private Sub Form_Load()
    2.     SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
    3. End Sub
    4.  
    5. Private Sub Form_Unload(Cancel As Integer)
    6.     KillTimer Me.hwnd, 0
    7. End Sub
    8.  
    9.  
    10. Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    11. Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    12.  
    13. Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
    14.     MsgBox "hi"
    15. 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
  •  



Click Here to Expand Forum to Full Width