Results 1 to 9 of 9

Thread: Why!?!

  1. #1

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134

    Angry Why!?!

    Why, in god's name, did Microsoft decide to make simple things so hard!! I'm trying to create simple multithreading, but LO AND BEHOLD VB6 doesn't let you pass a function in a class to the AddressOf operator >_<. Am I mistaken, or is this functionality intended?!?
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You can only use AddressOf with methods in a standard module.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    That's exactly right. Here is a quote from MSDN:
    ===================================
    AddressOf Operator


    A unary operator that causes the address of theprocedure it precedes to be passed to an API procedure that expects a function pointer at that position in theargument list.

    Syntax

    AddressOf procedurename

    The required procedurename specifies the procedure whose address is to be passed. It must represent a procedure in astandard module module in theproject 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.
    McGenius

  4. #4

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    What a useless, useless piece of utter **** this is

    But! I found a way , this is for all of you people wanting to implement multithreading with parameters:

    When you create the thread, you'll get the THREADID. Make all of your threads call back to the same function (in my case, I am making a PINGING program. My function is called DoPing, so "AddressOf DoPing"). Now, in the DoPing function (and here's the genius ), you use the GetCurrentThreadId API call! . Then you find out which array member (or collection, or whatever you're doing) the threadid belongs to, and voila, you can now do whatever you need because you have the array index and from this you can discern any info you originally wanted (in my case I actually wanted to pass the array index so this is perfect).
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Good thought. This is what is usually done when subclassing several objects as well. Like with a usercontrol, all the instances will share the same standard module within the project, so you add the current instance to a collection that keys off of the hWnd. And since hWnds are always unique, there is no worry about acting on the wrong object.

    Same basic idea.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    That's the way it should work - remember that in OO programming, functions work slightly different - there is a hidden first argument to the function that gets a pointer or reference to the specific instance of the object - VB calls this reference "Me", other languages use "this", etc. So the first argument in the function that the API would callback on would need to be a reference to the VB class instance, not doable.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  7. #7

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    That's too bad, then. I suppose the reason I was so surprised was that I just delved into .NET and you could use a class' functions with the AddressOf operatior (when they were SHARED functions).
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  8. #8
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Does shared mean per class rather than per instance? This is not a VB issue - the same issue will occur in any OO language that also allows references or pointers to functions...
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  9. #9

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    No, shared just means you can call the function or sub, or access the variable/class from within the same class (ie. another function or sub). For example:
    VB Code:
    1. Class MyClass
    2.     Dim Shared A As Long
    3.     Dim B As Long
    4.     Public Sub New
    5.         A = 15 'Works
    6.         B = 15 'Doesn't work
    7.         C() 'Works
    8.         D() 'Doesn't work
    9.     End Sub
    10.     Shared Sub C()
    11.         Console.WriteLine "C() Invoked."
    12.     End Sub
    13.     Sub D()
    14.         Console.WriteLine "D() Invoked."
    15.     End Sub
    16. End Class
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

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