Skip to content

Instantly share code, notes, and snippets.

@mistic100
Last active July 1, 2023 22:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mistic100/2a40e96220f7482827d7 to your computer and use it in GitHub Desktop.
Save mistic100/2a40e96220f7482827d7 to your computer and use it in GitHub Desktop.
[C] Use Windows mutex to create a single instance app
#include <windows.h>
int main(int argc, char *argv[])
{
// ensure only one running instance
HANDLE hMutexHandle = CreateMutex(NULL, TRUE, L"my.mutex.name");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
return 0;
}
// rest of the program
ReleaseMutex(hMutexHandle);
CloseHandle(hMutexHandle);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment