gpt4 book ai didi

c++ - CreateMutex 和 OpenMutex 返回 NULL

转载 作者:可可西里 更新时间:2023-11-01 11:14:03 27 4
gpt4 key购买 nike

我正在编写一个具有共享内存的应用程序,并正在创建名称上带有适当 "Local\" 前缀的命名互斥体。但是,每次我调用 CreateMutex 函数来创建句柄时,我都会得到一个 NULL 返回值。我什至尝试在那之后调用 OpenMutex 并获得 NULL 返回。

GetLastError() 函数返回 6,这意味着 ERROR_INVALID_HANDLE。我相信这发生在第一次尝试在任何进程中创建这个命名互斥锁时。我在包含一些 MFC 组件之后包含了 windows.h,并在应用程序的其他地方使用了 CMutex;所以我不知道这是否是一个问题。我始终为前两个参数传递 NULLFALSE,并且我使用的是 Windows XP。

这是我的代码所做的总结:

char to_name[16] = "Local\\to_1";
d_mutex_to_h = CreateMutex(NULL, FALSE, to_name);

if (d_mutex_to_h == NULL)
{
d_mutex_to_h = OpenMutex(NULL, FALSE, to_name);
}

最佳答案

来自 CreateMutex 的 MSDN 文档:

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 namespace.

尝试使用 WinObj查看是否有同名的非互斥对象。

关于c++ - CreateMutex 和 OpenMutex 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8611171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com