Skip to content

Instantly share code, notes, and snippets.

@ademkocamaz
Forked from mistic100/one-instance.cpp
Created December 9, 2022 08:55
Show Gist options
  • Save ademkocamaz/76a0fbd1ff03b0815024d782459c82e7 to your computer and use it in GitHub Desktop.
Save ademkocamaz/76a0fbd1ff03b0815024d782459c82e7 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