CreateMutex 一个命名的 mutex, 如果此名称已被使用,则会返回 NULL,否则返回 mutex 的HANDLE。
可是如下代码却:
HANDLE hMutex1 = CreateMutex(NULL, false, "MY_TEST_mutex");
HANDLE hMutex2 = CreateMutex(NULL, false, "MY_TEST_mutex");
HANDLE hMutex3 = CreateMutex(NULL, false, "MY_TEST_mutex");
却每次都反别返回
0x5c, 0x60, 0x64请问这是怎么回事啊?
可是如下代码却:
HANDLE hMutex1 = CreateMutex(NULL, false, "MY_TEST_mutex");
HANDLE hMutex2 = CreateMutex(NULL, false, "MY_TEST_mutex");
HANDLE hMutex3 = CreateMutex(NULL, false, "MY_TEST_mutex");
却每次都反别返回
0x5c, 0x60, 0x64请问这是怎么回事啊?
解决方案 »
- C和C++编程出现问题!!!
- 来,拼一拼Des速度,散分贴
- 如何将float 类型的数据转化为CString 类型的数据
- 求救---关于SetScrollInfo的问题,设置fRedraw=FALSE还是会重绘
- 高分:十六进制的数,比如A,转为ASCII码的值是多少?怎么计算?
- 100分!马上给!!(硬盘问题)
- 小弟初学TAPI,只是定义了一个新类,把TAPI封装一下,结果......请问为什么?
- WriteFile如何一行一行的写TXT文件
- 怎样写一个类似“发送到……”功能的程序?
- MFC fatal error C1189: #error 求助!!!
- vc里面怎样实现简单的查询?
- 请问使用CRecordset集支持数据库查询吗?如何写sql语句?
If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex.三次调用 createmutex 的返回也应该是一样的啊?
的通知!
HANDLE hMutex2 = CreateMutex(NULL, false, "MY_TEST_mutex");
HANDLE hMutex3 = CreateMutex(NULL, false, "MY_TEST_mutex");
却
hMutex1 = 0x005c;
hMutex2 = 0x0060;
hMutex3 = 0x0064;
照各位说法和 msdn 的说法,应该都是返回 0x005c 呀,
我都快憋死了,找不到问题所在。
大家可以编个例程自己试试,
1、如果已命名mutex存在,则另外创建一个 mutex, 二者独立,Win98、Win2k 均如此
2、如果已命名非mutex存在,则返回 false
3、如果OpenMutex,则必须closehandle,否则无法释放mutex, 如果再重新创建 mutex 的话,仍会出现ERROR_ALREADY_EXISTS错误。
以下代码
HANDLE hMutex1 = CreateMutex(NULL, false, "MY_TEST_mutex");
HANDLE hMutex2 = CreateMutex(NULL, false, "MY_TEST_mutex");
CloseMutex(hMutex1);
HANDLE hMutex3 = CreateMutex(NULL, false, "MY_TEST_mutex");
if( GetLastError()==ERROR_ALREADY_EXISTS)
AfxMessageBox("already exist!");
如果是副本,那么消息框不应出现,但是,试验是出现的。
我觉得应该是同一个,就是说,同一个Windows的核心对象,可以有不同的句柄。
但是,其表现和多个对象又有什么区别呢?我可以认为根本就是多个对象,只不过拥有同一名字。
msdn 上的说法实在是不太恰当。
我所知道的,窗口对象的handle 是唯一的,其他的process是唯一的, HMODULE因为是相对于进程的,所以也是唯一的。
而 event 的情况和 mutex 类似。
但是在WIN9X下没有解决,所以可能有问题
[in] Pointer to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters. Name comparison is case sensitive. //重点
If lpName matches the name of an existing named mutex object, this function requests the MUTEX_ALL_ACCESS access right. In this case, the bInitialOwner
//parameter is ignored because it has already been set by the creating process. If the lpMutexAttributes parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.If lpName is NULL, the mutex object is created without a name.//重点, 这里没有说 the name of existing mutex
If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
//
If lpName matches the name of an existing named mutex object, this function requests the MUTEX_ALL_ACCESS access right. In this case, the bInitialOwner
//
如果你设置了 MUTEX_ALL_ACCESS,就是有了一个了,这个时候,CreateMutex 就是 打开,而不是 创建。
2、如果已命名非mutex存在,则返回 false//重点, 这里没有说 the name of existing mutex
If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
//
这个对应你的 2。
HANDLE hMutex1 = CreateMutex(NULL, false, "MY_TEST_mutex");
HANDLE hMutex2 = CreateMutex(NULL, false, "MY_TEST_mutex");
ReleaseMutex(hMutex1);
ReleaseMutex(hMutex2);
HANDLE hMutex3 = CreateMutex(NULL, false, "MY_TEST_mutex");
if( GetLastError()==ERROR_ALREADY_EXISTS)
AfxMessageBox("already exist!");
还是会执行AfxMessageBox("already exist!");好像没有CloseMutex()这个函数
我的想法是这样的,你用ReleaseMutex(hMutex1);只能释放hMutex1这个变量,而它所标识的资源仍然还在,所以无论你ReleaseMutex多少次,if( GetLastError()==ERROR_ALREADY_EXISTS)都会成立,除非资源被释放,即进程或线程退出,或者关闭了使用该资源的句柄。
是 CloseHandle,没有 CloseMutex 函数。
的确,只有一个内核对象。
我一直纳闷对于 createMutex、OpenMutex,
多个进程中有不同的handle 可以理解,为何同一进程中会返回不同handle
其实很 sillily 的, mutex 是同步线程的,而非同步进程的,
而进程的地址空间是统一的,这要求当同一进程中的不同线程在存取同一mutex时,必须有不同的 ID, 也就是 handle 了。
这样看,便一切都解决了。
当一个命名 mutex 已经存在时, createMutex 和 OpenMutex 是没有太多区别的。
这时,两个函数都是一方面是增加一个全新的进程内的handle到内核对象的映射,一方面是在内核对象的引用计数加一;
另外, CloseHandle 一方面是释放进程内的handle资源,一方面是在内核对象的引用计数减一。