News:

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

VBScript RegExp COM Example

Started by ecube, July 29, 2010, 02:35:42 AM

Previous topic - Next topic

ecube

I haven't had a chance to test this as i'm still waiting on a working version of stringfunctions.lib from minor28, but is simple examples of using vbscripts neat regexp features to find patterns and replace strings within strings. Thanks for the neat Automation.lib Minor28!

.686
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\oleaut32.inc
include \masm32\include\Ole32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\oleaut32.lib
includelib \masm32\lib\Ole32.lib

includelib Automation.lib
include Automation.inc
includelib stringfunctions.lib
include stringfunctions.inc

RegMatch proto :DWORD,:DWORD,:DWORD
RegReplace proto :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
BStrP proto :DWORD
     CTEXT MACRO text:VARARG
            LOCAL TxtName
              .data
               TxtName BYTE text,0
              .code
            EXITM <ADDR TxtName>
     ENDM
     
.data
mypattern db "\d{5}"
mysearchstr db "hah12345"
searchstr db "green one green two green three",0
.data?
ppIRegExp2 dd ?
ppRegExp dd ?
resultsbuff db 256 dup(?)
.code
start:
invoke CoInitializeEx,0,0
invoke RegMatch,CTEXT("5"),FALSE,CTEXT("searchstringg352")
.if eax==1
invoke MessageBox,0,CTEXT("Found Pattern!"),CTEXT("Found!"),MB_ICONINFORMATION
.else
invoke MessageBox,0,CTEXT("Did Not Find Pattern!"),CTEXT("Did not Find!"),MB_ICONINFORMATION
.endif

invoke RegReplace,CTEXT("green"),TRUE,TRUE,addr searchstr,CTEXT("blue"),addr resultsbuff
invoke MessageBox,0,addr resultsbuff,addr searchstr,MB_ICONINFORMATION

invoke ExitProcess,0
RegMatch proc iPattern,IgnoreCase,iStr
LOCAL var1:VARIANT
LOCAL var2:VARIANT
LOCAL ReturnValue:dword
invoke Create_Object,WSTR("VBScript.RegExp"),offset ppRegExp

invoke Set_Interface,ppRegExp,WSTR("{3F4DACB0-160D-11D2-A8E9-00104B365C9F}"),1,offset ppIRegExp2

.if IgnoreCase==TRUE
invoke RunMethod,ppIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,0,VARIANT_TRUE
.else
invoke RunMethod,ppIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,0,VARIANT_FALSE
.endif

invoke VariantInit,addr var1
mov var1.vt,VT_BSTR
invoke BStrP,iPattern
mov var1.bstrVal,eax

invoke VariantInit,addr var2
mov var2.vt,VT_BSTR
invoke BStrP,iStr
mov var2.bstrVal,eax

invoke RunMethod,ppIRegExp2,WSTR("Pattern"),DISPATCH_PROPERTYPUT,NULL,1,addr var1
invoke RunMethod,ppIRegExp2,WSTR("Test"),DISPATCH_METHOD,addr ReturnValue,1,addr var2

invoke Release_Object,ppIRegExp2
invoke Release_Object,ppRegExp
mov eax,ReturnValue
ret
RegMatch endp

RegReplace proc iPattern,IgnoreCase,iReplaceAll,iStr,iReplacetext,oBuf
LOCAL var1:VARIANT
LOCAL var2:VARIANT
LOCAL var3:VARIANT
LOCAL ReturnValue:dword
invoke Create_Object,WSTR("VBScript.RegExp"),offset ppRegExp

invoke Set_Interface,ppRegExp,WSTR("{3F4DACB0-160D-11D2-A8E9-00104B365C9F}"),1,offset ppIRegExp2

.if IgnoreCase==TRUE
invoke RunMethod,ppIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,0,VARIANT_TRUE
.else
invoke RunMethod,ppIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,0,VARIANT_FALSE
.endif

.if iReplaceAll==TRUE
invoke RunMethod,ppIRegExp2,WSTR("Global"),DISPATCH_PROPERTYPUT,NULL,1,VARIANT_TRUE
.else
invoke RunMethod,ppIRegExp2,WSTR("Global"),DISPATCH_PROPERTYPUT,NULL,1,VARIANT_FALSE
.endif

