- Код: Выделить всё
#COMPILE EXE
#DIM ALL
FUNCTION DumpCode2File (BYVAL code_ptr AS BYTE PTR, file2dump AS STRING) AS LONG
DIM i AS LOCAL LONG, _
j AS LOCAL LONG, _
f AS LOCAL INTEGER, _
buff AS LOCAL STRING
i = 0
j = 0
DO
' small "heuristic" RET analyzer :-)
IF j = 0 AND _
@code_ptr[i] = &h5F AND _ ' POP EDI
@code_ptr[i+1] = &h5E AND _ ' POP ESI
@code_ptr[i+2] = &h5B AND _ ' POP EBX
@code_ptr[i+3] = &h5D THEN ' POP EBP
IF @code_ptr[i+4] = &hC2 THEN ' RET with parameter(s)
j = i + 6
ELSEIF @code_ptr[i+4] = &hC3 THEN ' RET without parameter(s)
j = i + 4
END IF
END IF
' bound checking
IF i >=2^15& THEN
MSGBOX "Combination of POP and RET instructions not found." & $CRLF & _
"Probably code is too long or invalid address code"
FUNCTION = 0
EXIT LOOP
END IF
buff = buff & CHR$( @code_ptr[i] )
INCR i
LOOP UNTIL i>j AND j>0
IF LEN(DIR$(file2dump))>0 THEN KILL file2dump ' prevent overwriting of old files..
f = FREEFILE
OPEN file2dump FOR BINARY AS f
PUT$ #f, buff
CLOSE f
FUNCTION = i ' returns number of bytes written to file
' ie. actual length of code
END FUNCTION
' some test functions
FUNCTION myFunc1 () AS LONG
LOCAL a%,b%,c%
a% = 10: b% = 20
MSGBOX "Hello from myFunc1"
c% = (a% + b%) * 2
FUNCTION = c%
END FUNCTION
'
FUNCTION myFunc2 (BYVAL filename AS STRING) AS LONG
LOCAL f%, l&
f% = FREEFILE
MSGBOX "next free file handle number is :" & STR$(f%)
FUNCTION = 0
IF LEN(DIR$(filename)) > 0 THEN
OPEN filename FOR BINARY AS f%
l& = LOF(f%)
CLOSE f%
FUNCTION = l&
END IF
END FUNCTION
'
FUNCTION myFunc3 (BYVAL p1%,BYVAL p2&, p3!) AS STRING
LOCAL t$
t$="myFunc3 with 3 parameters"
MSGBOX t$
END FUNCTION
FUNCTION PBMAIN
pbmain_label:
DumpCode2File (CODEPTR(myFunc1), "df1.bin")
DumpCode2File (CODEPTR(myFunc2), "df2.bin")
DumpCode2File (CODEPTR(myFunc3), "df3.bin")
DumpCode2File (CODEPTR(pbmain_label), "pbmlbl.bin")
DumpCode2File (CODEPTR(PBMAIN) , "pbmain.bin")
MSGBOX "Binary dump files has been created."
END FUNCTION