Assembler handler multiple start

Source: Internet
Author: User
Tags exit resource

It is recommended that you use the MASM for EditPlus to test.

As "prevent the program multiple start" in the article said, when already knew own instance in the running, in order to save resources, the next instance needs to exit, but the user needs to open the file, if does not handle then exits, that user does not swear to be strange, therefore, needs to exit before processing.

The first thing to tell you is that when Windows programs exit, Windows clears all the resources that are occupied by the current process, except for dynamic DLLs (static DLLs are automatically freed, and for DLLs that are dynamically loaded at run time, the release work is done by the user, Although Windows checks to see if it has not been used, sometimes it will not be released for special reasons, which is what an operating system calls resource recycling (forgetting English words, as it is), then the problem comes when the program exits Although you can send a message to another program, such as an instance of itself, the question is how to pass a resource, where a string is passed, and the resource being passed is an address in the current process, and the string address passed when the program exits is no longer available in the case of Windows Resource recycling. So how do you pass a correct string to the previous instance?

We can use the messages in the API Wm_settext and Wm_gettext, which are returned immediately after the two messages are sent, so that the current process exits immediately after the message is sent, and the SendMessage function sends a message to determine whether the current message is WM_ SetText and Wm_gettext, if so, then SendMessage not just send a message, but create a memory image (standard called mapping) file, save the characters that need to be passed into the memory image, and then pass the address of the memory image as a parameter to the target form. While memory images can be shared between programs, this indirectly does the transfer of resources:


... ...
. DATA
Szclassname db "Winasm_class", 0
. Data?
lpCommandLine DD?
. CODE
START:
... ...
Invoke GetCommandLine
mov lpcommandline, eax
Invoke Findwindow,offset Szclassname,null; Find your own class
CMP eax,0
JNZ @F
Invoke WinMain ....
Invoke exitprocess,0
@@:
Invoke Sendmessage,eax,wm_settext,0,lpcommandline
Invoke exitprocess,1; Remember to return a non-0 value after an error, although this is not an error.
......
Therefore, we also need to handle wm_settext messages ourselves:
... ...
. ElseIf Umsg==wm_settext
Invoke messagebox,0,lparam,0,0

After adding the above code in the template-related place, run the you will find that the second instance will exit immediately, but the first instance will pop up a message box when the second instance exits, and if you pass the argument when the second instance is started, then the information in the information box is the trailing parameter of the second instance.

This allows you to process the Wm_settext message, such as opening the file from the instance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.