I'm having some trouble with InString, although it may be the globallock thing.

Basically, I have winsock app acting as an IRC client and in the FD_READ message it calls WS_ReadData:



; ============= Read Data from the socket =============
WS_ReadData PROC
invoke ioctlsocket, sock, FIONREAD, addr sizetoread ; Check the number of bytes available to read from the socket
invoke GlobalAlloc, GHND, sizetoread ; allocate memory enough for the data to read from the socket
mov mHandle, eax
invoke GlobalLock, eax
mov recvAddr, eax
invoke recv, sock, recvAddr, sizetoread, 0
.if eax==SOCKET_ERROR
invoke WSAGetLastError
invoke wsprintfA, offset buff, offset txtError2, eax
invoke MessageBox, NULL, addr buff, addr MsgBoxCaption, MB_OK
.else
invoke IRC_ProcessData
;invoke MessageBox, NULL, addr txtError2, addr MsgBoxCaption, MB_OK
.endif
invoke GlobalUnlock, recvAddr
invoke GlobalFree, mHandle
ret
WS_ReadData ENDP


As you can see, this calles IRC_ProcessData, which is this:



IRC_ProcessData PROC
invoke SetDlgItemText, hwnd, IDC_EDIT, recvAddr
;invoke MessageBox, NULL, recvAddr, addr MsgBoxCaption, MB_OK

mov eax, 0
invoke InString, 1, [recvAddr], addr srchCmdPing ; Test for PING
.if eax == 1 ; If it IS a ping, it will be start at position 1, so safe to assume :)
invoke lnstr, recvAddr ; Get the full length of the string from the IRC Server
mov ebx, eax ; put the length of it into strlen
sub ebx, 8 ;
invoke szMid, recvAddr, addr tmpBuff, 6, ebx

invoke wsprintfA, offset buff, offset sndCmdPong, addr tmpBuff
;invoke MessageBox, NULL, addr buff, addr MsgBoxCaption, MB_OK

invoke lnstr, addr buff
invoke send, sock, addr buff, eax, 0

; Now if the connection state is 1 (still connecting) it means this is the first Ping we've received so we need to send
; the command to join the channel
.if connectionState == 1
invoke lnstr, addr sndCmdJoin
invoke send, sock, addr sndCmdJoin, eax, 0
.endif
mov eax, 1
ret
.endif

invoke InString, 1, [recvAddr], addr srchCmdJoin ; Test for a result of the Join command
.if eax > 0 ; If it IS, the return value will be > 0
mov connectionState, 2 ; As we've now got a reply from join, the connection state progresses
invoke MessageBox, NULL, addr sndCmdJoin, addr MsgBoxCaption, MB_OK
ret
.endif

ret
IRC_ProcessData ENDP


This comes up with an Application error indicating it referenced memory at 0xc0000000.

But, when I add a messagebox somewhere in there it runs fine. I assume it's a problem with data being received too quickly, or it's just dodgy coding :) .


...akenny
Posted on 2001-09-10 18:55:56 by akenny
IIRC, there was a bug in InString... Hutch solved the problem but I don't know if that was before or after MASM SP2...
The fixed version is here, although not fully tested.
Maybe that will fix your problem.

Thomas
Posted on 2001-09-11 01:36:23 by Thomas
That was exactly the problem! It has also helped clear a few other string searching peculiarities.

Thanks for your help (again) Thomas :alright:

...akenny
Posted on 2001-09-11 04:57:31 by akenny
Can someone tell me what they think of this string search algo! I posted it a while back. How can I optimise this?

Description: String search function for finding 'wrdf' in 'sentc', copying from 'sentc' + 'fromoffs' to 'copyto'. This function copies only 'copylen' amount of chars into 'copyto' or until it finds one of 5 chars from 'until' in 'sentc' whilst copying. It will then put a 0 byte at the end to terminate the string and the value of how many byte offsets from the beginning of 'sentc' it found the matching word is returned in eax. Otherwise it will return -1.

;==========

substrext proc STDCALL wrdf:dword, sentc:dword, copyto:dword, until:dword, fromoffs:dword, copylen:dword
mov ebx, sentc
cmp wrdf, NULL
je @@gofound
invoke lstrlen, wrdf
mov , eax
mov esi, wrdf
mov ebx, sentc
mov cl, ; wrdf[0] to cx
mov edi, 0
mov eax, 0
jmp @@lop

@@gofound:
mov , 0
mov , ebx
jmp @@foundmatch

@@lop:
mov dl, ; sentc to dx
cmp cl, dl ; compare wrdf[0] to sentc
je @@found
inc ebx
inc eax
cmp dl, 0 ; if at end of string
je @@done2
jmp @@lop

@@found:
mov , ebx
mov , eax
mov eax,
inc ebx
inc esi

@@lop2:
mov cl, byte ptr
mov dl, byte ptr
cmp cl, 0
je @@foundmatch
cmp cl, dl
jne @@nomatch
inc ebx
inc esi
jmp @@lop2

@@nomatch:
mov esi, wrdf
mov ebx,
mov eax,
inc ebx
inc eax
mov edi, 0
mov cl, ; move wrdf[0] back into cx
jmp @@lop

@@foundmatch:
mov ebx,
mov esi, 0
mov edi,
mov ecx,
mov edx,
cmp ecx, 0
je @@done
cmp edx, 0
je @@done

@@copychars:
inc esi
cmp esi, ecx
je @@appo
dec esi
mov al, byte ptr ; sentc to ax

@@compare:
push ebx
mov ebx, until
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2

@@cont:
mov ebx, edx
mov , al ; copy from sentc[?] to copyto[?]
cmp al, 0
je @@done

pop ebx
inc edi
inc esi
jmp @@copychars

@@appo:
pop ebx
mov al, 0
dec esi
mov , al
jmp @@done

@@appo2:
pop ebx
mov ebx, edx
mov al, 0
mov , al

@@done:
pop ebx
mov eax,
ret

@@done2:
mov eax, -1
ret

substrext endp
Posted on 2001-09-12 19:50:50 by nin