|
-
Jan 15th, 2003, 09:52 PM
#1
Thread Starter
Good Ol' Platypus
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)
-
Jan 15th, 2003, 10:30 PM
#2
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
-
Jan 15th, 2003, 10:37 PM
#3
Frenzied Member
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.
-
Jan 16th, 2003, 12:21 AM
#4
Thread Starter
Good Ol' Platypus
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)
-
Jan 16th, 2003, 07:49 AM
#5
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
-
Jan 16th, 2003, 11:20 AM
#6
Black Cat
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.
-
Jan 16th, 2003, 03:59 PM
#7
Thread Starter
Good Ol' Platypus
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)
-
Jan 17th, 2003, 10:52 AM
#8
Black Cat
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.
-
Jan 19th, 2003, 07:05 PM
#9
Thread Starter
Good Ol' Platypus
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:
Class MyClass
Dim Shared A As Long
Dim B As Long
Public Sub New
A = 15 'Works
B = 15 'Doesn't work
C() 'Works
D() 'Doesn't work
End Sub
Shared Sub C()
Console.WriteLine "C() Invoked."
End Sub
Sub D()
Console.WriteLine "D() Invoked."
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|