|
-
Feb 20th, 2005, 04:05 AM
#1
Thread Starter
Fanatic Member
Subclassing problems [Resolved]
I'm trying to create a multi-threadedWinsock API application by making multiple calls to HookForm and UnHookForm that call SetWindowLong. I have 2 threads (forms) at the moment. The sockets are created and data is sent for both threads but 1/2 the time data is returned for only 1 of the threads. Is there something fundamental I'm doing wrong?
VBAhack
Last edited by VBAhack; Mar 1st, 2005 at 03:10 PM.
-
Feb 20th, 2005, 12:25 PM
#2
Thread Starter
Fanatic Member
Re: Subclassing problems
Thought maybe some code would help:
VB Code:
'In 1st form
Private Sub Form_Load()
Dim sSave As String
hwnd1 = Me.hwnd
HookForm hwnd1, Me
StartWinsock sSave
lSocket1 = ConnectSock("finance.yahoo.com", 80, 0, Me.hwnd, False)
End Sub
Private Sub Form_Unload()
closesocket lSocket1
EndWinsock
UnHookForm Me.hwnd
End Sub
'In 2nd form
Private Sub Form_Load()
Dim sSave As String
hwnd2 = Me.hwnd
HookForm hwnd2, Me
StartWinsock sSave
lSocket2 = ConnectSock("finance.yahoo.com", 80, 0, Me.hwnd, False)
End Sub
Private Sub Form_Unload()
closesocket lSocket2
EndWinsock
UnHookForm Me.hwnd
End Sub
'In a standard module. Public variables Form1, Form2, lSocket1, lSocket2, hwnd1, hwnd2
Public Sub HookForm(ByVal hwnd As Long, f As Object)
Select Case hwnd
Case hwnd1: Set Form1 = f
Case hwnd2: Set Form2 = f
End Select
PrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHookForm(ByVal hwnd As Long)
If PrevProc <> 0 Then
SetWindowLong hwnd, GWL_WNDPROC, PrevProc
PrevProc = 0
End If
End Sub
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WINSOCKMSG Then
ProcessMessage wParam, lParam
Else
WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
End If
End Function
Public Sub ProcessMessage(ByVal lFromSocket As Long, ByVal lParam As Long)
Dim X As Long, ReadBuffer(1 To 1024) As Byte, strCommand As String
Select Case lParam
Case FD_CONNECT
Case FD_WRITE
Select Case lFromSocket
Case lSocket1
strCommand = "GET /q?s=INTC HTTP/1.0" + vbCrLf
strCommand = strCommand + "Pragma: no-cache" + vbCrLf
strCommand = strCommand + "Accept: */*" + vbCrLf
strCommand = strCommand + "Accept: text/html" + vbCrLf + vbCrLf
SendData lFromSocket, strCommand
Case lSocket2
strCommand = "GET /q?s=GE HTTP/1.0" + vbCrLf
strCommand = strCommand + "Pragma: no-cache" + vbCrLf
strCommand = strCommand + "Accept: */*" + vbCrLf
strCommand = strCommand + "Accept: text/html" + vbCrLf + vbCrLf
SendData lFromSocket, strCommand
End Select
Case FD_READ
Select Case lFromSocket
Case lSocket1
Do
X = recv(lFromSocket, ReadBuffer(1), 1024, 0)
If X > 0 Then
strData1 = strData1 + Left$(StrConv(ReadBuffer, vbUnicode), X)
Debug.Print X; " bytes read....." & lFromSocket
End If
If X <> 1024 Then Exit Do
Loop
Case lSocket2
Do
X = recv(lFromSocket, ReadBuffer(1), 1024, 0)
If X > 0 Then
strData2 = strData2 + Left$(StrConv(ReadBuffer, vbUnicode), X)
Debug.Print X; " bytes read..............." & lFromSocket
End If
If X <> 1024 Then Exit Do
Loop
End Select
Case FD_CLOSE
Select Case lFromSocket
Case lSocket1
Unload Form1
Set Form1 = Nothing
Debug.Print strData1
strData1 = vbNullString
Case lSocket2
Unload Form2
Set Form2 = Nothing
Debug.Print strData2
strData2 = vbNullString
End Select
End Select
End Sub
Connection occurs and data is sent in both cases, but only sometimes is data return for both (always for 1). Any suggestions? Thanks.
VBAhack
-
Feb 20th, 2005, 12:27 PM
#3
Thread Starter
Fanatic Member
Re: Subclassing problems
Forgot one bit:
VB Code:
'In standard module
Private Sub TryIt()
Load Form1
Load Form2
End Sub
VBAhack
-
Feb 22nd, 2005, 06:59 AM
#4
Addicted Member
Re: Subclassing problems
Code:
PrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
You must store Long's against hWnd's and not in variables.
Set Form1 = f can be another problem ! Try to use CopyMemory.
Go to http://www.planetsourcecode.com/vb/s...57119&lngWId=1 and take a look at my code. Make the changes. It will work, i bealive.
-
Feb 28th, 2005, 03:47 PM
#5
Thread Starter
Fanatic Member
Re: Subclassing problems (resolved)
Thanks for your reply. The main problem was that I was ending Winsock prematurely (each Form_Unload had a call to EndWinsock) . When I fixed that it worked but I still got an occasional crash that I never figured out, but my guess was that it had to do with passing forms as Objects. Anyway, I completely abandoned the approach of using forms and decided instead to use a single window (CreateWindowEx API) for subclassing and created multiple sockets attached to the window. I've been sucessfully in creating 52 simultaneous threads w/o any crashes.
VBAhack
-
Feb 28th, 2005, 03:52 PM
#6
Thread Starter
Fanatic Member
Subclassing problems [Resolved]
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
|