News:

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

Typecasting ?!

Started by Slugsnack, June 07, 2009, 03:19:07 AM

Previous topic - Next topic

Slugsnack

includelib \masm32\lib\shlwapi.lib
includelib \masm32\lib\psapi.lib
include \masm32\include\masm32rt.inc
include \masm32\include\shlwapi.inc
include \masm32\include\psapi.inc

EnumFunc proto :DWORD, :DWORD

WINDOWENTRY32 STRUCT
    lpszClassname     DWORD   ?
    lpszFilename      DWORD   ?
WINDOWENTRY32 ENDS

.data

szClassname                 byte            "IEFrame", 0
szFilename                  byte            "iexplore.exe", 0

.data?

we                          WINDOWENTRY32   <>
szCurrentFilename           byte 255 dup    (?)
szCurrentClassname          byte 255 dup    (?)

.code
    Start:

xor ebx, ebx

mov we.lpszClassname, offset szClassname
mov we.lpszFilename, offset szFilename

    invoke EnumWindows, addr EnumFunc, addr we
    invoke ExitProcess, ebx

EnumFunc proc hwnd:DWORD, lParam:LPARAM
LOCAL dwProcessId:DWORD
LOCAL hProcess:DWORD

    invoke GetClassName, hwnd, addr szCurrentClassname, 255
    invoke StrCmpC, addr szCurrentClassname, we.lpszClassname

    .IF eax == 0

            invoke GetWindowThreadProcessId, hwnd, addr dwProcessId
            invoke OpenProcess, PROCESS_ALL_ACCESS, FALSE, dwProcessId
        mov hProcess, eax

            invoke GetModuleFileNameEx, eax, 0, addr szCurrentFilename, 255
            invoke PathStripPath, addr szCurrentFilename
            invoke StrCmpC, we.lpszFilename, addr szCurrentFilename

            .IF eax == 0

                    invoke PostMessage, hwnd, WM_CLOSE, 0, 0

            .ENDIF

            invoke CloseHandle, hProcess

    .ENDIF

mov eax, 1

ret
EnumFunc endp

    end Start


This code conveniently closes all my IE windows at the end of the day.  However I think it is better practice not to access global variables from local functions.  Since I want two pointers passed into the EnumFunc, I created a structure that holds these two.  However in EnumFunc when I replace 'we' with 'lParam' it gets errors saying lpszFilename and lpszClassname don't exist..  I am guessing somehow lParam needs to be typecasted to WINDOWENTRY32 ?

sinsi

Easiest way -

  mov esi,lParam
  invoke StrCmpC, addr szCurrentClassname, (WINDOWENTRY32 PTR [esi]).lpszClassname

or you could use ASSUME

  mov esi,lParam
  ASSUME ESI:PTR WINDOWENTRY32
  invoke StrCmpC, addr szCurrentClassname, [esi].lpszClassname
  ...
  ret
  ASSUME ESI:NOTHING

Light travels faster than sound, that's why some people seem bright until you hear them.

Slugsnack

omg i had just figured out the first method after a bit of searching.  works perfect now.

includelib \masm32\lib\shlwapi.lib
includelib \masm32\lib\psapi.lib
include \masm32\include\masm32rt.inc
include \masm32\include\shlwapi.inc
include \masm32\include\psapi.inc

EnumFunc proto :DWORD, :DWORD

WINDOWENTRY32 STRUCT
    lpszClassname     DWORD   ?
    lpszFilename      DWORD   ?
WINDOWENTRY32 ENDS

.data

szClassname                 byte            "IEFrame", 0
szFilename                  byte            "iexplore.exe", 0

.data?

we                          WINDOWENTRY32   <>
szCurrentFilename           byte 255 dup    (?)
szCurrentClassname          byte 255 dup    (?)

.code
    Start:

xor ebx, ebx

mov we.lpszClassname, offset szClassname
mov we.lpszFilename, offset szFilename

    invoke EnumWindows, addr EnumFunc, addr we
    invoke ExitProcess, ebx

EnumFunc proc hwnd:DWORD, lParam:LPARAM
LOCAL dwProcessId:DWORD
LOCAL hProcess:DWORD

push ebx
mov ebx, lParam
    invoke GetClassName, hwnd, addr szCurrentClassname, 255
    invoke StrCmpC, addr szCurrentClassname, (WINDOWENTRY32 PTR [ebx]).lpszClassname

    .IF eax == 0

            invoke GetWindowThreadProcessId, hwnd, addr dwProcessId
            invoke OpenProcess, PROCESS_ALL_ACCESS, FALSE, dwProcessId
        mov hProcess, eax

            invoke GetModuleFileNameEx, eax, 0, addr szCurrentFilename, 255
            invoke PathStripPath, addr szCurrentFilename
            invoke StrCmpC, (WINDOWENTRY32 PTR [ebx]).lpszFilename, addr szCurrentFilename

            .IF eax == 0

                    invoke PostMessage, hwnd, WM_CLOSE, 0, 0

            .ENDIF

            invoke CloseHandle, hProcess

    .ENDIF

mov eax, 1
pop ebx

ret
EnumFunc endp

    end Start


thanks for telling me about second method though ; )