VC + + Implementation of the program mutually exclusive operation

Source: Internet
Author: User

In the software development process, sometimes it is necessary to control some programs can not run at the same time, that is, multiple programs mutually exclusive operation (also includes the prohibition of running multiple instances of the same program). To address this issue, we implemented a mutex operation between multiple programs using a memory-mapped file in Visual C + + 6.0.

Before you talk about specific programming methods, let's take a look at several important functions related to memory-mapped file operations:

1 The CreateFileMapping function creates a file mapping object for the specified file, which is the prototype of the following:

HANDLE CreateFileMapping(
   HANDLE hFile, // 用于映射的文件句柄
   LPSECURITY_ATTRIBUTES lpFileMappingAttributes, // 内存映射文件的安全描述符
   DWORD flProtect, // 文件的保护方式
   DWORD dwMaximumSizeHigh,
  // 文件映射对象的最大长度的高32位
   DWORD dwMaximumSizeLow,
  // 最大长度的低32位
   LPCTSTR lpName      
  // 指定这个内存映射文件的名字
  );

2 The MapViewOfFile function maps the view of a file to the address space of a process, returns the memory pointer of the LPVOID type, through which you can directly access the information in the file view:

LPVOID MapViewOfFile (
HANDLE Hfilemappingobjct,
Mapping File Object handles
DWORD dwdesiredaccess,//access mode
DWORD Dwfileoffsethigh,
High 32 bits of file offset address
DWORD Dwfileoffsetlow,
Low 32 bits of file offset address
DWORD Dwnumberofbytestomap
Size of Mapped view
);
In Visual C + + 6.0, we generate a dialog based application by default, and at the beginning of the initialization phase of the CWinApp derived class, add the following code at the start of the InitInstance function:
{HANDLE hmap=createfilemapping (HANDLE) 0xFFFFFFFF, NULL,
Page-readwrite, 0, 128,″mutexrunning″);
if (hmap==null)//If creation fails
{AfxMessageBox (″) failed to create a memory-mapped file object for mutual exclusion running!″
mb-ok| Mb-iconstop);
return FALSE; }
else if (GetLastError () ==error-already-exists)
{lpvoid lpmem=mapviewoffile (hmap, File_map-write, 0,0,0);
CString str= (Char) Lpmem;
UnmapViewOfFile (LPMEM);
CloseHandle (HMAP);
AfxMessageBox (str, mb-ok| Mb-iconstop);
return FALSE; }
Else
{lpvoid lpmem=mapviewoffile (hmap, File-map-write, 0,0,0);
strcpy (Char) lpmem,″xxx program is running!″);
UnmapViewOfFile (LPMEM);
}

AfxEnableControlContainer ();
......
This can be called before the InitInstance function is last Returnfalse
CloseHandle (HMAP);
Turn off the memory-mapped file object handle
return FALSE;
}

The above program has been debugged in Visual C + + 6.0.

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.