Prevent multiple start of programs

Source: Internet
Author: User
Tags api manual mutex

This article is a continuation of the 14th chapter of the "DOS to Win32" series of the following, which is recommended for review before reading.

As the sweet incense said, command-line arguments are ubiquitous in windows, but generally do not feel, double-click a TXT file, Windows will start the Notepad program and the TXT path as a parameter to provide it, which of course quite fast, generally open a file will again start associated with the program , but you will find that many multiple document (MDI) software will not start a new instance when it is running, but that the program that is already running opens the file you need to open again, which is to prevent multiple startup of the program.

This article first tells how to prevent, the next article again how to deal with (is to let the instance that already run to open the next instance of the file).

There are a number of ways to prevent programs from starting multiple, as follows:

1. You can use the FindWindow to find the title of the program that successfully returns the target window handle and fails to null.

 szWindowName db "Hello World!",0
     ... ...
     invoke FindWindow,NULL,offset szWindowName
     .if eax!=NULL
     invoke WinMain....
     .end if
     invoke ExitProcess,0

This method is not very good, because there will be some software window with the same name as your window title, attention to look at the above FindWindow, reference API manual found that the first parameter is classname, window class name in addition to their own instances are generally unique, this method is generally feasible:

2. Using the FindWindow Lookup program window class name, the function successfully returns the target window handle, failed to null.

 szClassName db "WINASM_CLASS!",0
     ... ...
     invoke FindWindow,offset szClassName,NULL
     .if eax!=NULL
     invoke WinMain....
     .end if
     invoke ExitProcess,0

But the disadvantage of this method is that when the program does not have a standard window or the dialog box as the main window of the program does not apply, there is no standard window of course no class name, and the dialog box as the main window of the program, because the default dialog box class names are #32768, This, of course, does not make it possible to determine whether an instance has been run.

3. Create a mutex (mutex) object, which is a system global identity that can be accessed by all programs after it is created. This function successfully returns a handle to a mutex object, and the failure returns null, but is generally judged by the GetLastError error number.

.data
      szMutex db "_Me?",0
.data?
      hMutex dd ?
      hInstance dd ?
.CODE
START:
      invoke GetModuleHandle,NULL
      mov hInstance,eax
      invoke
CreateMutex,NULL,TRUE,offset szMutex
      mov hMutex,eax
      invoke GetLastError        获得最后发生的错误编号
      cmp eax,ERROR_ALREADY_EXISTS  已经存在
      jz @F
      invoke WinMain...
      invoke ReleaseMutex,hMutex     只有在正常运行后才释放Mutex对象
      @@:
      invoke ExitProcess,0

4. There are other methods, such as creating a file in the same directory or Windows Temp directory at the start of the program, quit delete it, then the second more instances after running first to determine whether the existence of this file, exits, does not exist to start, if a program to use the INI is more convenient, and above said the same , set a key at startup, and then set to 0 when it exits, and the second instance takes this key as a "mutex". and write a key to the registry, the same way as above.

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.