invoke VariantInit,addr var1
mov var1.vt,VT_BSTR
invoke BStrP,iPattern
mov var1.bstrVal,eax

invoke VariantInit,addr var2
mov var2.vt,VT_BSTR
invoke BStrP,iStr
mov var2.bstrVal,eax

invoke VariantInit,addr var3
mov var3.vt,VT_BSTR
invoke BStrP,iReplacetext
mov var3.bstrVal,eax

invoke RunMethod,ppIRegExp2,WSTR("Pattern"),DISPATCH_PROPERTYPUT,NULL,1,addr var1
invoke RunMethod,ppIRegExp2,WSTR("Replace"),DISPATCH_METHOD,oBuf,2,addr var2,addr var3

invoke Release_Object,ppIRegExp2
invoke Release_Object,ppRegExp
ret
RegReplace endp

BStrP proc iData
LOCAL buffer[256]:byte
LOCAL pOlechar:dword
invoke RtlZeroMemory,addr buffer,256
invoke MultiByteToWideChar,CP_ACP,0,iData,-1,addr buffer,sizeof buffer
invoke SysAllocString,addr buffer
ret
BStrP endp
End start

ecube

Ok minor28 was kind enough to post the needed lib on his site http://www.bostream.nu/minor28/ so I managed to assemble the above, the regmatch works fine but the RegReplace doesn't atm, so i'll have to play more with it.

petezl

Just thinking, Is the dll using the vb60.dll ?
If so then if its accessing the registry through pure vb then it can't write/change the registry outside its own environment settings, to do that it needs to use the win32 api

sorry if I miss-understood what you are trying to do!
Cats and women do as they please
Dogs and men should realise it.

ecube

is just using vbscript.dll I believe. And it's not interacting with the registry? heh, just regular expressions for the ultimate in pattern matching/data manipulating.

askm

Indeed...

Any more forward steps E^Cube ?

ToutEnMasm


Quote
simple examples of using vbscripts neat regexp features
Not so simple,there is as much writing to do than with masm .
With only one proc,the same thing can be done without use of the dll.

ToutEnMasm


askm

; The following exits (0) but if the 'myreplace' is long enough it exits (-1073741819) ?
; I am not an expert, so please correct any code.
; Thanks to all
; askm

include \masm32\include\masm32rt.inc
include \masm32\include\advapi32.inc
include \masm32\include\Automation.inc
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\Automation.lib
RegTest proto :DWORD,:DWORD,:DWORD
RegReplace proto :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
BStrP proto :DWORD
CTEXT MACRO text:VARARG
LOCAL TxtName
.data
TxtName BYTE text,0
.code
EXITM <ADDR TxtName>
ENDM
.data
mypattern db "\d",0
mysearchstr db "12345",0

; no error
myreplace db "$1$2$1$2",0
; error
;myreplace db "$1$2$1$2$1$2$1$2",0

