简体   繁体   English

需要帮助理解“DBWinMutex”

[英]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.我正在使用这篇文章编写我自己的 DebugView 版本: https://www.codeproject.com/Articles/23776/Mechanism-of-OutputDebugString作为起点。 The code appears to work fine.该代码似乎工作正常。 However I do not understand the use of the named mutex "DBWinMutex".但是我不明白命名互斥体“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"?我认为必须反复锁定和解锁互斥锁,以便 OutputDebugString 可以写入共享的 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.但是,我能够读取其他程序编写的 OutputDebugString 消息,因此互斥锁似乎不会锁定“DBWIN_BUFFER”进行写入。 Also I can also run DebugView in parallell with my DebugView implementation and they both can read OutputDebugString messages.此外,我还可以与我的 DebugView 实现并行运行 DebugView,它们都可以读取 OutputDebugString 消息。 So it seems the mutex does not grant exclusive read to "DBWIN_BUFFER" neither.因此,互斥锁似乎也不授予“DBWIN_BUFFER”独占读取权限。

Using the MUTEX_ALL_ACCESS access as above means I have to run the program as administrator.使用上面的 MUTEX_ALL_ACCESS 访问意味着我必须以管理员身份运行程序。 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.当我将其替换为 SYNCHRONIZE 访问时,该程序在 function 看来完全相同,只是我不必以管理员身份运行它。 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.我还测试了上面 OpenMutex 的返回,如果它是 null 调用 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.您链接到的文章中所述, DBWinMutex仅由OutputDebugString()本身使用,以防止多个线程同时写入 output 缓冲区。 It is not necessary for a debug monitor to use DBWinMutex at all:调试监视器根本不需要使用DBWinMutex

图片

However, there is a mistake in the above image.但是,上图中有一个错误。 It should look more like this instead:它应该看起来更像这样:

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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