flat assembler
Message board for the users of flat assembler.

Index > Windows > [solved]Handle to window of exist instance.

Author
Thread Post new topic Reply to topic
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 05 Sep 2020, 17:56
Hi
I need to get a handle of already exist window to give to it focus when I try to start second instance of my program. What is a shortest way to do that? Can I use semaphore's handle somehow?


Last edited by Overclick on 08 Sep 2020, 17:40; edited 1 time in total
Post 05 Sep 2020, 17:56
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20459
Location: In your JS exploiting you and your system
revolution 06 Sep 2020, 07:57
  1. Use a Mutex to detect a running instance.
  2. Send a message to the existing instance from the new instance.
  3. Close the new instance.
Post 06 Sep 2020, 07:57
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 06 Sep 2020, 17:37
Nice idea but I asking how to send message when I don't know it's handle.
Actually I have some solution -- save hWnd to temp file but what about functions? Is there any as Process.GetProcessesByName method?

Mutex and Semaphore is not much different for my situation. I prevent second instance correctly. I just don't want to use extra files for that sort of messaging.
Post 06 Sep 2020, 17:37
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4079
Location: vpcmpistri
bitRAKE 06 Sep 2020, 19:00
Any global object can be used for synchronization, but you want to send data globally - look into Atom tables. Maybe store the process id when the atom is created. Then all second instances can communicate with the first process.

https://docs.microsoft.com/en-us/windows/win32/dataxchg/about-atom-tables

Not all globals are the same: there is system, session, and application - global.

What about RegisterWindowMessageA / HWND_BROADCAST ?

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 06 Sep 2020, 19:00
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 06 Sep 2020, 21:13
HWND_BROADCAST hmmm.... seems it's best way to do my task. Only spam to all windows is not very elegant. Also I need to listen for such kind of messages all the time where I want checking task in second instance only.
Atom can manage one message where is nothing about condition or partly send of messages(handle). I can use it only to detect of second try of activation but it also not elegant enough to use as messenger for first of instance.

My friends, I need something to keep exist hWnd handle for next try of start but not file. Some sort of global memory. Or I need some easy access to previously started window by name of it. If nothing of this is possible I'll use temp file as it works almost good enough. Almost, as I don't like extra files. Not big deal actually.
Post 06 Sep 2020, 21:13
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20459
Location: In your JS exploiting you and your system
revolution 06 Sep 2020, 22:37
You would send a message to the process, not the window. Then the process can control its own windows as it needs to.
Post 06 Sep 2020, 22:37
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 06 Sep 2020, 23:00
You can try using File Mapping
Post 06 Sep 2020, 23:00
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 06 Sep 2020, 23:10
revolution, can you show any short example? Just to explain, don't worry about compilation )

sinsi, what is a different from using temp file?
Post 06 Sep 2020, 23:10
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4079
Location: vpcmpistri
bitRAKE 07 Sep 2020, 00:04
A mutex or file that is tied to the lifetime of the process is the most flexible solution and prevents strange corner cases. The problem is that there are a couple of dozen ways it could be done, but few correct ways.

You could hijack the clipboard with a special format. Twisted Evil
(it's just a glorified atom table)
Post 07 Sep 2020, 00:04
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 07 Sep 2020, 01:00
Post 07 Sep 2020, 01:00
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 08 Sep 2020, 17:40
sinsi, nice solution, thanks. No need to use semaphore or mutex any more too.
Code:
wm_initdialog:
        invoke  CreateFileMapping,INVALID_HANDLE_VALUE,0,PAGE_READWRITE,\
                        0,8,NameOfInstance
        mov             [hMapFile],rax
        invoke  GetLastError
        cmp             rax,0
        je              @F
        invoke  OpenFileMapping,FILE_MAP_ALL_ACCESS,FALSE,NameOfInstance
        mov             [hMapFile],rax
        invoke  MapViewOfFile,rax,FILE_MAP_ALL_ACCESS,0,0,8
        mov             [pMapBuf],rax
        mov             rbx,[rax]
        mov             [hWindow],rbx
        invoke  SetForegroundWindow,[hWindow]
        invoke  ShowWindow,[hWindow],SW_RESTORE
        jmp             wm_close
        @@:
        invoke  MapViewOfFile,[hMapFile],FILE_MAP_ALL_ACCESS,0,0,8
        mov             [pMapBuf],rax
        mov             rbx,[hWindow]
        mov             [rax],rbx
;...
; rest of initialization stage
;...
        invoke  UpdateWindow,[hWnd]
        mov             rax,1
        ret    
wm_close:
        invoke  UnmapViewOfFile,[pMapBuf]
        invoke  CloseHandle,[hMapFile]
        invoke  EndDialog,[hWnd],0
        xor             rax,rax
        ret    


Last edited by Overclick on 08 Sep 2020, 21:58; edited 1 time in total
Post 08 Sep 2020, 17:40
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4079
Location: vpcmpistri
bitRAKE 08 Sep 2020, 19:38
If I close the first instance after the second instance calls CreateFileMapping what happens then? I think both instances close - which is probably okay? If the first instance hasn't stored hWindow and another instance tried to read that value, what do they get?

https://devblogs.microsoft.com/oldnewthing/20151218-00/?p=92672

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup


Last edited by bitRAKE on 08 Sep 2020, 22:07; edited 1 time in total
Post 08 Sep 2020, 19:38
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 08 Sep 2020, 21:46
The second instance calls CreateFileMapping without success at time you try to run it. That will breake instance from initialization stage it will never be drawned. Also you cannot do two things at same time. But even if you can Smile yes, when you close the first one (the only drawned instance) it will close as normally requested (by pressed button for example).

I see ellipsis is not enough to explain continue of initialization stage. Let me correct it by:
Code:
...
invoke UpdateWindow,[hWnd]
mov     rax,1
ret    
Post 08 Sep 2020, 21:46
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4079
Location: vpcmpistri
bitRAKE 08 Sep 2020, 22:10
File mappings do not inherently have atomic access:
https://devblogs.microsoft.com/oldnewthing/20151218-00/?p=92672

It's possible for the second instance to get a zero hWindow - at the very least you should test for that and skip to the wm_close, imho.

I was not confused by your notation.
Post 08 Sep 2020, 22:10
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 08 Sep 2020, 22:37
Second process only read from named file mapping. It is going to be closed any way, so it's nothing to lose even so happen. Also it is not some sort of original file, it is already faked file with the name as object. I don't use dual access to it, I don't need to watch changes on it. It writes once and shares it's data to anyone asking for.

Also, your link is about paged mapping where different processes getting write access to same mapped file with page shifting. It is nothing about my situation. I don't have any hypothetical risk of that.
Post 08 Sep 2020, 22:37
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 09 Sep 2020, 10:14
If it's the same program, you should first try OpenFileMapping and check the return value, or look
for ERROR_ALREADY_EXISTS from the call to CreateFileMapping, even if it gives you a valid hwnd.
Post 09 Sep 2020, 10:14
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 09 Sep 2020, 11:28
Quote:

look for ERROR_ALREADY_EXISTS
So I do.
Post 09 Sep 2020, 11:28
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 10 Sep 2020, 00:10
I've got another solution that exactly I meant:
Code:
wm_initdialog:
        invoke  FindWindow,0,YourTitle
        mov             [hWindow],rax
        or              rax,rax
        jz              @F
        invoke  SetForegroundWindow,[hWindow]
        invoke  ShowWindow,[hWindow],SW_RESTORE
        jmp             wm_close
        @@:
        invoke  SetWindowText,[hWnd],YourTitle
        ...    
Post 10 Sep 2020, 00:10
View user's profile Send private message Visit poster's website 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.