Repository: xbmc/xbmc
File: xbmc/platform/win32/WinMain.cpp (C++)

Code before:
  constexpr DWORD NO_SHARING = 0;

  using KODI::PLATFORM::WINDOWS::ToW;
  handle = CreateFile(ToW(file).c_str(), NO_DESIRED_ACCESS, NO_SHARING, nullptr, CREATE_NEW,
                      FILE_FLAG_DELETE_ON_CLOSE, NULL);

  if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS)
    return true;

  return false;
}

/*!
 * \brief Detect another running instance, using a global mutex.
 * 
 * Attempt to create the mutex. If it already exists, that means another instance created it and is
 * running. This is the legacy pre-v22 exclusion mechanism.
 * 
 * \param[out] handle Windows handle for the mutex. Close the handle on program exit, not earlier.
 * \return true if the mutex already exists (ie another instance is running)
 */
bool CheckAndSetMutex(HANDLE& handle)
{
  using KODI::PLATFORM::WINDOWS::ToW;
  handle = CreateMutex(nullptr, FALSE,
                       ToW(std::string(CCompileInfo::GetAppName()) + " Media Center").c_str());
  if (handle != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
    return true;

  return false;
}
} // namespace

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
_Use_decl_annotations_ INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
  // parse command line parameters
  const auto params = ParseCommandLine();

  // this fixes crash if OPENSSL_CONF is set to existed openssl.cfg
  // need to set it as soon as possible
  CEnvironment::unsetenv("OPENSSL_CONF");

  // Initializes CreateMiniDump to handle exceptions.
  char ver[100];
  if (strlen(CCompileInfo::GetSuffix()) > 0)
    sprintf_s(ver, "%d.%d-%s Git:%s", CCompileInfo::GetMajor(),
    CCompileInfo::GetMinor(), CCompileInfo::GetSuffix(), CCompileInfo::GetSCMID());

Review comment (bug):
This code is not much readable and even confuse because final string variable is not used and CreateMutex parameter is only wchar_t*.   Why not only:

```c++
handle = CreateMutexW(nullptr, FALSE, L"Kodi Media Center");
```

After all the name of mutex is not very important because only is used here (even if in a future Kodi changes the name). Also call `CreateMutexW` to denote the intentionality of use wide char version.

Code after:

  constexpr DWORD NO_DESIRED_ACCESS = 0;
  constexpr DWORD NO_SHARING = 0;

  using KODI::PLATFORM::WINDOWS::ToW;
  handle = CreateFileW(ToW(file).c_str(), NO_DESIRED_ACCESS, NO_SHARING, nullptr, CREATE_NEW,
                       FILE_FLAG_DELETE_ON_CLOSE, NULL);

  if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS)
    return true;

  return false;
}

/*!
 * \brief Detect another running instance, using a global mutex.
 * 
 * Attempt to create the mutex. If it already exists, that means another instance created it and is
 * running. This is the legacy pre-v22 exclusion mechanism.
 * 
 * \param[out] handle Windows handle for the mutex. Close the handle on program exit, not earlier.
 * \return true if the mutex already exists (ie another instance is running)
 */
bool CheckAndSetMutex(HANDLE& handle)
{
  handle = CreateMutexW(nullptr, FALSE, L"Kodi Media Center");
  if (handle != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
    return true;

  return false;
}
} // namespace

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
_Use_decl_annotations_ INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
  // parse command line parameters
  const auto params = ParseCommandLine();

  // this fixes crash if OPENSSL_CONF is set to existed openssl.cfg
  // need to set it as soon as possible
  CEnvironment::unsetenv("OPENSSL_CONF");

  // Initializes CreateMiniDump to handle exceptions.
  char ver[100];
  if (strlen(CCompileInfo::GetSuffix()) > 0)
    sprintf_s(ver, "%d.%d-%s Git:%s", CCompileInfo::GetMajor(),
    CCompileInfo::GetMinor(), CCompileInfo::GetSuffix(), CCompileInfo::GetSCMID());