简体   繁体   中英

Need help understanding “DBWinMutex”

I am writing my own version of DebugView using this article: https://www.codeproject.com/Articles/23776/Mechanism-of-OutputDebugString as a starting point. The code appears to work fine. However I do not understand the use of the named mutex "DBWinMutex". This mutex is opened at the beginning of the code:

CComBSTR DBWinMutex = L"DBWinMutex";
HANDLE m_hDBWinMutex = ::OpenMutex(MUTEX_ALL_ACCESS,
                                   FALSE,
                                   DBWinMutex);

and not closed before the end of the program??

I find this strange. I would think that the mutex would have to be locked and unlocked repeatedly so that OutputDebugString could write to the shared memory "DBWIN_BUFFER"?

However I am able to read OutputDebugString messages written by other programs so the mutex does not appear to lock "DBWIN_BUFFER" for writing. Also I can also run DebugView in parallell with my DebugView implementation and they both can read OutputDebugString messages. So it seems the mutex does not grant exclusive read to "DBWIN_BUFFER" neither.

Using the MUTEX_ALL_ACCESS access as above means I have to run the program as administrator. When I replace this with SYNCHRONIZE access the program appears to function exactly the same except that I do not have to run it as administrator. Is this OK or may it cause some subtle bug?

Also I test the return from OpenMutex above and if it is null call CreateMutex.

As described in the article you linked to , DBWinMutex is used only by OutputDebugString() itself, to prevent multiple threads from writing to the output buffer at the same time. It is not necessary for a debug monitor to use DBWinMutex at all:

图片

However, there is a mistake in the above image. It should look more like this instead:

图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM