flat assembler
Message board for the users of flat assembler.
Index
> Windows > [solved]Handle to window of exist instance. |
Author |
|
revolution 06 Sep 2020, 07:57
|
|||
06 Sep 2020, 07:57 |
|
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. |
|||
06 Sep 2020, 17:37 |
|
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 |
|||
06 Sep 2020, 19:00 |
|
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. |
|||
06 Sep 2020, 21:13 |
|
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.
|
|||
06 Sep 2020, 22:37 |
|
sinsi 06 Sep 2020, 23:00
You can try using File Mapping
|
|||
06 Sep 2020, 23:00 |
|
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? |
|||
06 Sep 2020, 23:10 |
|
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. (it's just a glorified atom table) |
|||
07 Sep 2020, 00:04 |
|
sinsi 07 Sep 2020, 01:00
|
|||
07 Sep 2020, 01:00 |
|
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 |
|||
08 Sep 2020, 17:40 |
|
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 |
|||
08 Sep 2020, 19:38 |
|
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 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 |
|||
08 Sep 2020, 21:46 |
|
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. |
|||
08 Sep 2020, 22:10 |
|
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. |
|||
08 Sep 2020, 22:37 |
|
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. |
|||
09 Sep 2020, 10:14 |
|
Overclick 09 Sep 2020, 11:28
Quote:
|
|||
09 Sep 2020, 11:28 |
|
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 ... |
|||
10 Sep 2020, 00:10 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.