dcsimg
Results 1 to 9 of 9

Thread: vbRichClient cSortedDictionary

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2019
    Posts
    21

    vbRichClient cSortedDictionary

    I have used this as a replacement for scripting.dictionary but run into a problem. Has anyone else had the experience of cSortedDictionary either not adding a key or not finding a key?

    I THINK i have this problem and am currently testing by replacing cSorted with scripting.dictionary and the problem seems to go away.

  2. #2
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,073

    Re: vbRichClient cSortedDictionary

    Posting some code that illustrates the problem would be very helpful
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  3. #3
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,192

    Re: vbRichClient cSortedDictionary

    Quote Originally Posted by vbwins View Post
    I have used this as a replacement for scripting.dictionary but run into a problem. Has anyone else had the experience of cSortedDictionary either not adding a key or not finding a key?

    I THINK i have this problem and am currently testing by replacing cSorted with scripting.dictionary and the problem seems to go away.
    In an earlier private message you explained, that your problems with the cSortedDictionary occur in "a callback".

    And since I'm using the cSortedDictionary in a whole lot of Apps and COM-Dlls without any problems -
    I can only assume that your callback is a "threaded one" (coming in from a foreign created thread,
    which was not created as a COM-compatible STA-Thread).

    That the Scripting-Dictionary has less of a problem (when called back from such a "non STA-thread")
    is not surprising - it was implemented in C(++) and does not have to ensure a proper initializing of the
    vbRuntime-Dll on said thread...

    I can only underline the request of Colin (regarding your "showing us some code") - a request I made also in an
    answer to your Private Message you've sent me earlier... it would probably help already, when you could shed some
    more light on, what kind of Callback we are talking about (to be able to determine, whether it's a threaded one).

    HTH

    Olaf

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2019
    Posts
    21

    Re: vbRichClient cSortedDictionary

    Hi Olaf and thanks for responding,

    I sent you a PM from a different username which I can no longer access and all attempts to reset the password & ask admins to help have resulted in silence...

    If I remember I did sent you the offending code in the PM. It was being called from an asynchronous DNS request callback via sub classing so I guess it is not technically a callback I suppose.

    What would happen is that I would add the item, check if it added (sometimes it did) and then if the check failed using the exact same key (actually cut and paste the add code) it would add it again which usually worked. I swapped it out for scripting dictionary and the problem went away.

    Since that time I have moved on from that approach for reasons which are a bit beyond the scope of the question. I just wanted to know if anyone had a similar problem.

    As a side bar is there a datatype to use as a key that will provide better performance than say a string?

    cheers

  5. #5
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    458

    Re: vbRichClient cSortedDictionary

    @vbwins you have been asked nicely twice now to show some code. I wonder why you won't.

    Olaf makes it clear he received no code from you via PM. Please show some code so that you may be helped.

  6. #6
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,192

    Re: vbRichClient cSortedDictionary

    Quote Originally Posted by Steve Grant View Post
    Olaf makes it clear he received no code from you via PM. Please show some code so that you may be helped.
    That would be appreciated - yes.
    Even the names of the used async DNS-APIs would help (and a few infos, what exactly is meant and used with regards to "subclassing") -
    to at least identify, whether my assumption of a "free-threaded callback" being the culprit, is correct.

    For the record:
    I received a few lines of code via PM - but these were showing only normal Dict.Add and Dict.Exists calls
    (which worked flawlessly in a normal singlethreaded StdExe-scenario).

    It's a bit like posting only something like:
    "My app crashes on the following line - is CopyMemory faulty?"
    --> CopyMemory MyStrPtr, MySourcePtr, SomeLenVariable

    Without any info, where the Parameters came from, and what their current content was.

    @vbwins
    So, I really need some context (some surrounding code), to identify if the problem is threading-related (async DNS-API callbacks sure sound suspicious).

    And as said already - the MS-Scripting-Dictionary could survive in a free-threaded callback better,
    because it does not depend on a (TLS-wise) correctly initialized vbRuntime (like the cSortedDictionary).

    Generally, the approach how we deal in VB6 with such "callbacks from free-threaded async-system-libs" is usually,
    that only TypeLib-defined API-calls can be used safely within them - so we only re-delegate and decouple from these callbacks
    to our safe main-thread, via (TypeLib-defined) PostMessage-calls or something similar - exiting the callback as soon as possible.

    Olaf

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2019
    Posts
    21

    Re: vbRichClient cSortedDictionary

    Ok I will put the code back the way it was, attempt to reproduce and revert.

    thanks for the responses.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2019
    Posts
    21

    Re: vbRichClient cSortedDictionary

    Quote Originally Posted by Schmidt View Post
    That would be appreciated - yes.
    Even the names of the used async DNS-APIs would help (and a few infos, what exactly is meant and used with regards to "subclassing") -
    to at least identify, whether my assumption of a "free-threaded callback" being the culprit, is correct.

    For the record:
    I received a few lines of code via PM - but these were showing only normal Dict.Add and Dict.Exists calls
    (which worked flawlessly in a normal singlethreaded StdExe-scenario).

    It's a bit like posting only something like:
    "My app crashes on the following line - is CopyMemory faulty?"
    --> CopyMemory MyStrPtr, MySourcePtr, SomeLenVariable

    Without any info, where the Parameters came from, and what their current content was.

    @vbwins
    So, I really need some context (some surrounding code), to identify if the problem is threading-related (async DNS-API callbacks sure sound suspicious).

    And as said already - the MS-Scripting-Dictionary could survive in a free-threaded callback better,
    because it does not depend on a (TLS-wise) correctly initialized vbRuntime (like the cSortedDictionary).

    Generally, the approach how we deal in VB6 with such "callbacks from free-threaded async-system-libs" is usually,
    that only TypeLib-defined API-calls can be used safely within them - so we only re-delegate and decouple from these callbacks
    to our safe main-thread, via (TypeLib-defined) PostMessage-calls or something similar - exiting the callback as soon as possible.

    Olaf
    Ok I hope I have included everything.

    Here is where the windows message is hooked

    Public Function tcpAgentAsyncDnsHook(hWnd As Long)
    asyncDnsSupport.lpPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf dnsWindowProc)
    End Function

    Ok here is where the request is pushed out from a class

    Public Function tcpAgentAsyncReverseLookup(strHostAddress As String, ByVal hWnd) As Boolean
    10 On Error GoTo errorHandler

    Dim lngRequestID As Long

    Dim lngMemoryPointer As Long

    Dim lngAddress As Long

    Dim strKey As String

    20 tcpAgentAsyncReverseLookup = True

    30 If Not m_dnsTried.Exists(strHostAddress) Then

    writeAgentLog "DNS - Lookup. Trying to look up " & strHostAddress

    40 m_dnsTried.Add strHostAddress, 0

    50 Else

    60 Exit Function

    70 End If

    80 If asyncDnsSupport.m_lngResolveMessage = 0 Then

    90 asyncDnsSupport.m_lngResolveMessage = RegisterWindowMessage(App.EXEName & ".ResolveMessage")

    100 writeAgentLog "INFORMATION - Registered windows message for DNS resolution"

    110 End If

    120 lngMemoryPointer = MAlloc(MAXGETHOSTSTRUCT)
    '
    130 If lngMemoryPointer = 0 Then

    140 tcpAgentAsyncReverseLookup = False

    150 Exit Function
    '
    160 End If

    170 lngAddress = inet_addr(strHostAddress)

    180 lngRequestID = WSAAsyncGetHostByAddr(hWnd, m_lngResolveMessage, lngAddress, 4&, AF_INET, ByVal lngMemoryPointer, MAXGETHOSTSTRUCT) 'Modified: 04-MAR-2002

    190 strKey = lngRequestID & ":P"

    200 asyncDnsSupport.m_colMemoryBlocks.Add strKey, lngMemoryPointer

    210 strKey = lngRequestID & ":IP"

    220 asyncDnsSupport.m_colMemoryBlocks.Add strKey, strHostAddress

    230 strKey = lngRequestID & ":RT"

    240 asyncDnsSupport.m_colMemoryBlocks.Add strKey, timeGetTime
    '
    250 If lngRequestID = 0 Then

    260 Free (lngMemoryPointer)

    270 lngMemoryPointer = 0

    280 strKey = lngRequestID & ":P"

    290 asyncDnsSupport.m_colMemoryBlocks.Remove strKey

    300 strKey = lngRequestID & ":IP"

    310 asyncDnsSupport.m_colMemoryBlocks.Remove strKey

    320 strKey = lngRequestID & ":RT"

    330 asyncDnsSupport.m_colMemoryBlocks.Remove strKey

    340 End If

    350 Exit Function

    errorHandler:

    360 errorDisplay Err.description, "tcpAgentAsyncReverseLookup", Erl

    370 If lngMemoryPointer <> 0 Then

    380 Free (lngMemoryPointer)

    390 End If

    400 Resume endofitall
    endofitall:
    End Function

    Here is where the windows message was processed. This code is not longer in use. The commented out section use to add to cSortedDictionary. This would fail intermittently with no exception thrown. It would log that it had added it but could not look it up and added it again.

    Public Function dnsWindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    10 On Error GoTo errorHandler

    Dim lngErrorCode As Long

    Dim strKey As String

    Dim lngMemoryAddress As Long

    Dim udtHost As HOSTENT

    Dim lngIpAddrPtr As Long

    Dim lngHostAddress As Long

    Dim strHostName As String

    Dim strHostIp As String

    Dim startTime As Long

    Dim existingName As String

    Dim status As Long

    Dim realHostName As String

    Dim realIp As String

    20 If uMsg <> m_lngResolveMessage Then

    30 dnsWindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)

    40 Else
    50
    60 lngErrorCode = HiWord(lParam)

    70 strKey = wParam & ":P"

    80 If m_colMemoryBlocks.Exists(strKey) Then

    90 lngMemoryAddress = m_colMemoryBlocks(strKey)

    100 m_colMemoryBlocks.Remove strKey

    110 strKey = wParam & ":IP"

    120 strHostIp = m_colMemoryBlocks(strKey)

    130 realIp = strHostIp

    140 m_colMemoryBlocks.Remove strKey

    150 strKey = wParam & ":RT"

    160 startTime = m_colMemoryBlocks(strKey)

    170 m_colMemoryBlocks.Remove strKey

    180 If lngErrorCode = 0 Then

    190 CopyMemory udtHost, ByVal lngMemoryAddress, Len(udtHost)

    200 CopyMemory lngIpAddrPtr, ByVal udtHost.hAddrList, 4

    210 CopyMemory lngHostAddress, ByVal lngIpAddrPtr, 4

    220 strHostName = StringFromPointer(udtHost.hName)

    230 realHostName = strHostName

    240 Else

    250 strHostName = "Unknown"

    260 realHostName = "Unknown"

    270 End If

    280 If Not Module1.conDnsAndPortCache.Exists(realIp) Then

    290 Module1.conDnsAndPortCache.Add realIp, realHostName

    300 If Module1.conDnsAndPortCache.Exists(realIp) Then

    310 writeAgentLog "INFORMATION - Cached IP " & realIp & " as " & realHostName & " dictionary count " & Module1.conDnsAndPortCache.count

    320 Else

    330 writeAgentLog "ERROR - Cached IP " & realIp & " was added but could not be looked up.Trying again " & " dictionary count " & Module1.conDnsAndPortCache.count & " trying again"

    340 Module1.conDnsAndPortCache.Add realIp, realHostName

    350 End If

    360 End If

    370 Free (lngMemoryAddress)

    380 lngMemoryAddress = 0

    390 Exit Function

    400 Else ' need some error handling here

    410 End If

    420 End If

    430 Exit Function

    errorHandler:

    440 errorDisplay Err.description, "dnsWindowProc", Erl

    450 If lpPrevWndProc <> 0 Then

    460 dnsWindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)

    470 Else

    480 dnsWindowProc = 0

    490 End If

    500 If lngMemoryAddress <> 0 Then

    510 Free (lngMemoryAddress)

    520 End If

    530 Resume endofitall

    endofitall:
    End Function

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2019
    Posts
    21

    Re: vbRichClient cSortedDictionary

    Sorry all I don't know why all my code indents disappeared when I pasted it in.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width