flat assembler
Message board for the users of flat assembler.

Index > Windows > proper way to sub class a MessageBox created by .....

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 9130
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
hi,
is there a proper way to subclass a MessageBox created by another application? (win32)

eg.
application A pops up a MessageBox with Yes / No , ask the user whether to delete this files or not.

what my application (application B) going to do is, if users click Yes, i want to save the file first before it gets deleted.

i have no trouble to find the handle for the pops up MessageBox by application A,
but i got trouble on how to get the user respond from the application A MessageBox.

any guide or tips for me to handle this problem?
Post 22 Mar 2007, 19:49
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9130
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
i just coded using the following method.
it works, but ... just i don't feel it is bug free....

make a dll
hook the WH_CBT

detect the HCBT_ACTIVATE event
lstrcmp the msgbox title
if title matched, use SetWindowLong subclass that msgbox

check for WM_COMMAND , BN_CLICKED shl 16 or 6
if yes, then use GetDlgItem,[hwnd],0xFFFF to get the handle then the text of the static control.
lstrcmp with the one we want.

then finally we know the user click Yes.
Post 23 Mar 2007, 01:45
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9130
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
hi,
just wish to know, is that safe to use SetWindowsHookEx on dllmain DLL_PROCESS_ATTACH and UnhookWindowsHookEx on DLL_PROCESS_DETACH ?
Post 23 Mar 2007, 18:09
View user's profile Send private message Reply with quote
hidden



Joined: 14 Feb 2007
Posts: 49
hidden
sleepsleep wrote:
hi,
just wish to know, is that safe to use SetWindowsHookEx on dllmain DLL_PROCESS_ATTACH and UnhookWindowsHookEx on DLL_PROCESS_DETACH ?
Then you have to check, who does attach this dll, because the program, that you hooked up will also attach this dll.

_________________
Image Lang: (eng|рус)
Post 23 Mar 2007, 20:18
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9130
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
thank hidden,
i end up using a cbtHookCounter, whenever a DLL_PROCESS_ATTACH detected, it adds 1. If it is 0, it would SetWindowsHookEx, so counter starts from -1 Smile

if DLL_PROCESS_DETACH found, and counter shows 0, it would call UnhookWindowsHookEx.

something like below
Code:
proc dllmain hinstDLL, fdwReason, lpvReserved
                cmp     [fdwReason],DLL_PROCESS_ATTACH
                je      .pattach
                cmp     [fdwReason],DLL_PROCESS_DETACH
                je      .pdettach
                ret
        
        .pdettach:
                dec     [cbtHookCounter]
                cmp     [cbtHookCounter],0
                jne     .exit
        invoke  OutputDebugString,r1
        invoke  UnhookWindowsHookEx,[cbtHookH]
                ret
        
        .pattach:
                push    [hinstDLL]
                pop     [dllH]

                inc     [cbtHookCounter]
                cmp     [cbtHookCounter],0
                jne     .exit
        invoke  OutputDebugString,r2
        invoke  SetWindowsHookEx,WH_CBT,cbtHookProc,[hinstDLL],0
                mov     [cbtHookH],eax
        .exit:
                mov     eax,TRUE
                ret
endp
    
Post 24 Mar 2007, 03:34
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Both "cmp [cbtHookCounter],0" can be removed without loosing current functionality.
Post 24 Mar 2007, 03:47
View user's profile Send private message Reply with quote
hidden



Joined: 14 Feb 2007
Posts: 49
hidden
Where do you store cbtHookCounter? Do you know, that if you're hooking another process, your module will be loaded second time into that prosess' addressing apace and this variable won't be available?

Why don't you just do like shown in MSDN example? http://msdn2.microsoft.com/en-us/library/ms644960.aspx
It's much more easy.

Or if you don't want application initialize hooking, check which application loaded this module by GetModuleFileName

PS You're using system debugger, right?
OutputDebugString at MSDN wrote:
If the application has no debugger, the system debugger displays the string. If the application has no debugger and the system debugger is not active, OutputDebugString does nothing.
Post 24 Mar 2007, 04:24
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9130
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
yo yo LocoDelAssembly & hidden
i store the cbtHookCounter on .sdata shareable readable writeable section.

this dll is actually coded to fix a missing feature in another software.
and i don't know what is that application name too, the sort of guide line given by my friend or client Razz is, i should write the missing feature whenever i detect a pops up of this message box and when user clicks yes.

wat i got is, the messagebox title and its static control text... thats all Very Happy .
yeap, i am using the debugview to dump the debug string :p
http://www.microsoft.com/technet/sysinternals/utilities/debugview.mspx

well, i did monitored some debug message sent by another application Smile Smile
Post 24 Mar 2007, 04:50
View user's profile Send private message Reply with quote
hidden



Joined: 14 Feb 2007
Posts: 49
hidden
But you know name of your program, and you need to set up hook only if your program attach this dll otherwise do nothing.

Quote:
i store the cbtHookCounter on .sdata shareable readable writeable section.
It's available only in your process, but in another process it's again has default value. You can store it as "named even"(CreateEvent) for example, but check filename, I think, easily.
Post 24 Mar 2007, 05:31
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9130
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
okie, the GetModuleFileName seems like a more easier way to deal things.

but it returns eg. "C:\somefolder\desktop\myapp.exe" and etc that come prefixed with "C:\watever\watever\thenonly\theapp.exe"

is there an easy to get only the application name? no need path and drive prefix?
Post 24 Mar 2007, 15:31
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9130
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
while, code an ugly way of doing things
Code:
        .processdetach:
        invoke  GetModuleHandle,NULL
        invoke  GetModuleFileName,eax,modBuff,MAX_PATH
                cmp     eax,0
                je      @f
                add     eax,modBuff
        
        .repeatcheck:
                dec     eax
                cmp     byte [eax],"\"
                jne     .repeatcheck
        invoke  OutputDebugString,eax
    
Post 24 Mar 2007, 15:52
View user's profile Send private message Reply with quote
hidden



Joined: 14 Feb 2007
Posts: 49
hidden
Actually, I don't think it's an ugly way, you can also compare in the same time and stop comparing when you found "\"

Also you can use StrRChr to get "\myapp.exe" from "C:\somefolder\desktop\myapp.exe" and then compare them by StrCmpI, but for this you need "shlwapi.dll" and another buffer for the filename, but I wouldn't use them.
Post 25 Mar 2007, 03:31
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.