Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Miscellaneous snippets

Started by jj2007, August 20, 2017, 08:02:31 AM

Previous topic - Next topic


Sometimes I need to save a webpage but don't want to open it. So I copy the URL and run this proggie (source+exe attached):

include \masm32\MasmBasic\         ; download
  Let esi=Clip$()               ; get URL from clipboard, e.g.
  .if Instr_(esi, "://")
        FileWrite "SaveLinkAsTemp.html", Cat$(Chr$("<html><head><title>Temporary link</title></head>", 13, 10, "<body>", 60, "a href=", 34)+esi+Chr$(34, 62, "Right-click here and choose 'Save Link as...' to download ")+esi+Chr$(60, "/a", 62, "</body></html>"))
        ShEx "SaveLinkAsTemp.html"      ; launch the browser
        MsgBox 0, esi, "No URL found:", MB_OK


Attached a little file DownloadMasm32.exe that does something entirely harmless:
include \masm32\MasmBasic\
  Let esi="0.75=draob?php.xedni/draob/moc.23msam//:ptth"
  PrintLine esi
  Let edi=FileRead$(Mirror$(esi))
  Inkey NoTag$(edi)

In addition, the attachment contains a project that finds http:// and https:// in files, even if they are mirrored; just drag a file over SpyWareScanMB.exe 8)

Warning: While SpyWareScanMB.exe finds interesting http:// and https:// matches in exes and dlls, it is not a replacement for an AntiVirus. Malware writers use more sophisticated tricks to hide their URLs.


This snippet
- reads a text file into a RichEdit control (its name is supplied by the commandline)
- formats the first line in bold
- on request via the menu, prints the text.

To test it, extract all files to a temporary folder and drag the text file over the exe (the *.asc file is the source as RTF).

If needed, the whole process can be automated, i.e. eliminate the Event Menu step and just run the exe with the commandline argument, then exit automatically. See PrintRtf for printer dialog handling options.

GuiMenu equ @File, &Open, &Print, E&xit
GuiParas equ "Print formatted text demo", x100, y100, w400, h200; , cblack, b00FFFFD0h
include \masm32\MasmBasic\Res\MbGui.asm

  MakeFont hMyFont, Height:20, "Lucida Handwriting"
  GuiControl MyEdit, "RichEdit", font hMyFont
  SetGlobals f$, charfmt:CHARFORMAT2
  Let f$=FileRead$(CL$())               ; read the text file passed in the commandline
  SetWin$ hMyEdit=f$
  .if Instr_(f$, CrLf$)
       dec edx                         ; index of CrLf-1
       invoke SendMessage, hMyEdit, EM_SETSEL, 0, edx
       m2m charfmt.cbSize, CHARFORMAT2
       m2m charfmt.dwMask, CFM_BOLD            ; CharFtMask
       m2m charfmt.dwEffects, CFE_BOLD         ; Character effects
       invoke SendMessage, hMyEdit, EM_SETCHARFORMAT, SCF_SELECTION, ADDR charfmt
       invoke SendMessage, hMyEdit, EM_SETSEL, 0, 0

Event Menu
  .if MenuID==1
        void PrintRtf(hMyEdit, 1)     ; 0=use default printer, 1=ask for printer dialog


include \masm32\include\

txFile db "extracted.exe", 0
bytesWritten dd ?

  invoke FindResource, 0, 99, RT_RCDATA
  .if eax
xchg eax, ebx ; hRes
push rv(SizeofResource, 0, ebx)
push rv(LoadResource, 0, ebx) ; returns pointer to content in eax
invoke CreateFile, addr txFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0
xchg eax, esi
pop eax ; pointer to content
pop ecx ; size in bytes
invoke WriteFile, esi, eax, ecx, addr bytesWritten, 0
invoke CloseHandle, esi
invoke WinExec, addr txFile, SW_SHOW
end start


Equations in Assembly: SmplMath


Just encountered a case where I had to check if a file was older than dd.month.year, and needed a snippet to perform this check:

