News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Coding an Exportable SearAndReplace procedure

Started by ttaylor, January 14, 2009, 05:31:44 AM

Previous topic - Next topic

ttaylor

Hello world, I needed a function to search and replace text so I jumped right in and decided to get started. I am very new to writing assembler. I have been trying Avidly to learn but something happened yesterday and when I woke up, everything seemed easier so here is what I came up with.

*Edit -> The code has been updated. It still does not work... Hutch! How can I use OLE memory to return the modified text?


FindAndReplace proc lpSzFind:LPSTR, lpSzReplace:LPSTR, lpSzText:LPSTR

;Setup locals
LOCAL lpSzBuffer:DWORD
LOCAL szBuffer:BYTE
LOCAL sourceLen:DWORD
LOCAL lpText:DWORD
LOCAL findLen:DWORD
LOCAL replaceLen
LOCAL findCount:DWORD

;Derefrence lpSzText because it is passed by reference
mov eax, lpSzText ; copy address into eax
mov eax, [eax] ; dereference it
mov lpText, eax

;Clear eax for sanity: else if I die you might think I'm crazy :D
xor eax, eax

;Count occurances of lpSzFind in lpSzText
mov findCount, rv(szWcnt, lpText, lpSzFind)

;If we need to replace something
.IF findCount > 0
;execute these statements ...

;Store buffer address
mov lpSzBuffer, ptr$(szBuffer)

;Store source length
mov sourceLen, len(lpText)

;Add some padding to the source length
add sourceLen, 16384

;Store find length
mov findLen, len(lpSzFind)

;Store replace length
mov replaceLen, len(lpSzReplace)

mov eax, replaceLen ;Copy replaceLen for further operation

.IF findLen >= eax
;The source length is large enough

nop

.else
;The buffer length needs to be extended

sub eax, findLen ;Subtract findLen from replaceLen copy
invoke IntMul, findCount, eax ;Multiply the difference
add sourceLen, eax ;Add the extra bytes to the source length

.endif

xor eax, eax ;Clear eax because we have done nothing yet

mov lpSzBuffer, alloc$(sourceLen) ;Allocate buffer space

; perform replacement
invoke szRep, lpText, lpSzBuffer, lpSzFind, lpSzReplace

;Taking a shot in the dark here I am guessing my lpSzText buffer
;Won't be large enough. So we will allocate the new source length

mov lpText, alloc(sourceLen) ;Using the fixed length allocation macro

;invoke szCopy, lpSzBuffer, lpText ;Return the new text
invoke RtlMoveMemory, lpText, lpSzBuffer, sourceLen
free$ lpSzBuffer ;Deallocate buffer

.endif

;Return 0 if nothing to do else return what we did.
mov eax, findCount

ret

FindAndReplace endp


I wrote this proc using Hutch's documentation on szRep in the MASM32 Library Ref.

I'm not sure if it really works yet. I am seeking a professional/advanced opinion on that. Attached you will find the RadASM DLL project with all binaries removed.  :U

*Edit -> I worked up the nerve to try it using Visual Basic 6 No errors! But the parameters are mangled. They have all been passed ByRef.

*Edit -> passed by ref.. NO WONDER! Now we pass only the text to search (lpSzText) and modify by ref.


ttaylor

#1
Well... Seems I forgot to ATTACH!  ::)

*Edit -> The project has been updated. Visual Basic 6.0 Binary and project files included. You must compile the dll. It does not replace text for an external application as of yet!

Developed with RadASM I believe this project is about 95% complete. Can somebody please post information on how to return the modified string without APIs "cleaning" up after my handy work?

Well, well, well, what do we have here? http://msdn.microsoft.com/en-us/library/aa263531(VS.60).aspx

:dazzled: Well, it appears I have been beaten thoroughly with a large stick. A man has to know his limits. I know I've met mine. I shall return after retirement. January 14, 2009 9:34 AM -6 GMT

[attachment deleted by admin]