January 1998
Download X-ProcessOptex.exe (3KB)
Jeffrey Richter wrote Advanced Windows, Third Edition (Microsoft Press, 1998) and Windows 95: A Developer's Guide (M&T Books, 1995). Jeff is a consultant and teaches Win32 programming courses (http://www.jeffreyrichter.com.)
I discussed how to write an unsetup program that was able to delete itself from the disk in my January 1996 Win32® Q&A column, and I presented three techniques for making an application delete itself. A reader recently suggested another technique that I'd like to share with you. I have tested this technique only on Windows NT®, but I believe that it will also work on Windows® 95. Figure 1 shows the code.
Q I need to synchronize threads running in multiple processes. I would like to use critical sections because it is my understanding that entering and leaving critical sections is significantly faster than waiting on and releasing mutex kernel objects. However, the Win32 documentation states that critical sections cannot be used to synchronize threads in multiple processes.
|
|
When constructing a COptex, you must give it a string name. This is used for sharing across process boundaries. When another process constructs a COptex using the same name, the constructor code will see that the object already exists and simply perform the desired synchronization across process boundaries, if necessary. Having the two constructors allows you to pass either an ANSI or Unicode name for the object. The second parameter to the constructor allows you to specify a spin count or accept the default of 4000 (which is what the operating system functions use to serialize a heap). The remaining members of the COptex class have a one-to-one correspondence with the Win32 critical section functions, so I will not describe them here. So how does the COptex work? Well, a COptex object actually consists of two data blocks: a local, private block and a global, shared block. When you create a COptex object, the local block consists of the data members inside the COptex class definition. The m_hevt and m_hfm members are initialized to a named event and a named memory-mapped file object, respectively. Since the objects are named, they can be shared across process boundaries. But since handles to these objects are process-relative, each COptex object must keep its own values for these handles. The m_pSharedInfo member points to the memory-mapped file, which contains the global data that all COptex objects for a given name share. The data in the memory-mapped file is a SHAREDINFO structure as privately defined inside the COptex class. A complete description of these members and how they relate to one another can be found in the July 1996 issue of MSJ. Note that the m_dwSpinCount member is new for this version. Have a question about programming in Win32? Send your questions via email to Jeffrey Richter from his website at http://www.jeffreyrichter.com. |
From the January 1998 issue of Microsoft Systems Journal.