原理是创建一个内核对象之后 如果再创建一个同名的对象 就会给代码中的GetLastError函数对应的变量修改为 ERROR_ALREADY_EXISTS (但是不影响"创建"对象 因为实际上并没有创建 只是把hMutex 变量指向了已存在的同名对象) 

#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<TCHAR.h>

int main()
{
    HANDLE hMutex = CreateMutex(NULL, FALSE, TEXT("JeffObj"));  //第一个参数一定是NULL不然没有用
    if (GetLastError() == ERROR_ALREADY_EXISTS) 
    {
        CloseHandle(hMutex);
        return(0);
    }
    printf("LastError: %d",GetLastError());
    _getch();
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-07-08
  • 2021-10-19
  • 2022-01-12
  • 2021-08-01
  • 2021-10-16
猜你喜欢
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案