TestString db "<>Green one Green two Green Green three</>",0
.data?
ppIRegExp2 dd ?
ppRegExp dd ?
pqIRegExp2 dd ?
pqRegExp dd ?
rbW dd ?
.code
start:
invoke CoInitializeEx,0,0
invoke RegTest,CTEXT("S;trIngG3"),TRUE,CTEXT("s.earchs;tRiNgG352m")
.if eax==1
invoke MessageBox,0,CTEXT("Found Pattern !"),CTEXT("Found !"),MB_ICONINFORMATION
.else
invoke MessageBox,0,CTEXT("Did Not Find Pattern !"),CTEXT("Did not Find !"),MB_ICONINFORMATION
.endif
invoke RegReplace,CTEXT("(gr)(een)"),TRUE,TRUE,addr TestString,CTEXT("blueyellow"), addr rbW
invoke ExitProcess,0
RegTest proc iPattern,IgnoreCase,iString
LOCAL varPattern:VARIANT
LOCAL varString:VARIANT
LOCAL variantIgnoreCase :VARIANT
LOCAL ReturnValue:dword
invoke VariantInit,addr varPattern
mov varPattern.vt,VT_BSTR
invoke BStrP,iPattern
mov varPattern.bstrVal,eax
invoke VariantInit,addr varString
mov varString.vt,VT_BSTR
invoke BStrP,iString
mov varString.bstrVal,eax
invoke Create_Object,WSTR("VBScript.RegExp"),offset ppRegExp
invoke Set_Interface,ppRegExp,WSTR("{3F4DACB0-160D-11D2-A8E9-00104B365C9F}"),1,offset ppIRegExp2
invoke VariantInit,addr variantIgnoreCase
mov variantIgnoreCase.vt,VT_BOOL
.if IgnoreCase==TRUE
mov variantIgnoreCase.boolVal,VARIANT_TRUE
invoke RunMethod,ppIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,1, addr variantIgnoreCase
.elseif IgnoreCase==FALSE
mov variantIgnoreCase.boolVal,VARIANT_FALSE
invoke RunMethod,ppIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,1, addr variantIgnoreCase
.endif
invoke RunMethod,ppIRegExp2,WSTR("Pattern"),DISPATCH_PROPERTYPUT,NULL,1,addr varPattern
invoke RunMethod,ppIRegExp2,WSTR("Test"),DISPATCH_METHOD,addr ReturnValue,1,addr varString
invoke Release_Object,ppIRegExp2
invoke Release_Object,ppRegExp
mov eax,ReturnValue
ret
RegTest endp
RegReplace proc Pattern,IgnoreCase,ReplaceAll,String,Replace,ptrBuffer
LOCAL varReplace    :VARIANT
LOCAL varString  :VARIANT
LOCAL varPattern :VARIANT
LOCAL varoBuf   :VARIANT
LOCAL variantIgnoreCase :VARIANT
LOCAL variantGlobal :VARIANT
LOCAL ReturnValue   :dword
LOCAL buffW :dword
LOCAL buff2W :dword
LOCAL lenB :dword
mov buffW, alloc(1024)
mov buff2W, alloc(1024)
invoke Create_Object,WSTR("VBScript.RegExp"),offset pqRegExp
invoke Set_Interface,pqRegExp,WSTR("{3F4DACB0-160D-11D2-A8E9-00104B365C9F}"),1,offset pqIRegExp2
invoke VariantInit,addr varReplace
mov varReplace.vt,VT_BSTR
invoke BStrP,Replace
mov varReplace.bstrVal,eax
invoke VariantInit,addr varString
mov varString.vt,VT_BSTR
invoke BStrP, String
mov varString.bstrVal,eax
invoke VariantInit,addr varPattern
mov varPattern.vt,VT_BSTR
invoke BStrP,Pattern
mov varPattern.bstrVal,eax
invoke VariantInit,addr varoBuf
mov varoBuf.vt,VT_BSTR
invoke BStrP, ptrBuffer
mov varoBuf.bstrVal,eax
invoke RunMethod,pqIRegExp2,WSTR("Pattern"),DISPATCH_PROPERTYPUT,NULL,1,addr varPattern
invoke VariantInit,addr variantGlobal
mov variantGlobal.vt,VT_BOOL
invoke VariantInit,addr variantIgnoreCase
mov variantIgnoreCase.vt,VT_BOOL
.if ReplaceAll==TRUE
mov variantGlobal.boolVal,VARIANT_TRUE
invoke RunMethod,pqIRegExp2,WSTR("Global"),DISPATCH_PROPERTYPUT,NULL,1, addr variantGlobal
.elseif ReplaceAll==FALSE
mov variantGlobal.boolVal,VARIANT_FALSE
invoke RunMethod,pqIRegExp2,WSTR("Global"),DISPATCH_PROPERTYPUT,NULL,1,addr variantGlobal
.endif
.if IgnoreCase==TRUE
mov variantIgnoreCase.boolVal,VARIANT_TRUE
invoke RunMethod,pqIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,1,addr variantIgnoreCase
.elseif IgnoreCase==FALSE
mov variantIgnoreCase.boolVal,VARIANT_FALSE
invoke RunMethod,pqIRegExp2,WSTR("IgnoreCase"),DISPATCH_PROPERTYPUT,NULL,1,addr variantIgnoreCase
.endif
invoke RunMethod,pqIRegExp2,WSTR("Replace"),DISPATCH_METHOD,addr varoBuf,2,addr varString,addr varReplace
invoke Release_Object,pqIRegExp2
invoke Release_Object,pqRegExp
invoke szLen, addr varoBuf
inc eax
mov lenB, eax
invoke MultiByteToWideChar,CP_ACP,MB_PRECOMPOSED,addr varoBuf,-1,buff2W,lenB
invoke MessageBoxW, 0, buff2W, buff2W, MB_OK
free buffW
free buff2W
ret
RegReplace endp
BStrP proc iData
;align 4
LOCAL buffer[512]:byte
LOCAL pOlechar:dword
LOCAL iData_size:dword
invoke RtlZeroMemory,addr buffer,512
invoke MultiByteToWideChar,CP_ACP,0,iData,-1,NULL,0
mov iData_size, eax
invoke MultiByteToWideChar,CP_ACP,0,iData,-1,addr buffer,iData_size
invoke SysAllocString,addr buffer
ret
BStrP endp
End start


