Results 1 to 6 of 6

Thread: Subclassing problems [Resolved]

  1. #1

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    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.

  2. #2

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Subclassing problems

    Thought maybe some code would help:

    VB Code:
    1. 'In 1st form
    2. Private Sub Form_Load()
    3.     Dim sSave As String
    4.     hwnd1 = Me.hwnd
    5.     HookForm hwnd1, Me
    6.     StartWinsock sSave
    7.     lSocket1 = ConnectSock("finance.yahoo.com", 80, 0, Me.hwnd, False)
    8. End Sub
    9.  
    10. Private Sub Form_Unload()
    11.     closesocket lSocket1
    12.     EndWinsock
    13.     UnHookForm Me.hwnd
    14. End Sub
    15.  
    16. 'In 2nd form
    17. Private Sub Form_Load()
    18.     Dim sSave As String
    19.     hwnd2 = Me.hwnd
    20.     HookForm hwnd2, Me
    21.     StartWinsock sSave
    22.     lSocket2 = ConnectSock("finance.yahoo.com", 80, 0, Me.hwnd, False)
    23. End Sub
    24.  
    25. Private Sub Form_Unload()
    26.     closesocket lSocket2
    27.     EndWinsock
    28.     UnHookForm Me.hwnd
    29. End Sub
    30.  
    31. 'In a standard module.  Public variables Form1, Form2, lSocket1, lSocket2, hwnd1, hwnd2
    32. Public Sub HookForm(ByVal hwnd As Long, f As Object)
    33.     Select Case hwnd
    34.         Case hwnd1: Set Form1 = f
    35.         Case hwnd2: Set Form2 = f
    36.     End Select
    37.     PrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
    38. End Sub
    39.  
    40. Public Sub UnHookForm(ByVal hwnd As Long)
    41.     If PrevProc <> 0 Then
    42.         SetWindowLong hwnd, GWL_WNDPROC, PrevProc
    43.         PrevProc = 0
    44.     End If
    45. End Sub
    46.  
    47. Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    48.     If uMsg = WINSOCKMSG Then
    49.         ProcessMessage wParam, lParam
    50.     Else
    51.         WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
    52.     End If
    53. End Function
    54.  
    55. Public Sub ProcessMessage(ByVal lFromSocket As Long, ByVal lParam As Long)
    56.     Dim X As Long, ReadBuffer(1 To 1024) As Byte, strCommand As String
    57.     Select Case lParam
    58.         Case FD_CONNECT
    59.         Case FD_WRITE
    60.             Select Case lFromSocket
    61.                 Case lSocket1
    62.                     strCommand = "GET /q?s=INTC HTTP/1.0" + vbCrLf
    63.                     strCommand = strCommand + "Pragma: no-cache" + vbCrLf
    64.                     strCommand = strCommand + "Accept: */*" + vbCrLf
    65.                     strCommand = strCommand + "Accept: text/html" + vbCrLf + vbCrLf
    66.                     SendData lFromSocket, strCommand
    67.                 Case lSocket2
    68.                     strCommand = "GET /q?s=GE HTTP/1.0" + vbCrLf
    69.                     strCommand = strCommand + "Pragma: no-cache" + vbCrLf
    70.                     strCommand = strCommand + "Accept: */*" + vbCrLf
    71.                     strCommand = strCommand + "Accept: text/html" + vbCrLf + vbCrLf
    72.                     SendData lFromSocket, strCommand
    73.             End Select
    74.         Case FD_READ
    75.             Select Case lFromSocket
    76.                 Case lSocket1
    77.                     Do
    78.                         X = recv(lFromSocket, ReadBuffer(1), 1024, 0)
    79.                         If X > 0 Then
    80.                             strData1 = strData1 + Left$(StrConv(ReadBuffer, vbUnicode), X)
    81.                             Debug.Print X; " bytes read....." & lFromSocket
    82.                         End If
    83.                         If X <> 1024 Then Exit Do
    84.                     Loop
    85.                 Case lSocket2
    86.                     Do
    87.                         X = recv(lFromSocket, ReadBuffer(1), 1024, 0)
    88.                         If X > 0 Then
    89.                             strData2 = strData2 + Left$(StrConv(ReadBuffer, vbUnicode), X)
    90.                             Debug.Print X; " bytes read..............." & lFromSocket
    91.                         End If
    92.                         If X <> 1024 Then Exit Do
    93.                     Loop
    94.             End Select
    95.         Case FD_CLOSE
    96.             Select Case lFromSocket
    97.                 Case lSocket1
    98.                     Unload Form1
    99.                     Set Form1 = Nothing
    100.                     Debug.Print strData1
    101.                     strData1 = vbNullString
    102.                 Case lSocket2
    103.                     Unload Form2
    104.                     Set Form2 = Nothing
    105.                     Debug.Print strData2
    106.                     strData2 = vbNullString
    107.             End Select
    108.     End Select
    109. 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

  3. #3

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Subclassing problems

    Forgot one bit:

    VB Code:
    1. 'In standard module
    2. Private Sub TryIt()
    3.     Load Form1
    4.     Load Form2
    5. End Sub

    VBAhack

  4. #4
    Addicted Member Abilio's Avatar
    Join Date
    May 2003
    Location
    Aveiro - Portugal
    Posts
    222

    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.

  5. #5

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    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

  6. #6

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Resolved Subclassing problems [Resolved]

    Resolved.

    VBAhack

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