include \masm32\MasmBasic\         ; download
  .if Exist(CL$())
        sub esp, 2*FILETIME             ; create two structures on the stack
        movlps QWORD ptr [esp], TimeSF("02.07.2018 11:16:50")   ; European format!
        lea ecx, [esp+QWORD]
        movlps QWORD ptr [ecx], GfLastWrite(-1)         ; the lower QWORD returns the FILETIME
        invoke CompareFileTime, ecx, esp
        add esp, 2*FILETIME             ; correct the stack
        .if signed eax<0
                wMsgBox 0, wCL$(), "File is older. Process?", MB_YESNOCANCEL
                .if eax==IDYES
                                ; ... do stuff ...
                wMsgBox 0, wCL$(), "File is younger:", MB_OK
        wMsgBox 0, wCL$(), "No such file:", MB_OK


Create a hundred folders, write a testfile to each of them:

include \masm32\MasmBasic\         ; download
  For_ ecx=0 To 99
        Let esi=Str$("New Folder%i", ecx)
        MakeDir esi
        .Break .if !Zero?
        Print "+"
        Let esi=esi+"\test.txt"
        PrintLine Str$(ecx), Tb$, esi
        FileWrite esi, Str$("Test %i", ecx)


An edit control that allows to enter only valid decimal numbers. Bug feedback welcome :biggrin:

P.S.: PNG_LoadFile à la MasmBasic attached, using GuiImage. The *.asc source opens in WordPad, but here it is anyway for those who can't find WordPad or RichMasm:

include \masm32\MasmBasic\Res\MbGui.asm
Event Paint
GuiImage "car.png", fit


Quote from: jj2007 on July 14, 2018, 05:10:28 AM
An edit control that allows to enter only valid decimal numbers. Bug feedback welcome :biggrin:

oops!    :greensml: 

You did say 'decimal' numbers, not integers, right?

Also for the png image 'fit' viewer, when resizing from either side or from the top, the image resizes reflecting the new dimensions of the window.
But when resizing the window from the bottom, it crops or cuts off the image.


Quote from: zedd151 on July 19, 2018, 09:20:26 AMYou did say 'decimal' numbers, not integers, right?

Thanks for the feedback. Try 1234567890.123456784 vs 1234567890.123456785 - this is REAL10 precision, and it has its limits, i.e. around 18 digits.

        MovVal ST(0), Win$(@hWnd)
        SetWin$ hMyResult=Str$("Result=%Jf", ST(0)v)

If you replace the "J" with an "I", it displays one digit less, and all numbers will be correct.

Quotewhen resizing the window from the bottom, it crops or cuts off the image.

Interesting. It works correctly on this Win7-64 machine. Just made a test with my WinXP VM, and you are right: When resizing from left or top, it works (but with tremendous flicker - on Win7, it's smooth); when resizing from right or bottom, it crops. Hmmm :(


Windows XP Pro SP3  in VirtualBox <-- may have something to do with it?

The screen capture shows the top image resized from lower right upward

The bottom shows resized from top left downward.

Windows 7 32 bit works as advertised.

With the newer hardware that is out these days, it's a shame to have to run xp in a virtual machine.


Quote from: zedd151 on July 19, 2018, 10:34:20 AM
Windows XP Pro SP3  in VirtualBox <-- may have something to do with it?

I doubt it, VM and real machine will probably behave the same. Googling for "wm_sizing" "xp" yields lots of results, it could be a known problem.


Quote from: jj2007 on July 19, 2018, 10:52:06 AM

Googling for "wm_sizing" "xp" yields lots of results, it could be a known problem.

Only way to know for sure is to get someone that has XP installed natively on their system to test the program.



   Tested the ShowPng on Windows XP.  Seems to crop on resizing.
maximize and restore causes the image to fit the window.  Is
that what was wanted?




Quote from: FORTRANS on July 20, 2018, 01:12:44 AMon Windows XP.  Seems to crop on resizing.... Is that what was wanted?

No, not really, see reply #53+#54. It's limited to XP, though. Apparently one message is not being sent correctly in XP.