askm

Clarification

Line before ExitProcess should read:

invoke RegReplace,CTEXT("(gr)(een)"),TRUE,TRUE,addr TestString,addr myreplace, addr rbW

which refers to 'myreplace', which has the replace pattern of a certain safety length.

The rbW does little. VARIANTS are not fully understood.

Again corrections welcome.

askm




askm

Problem with a return value or regexp reordering issue in a multi allocator environment ?

include \masm32\include\masm32rt.inc   ;check

The message box will work   ;check

It's going to have an effect on write combining buffers, caching, memory fencing and synchronization issue   ;check

Thanks but this is causing topic buffer over run

askm

Possible solution to these examples coming soon. The reign of this problem has been too long.
askm

askm

;Try this

.686
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\advapi32.inc
include \masm32\include\Automation.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\include\oleaut32.inc
include \masm32\include\ole32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\Automation.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\oleaut32.lib
includelib \masm32\lib\ole32.lib
includelib \masm32\lib\user32.lib
RegTest proto :DWORD, :DWORD, :DWORD
RegReplace proto :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
BStrP proto :DWORD
CTEXT MACRO text:VARARG
    LOCAL TxtName
    .data
    TxtName BYTE text, 0
    .code
    EXITM <ADDR TxtName>
ENDM
.data
mypattern db "\d"
mysearchstr db "12345"
vTRUE   dd 0FFFFh
vFALSE  dd 00000h
TestString db "Tracing the execution",13,10,"of a jmp",13,10,"instruction.", 0,0
nFND db "Not Found !", 0
yFND db "Found !", 0
FND LABEL word
dd nFND
dd yFND
.data?
rtRegExp dd ?
rtIRegExp2 dd ?
rrRegExp dd ?
rrIRegExp2 dd ?
.code
start:
invoke CoInitializeEx, 0, 0
invoke RegTest, vFALSE, CTEXT("try"), CTEXT("s.earchs;tRyNgG352m")
mov ecx, offset FND
mov eax, [ecx][eax*4]
invoke MessageBox, 0, eax, CTEXT("Pattern Found ?"), MB_ICONINFORMATION
invoke RegReplace, vFALSE, vFALSE, addr TestString, CTEXT("jmp"), CTEXT("ExitProcess")
invoke CoUninitialize
invoke ExitProcess, 0

RegTest proc IgnoreCase, iPattern, iString
LOCAL varPattern:VARIANT
LOCAL varString:VARIANT
LOCAL varIgnoreCase :VARIANT
LOCAL ReturnValue:dword
invoke VariantInit, addr varPattern
mov varPattern.vt, VT_BSTR
invoke BStrP, iPattern
mov varPattern.bstrVal, eax
invoke VariantInit, addr varString
mov varString.vt, VT_BSTR
invoke BStrP, iString
mov varString.bstrVal, eax
invoke Create_Object, WSTR("VBScript.RegExp"), offset rtRegExp
invoke Set_Interface, rtRegExp, WSTR("{3F4DACB0-160D-11D2-A8E9-00104B365C9F}"), 1, offset rtIRegExp2
invoke VariantInit, addr varIgnoreCase
mov varIgnoreCase.vt, VT_BOOL
mov eax, IgnoreCase
mov varIgnoreCase.boolVal, ax
invoke RunMethod, rtIRegExp2, WSTR("IgnoreCase"), DISPATCH_PROPERTYPUT, NULL, 1, addr varIgnoreCase
invoke RunMethod, rtIRegExp2, WSTR("Pattern"), DISPATCH_PROPERTYPUT, NULL, 1, addr varPattern
invoke RunMethod, rtIRegExp2, WSTR("Test"), DISPATCH_METHOD, addr ReturnValue, 1, addr varString
invoke Release_Object, rtIRegExp2
invoke Release_Object, rtRegExp
mov eax, ReturnValue
ret
RegTest endp
RegReplace proc IgnoreCase, ReplaceAll, String, Pattern, Replace
LOCAL varReplace    :VARIANT
LOCAL varString  :VARIANT
LOCAL varPattern :VARIANT
LOCAL varBuffer   :VARIANT
LOCAL varIgnoreCase :VARIANT
LOCAL varGlobal :VARIANT
LOCAL addrbstrVal :dword
invoke VariantInit, addr varPattern
mov varPattern.vt, VT_BSTR
invoke BStrP, Pattern
mov varPattern.bstrVal, eax
invoke Create_Object, WSTR("VBScript.RegExp"), offset rrRegExp
invoke Set_Interface, rrRegExp, WSTR("{3F4DACB0-160D-11D2-A8E9-00104B365C9F}"), 1, offset rrIRegExp2
invoke RunMethod, rrIRegExp2, WSTR("Pattern"), DISPATCH_PROPERTYPUT, NULL, 1, addr varPattern
invoke VariantInit, addr varGlobal
mov varGlobal.vt, VT_BOOL
mov eax, ReplaceAll
mov varGlobal.boolVal, ax
invoke RunMethod, rrIRegExp2, WSTR("Global"), DISPATCH_PROPERTYPUT, NULL, 1, addr varGlobal
invoke VariantInit, addr varIgnoreCase
mov varIgnoreCase.vt, VT_BOOL
mov eax, IgnoreCase
mov varIgnoreCase.boolVal, ax
invoke RunMethod, rrIRegExp2, WSTR("IgnoreCase"), DISPATCH_PROPERTYPUT, NULL, 1, addr varIgnoreCase
invoke VariantInit, addr varString
mov varString.vt, VT_BSTR
invoke BStrP, String
mov varString.bstrVal, eax
invoke VariantInit, addr varReplace
mov varReplace.vt, VT_BSTR
invoke BStrP, Replace
mov varReplace.bstrVal, eax
invoke VariantInit, addr varBuffer
mov varBuffer.vt, VT_BSTR
mov varBuffer.bstrVal, BSTR("varBuffer")
invoke RunMethod2, rrIRegExp2, WSTR("Replace"), DISPATCH_METHOD, addr varBuffer, 2, addr varString, addr varReplace
mov eax, [varBuffer.bstrVal]
mov addrbstrVal, eax
invoke MessageBoxW, 0, addrbstrVal, WSTR("The Replaced text is..."), MB_OK
invoke Release_Object, rrIRegExp2
invoke Release_Object, rrRegExp
invoke SysFreeString,varPattern.bstrVal
invoke SysFreeString,varString.bstrVal
invoke SysFreeString,varReplace.bstrVal
invoke SysFreeString,varBuffer.bstrVal
ret
RegReplace endp
BStrP proc iData
LOCAL buffer[512]:byte
LOCAL pOlechar:dword
invoke RtlZeroMemory, addr buffer, 512
invoke MultiByteToWideChar, CP_ACP, 0, iData, -1, addr buffer, 496
invoke SysAllocString, addr buffer
ret
BStrP endp
End start