Files
dl_old/dl/system/dl_windows.cpp
T
2025-02-17 19:00:08 +08:00

117 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "dl_windows.h"
#if DL_PLATFORM == DL_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <shellapi.h>
#include "io/dl_file.h"
#include "memory/dl_code_cvt.h"
namespace dl::Win32
{
SystemMutex::SystemMutex(std::string_view name, size_t count = 1) :
_free{ true },
_mutex{ NULL }
{
for (size_t i = 0; i < count; ++i)
{
_mutex = CreateMutexA(NULL, TRUE, fmt::format("{}{}", name, i).c_str());
if (_mutex == NULL)
return;
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
_free = false;
CloseHandle(_mutex);
_mutex = NULL;
}
else
{
_free = true;
return;
}
}
}
SystemMutex::~SystemMutex()
{
if (_mutex != NULL)
CloseHandle(_mutex);
}
bool OpenUrl(std::string_view url,
std::string_view cmd,
std::string_view work_path)
{
std::string gbk_exe = dl::CodeCvt::Utf8ToMultiByte(url);
std::string gbk_cmd = dl::CodeCvt::Utf8ToMultiByte(cmd);
std::string gbk_path = dl::CodeCvt::Utf8ToMultiByte(work_path);
HINSTANCE h = ShellExecuteA(NULL, "open", gbk_exe.c_str(), gbk_cmd.c_str(), gbk_path.c_str(), SW_SHOW);
if ((uint64_t)h <= 32)
{
log_err("打开失败:{},{}", url, cmd);
return false;
}
log_info("{} {} {}", url, cmd, work_path);
return true;
}
std::string SearchPathX(std::string_view path_child, int type)
{
for (char i = 'C'; i < 'H'; ++i)
{
if (!dl::File::IsExist(fmt::format("{}:/", i)))
continue;
if (type & EWST_PROGRAM)
{
std::string path = fmt::format("{}:/Program Files/{}",
i, path_child);
if (dl::File::IsExist(path))
return std::string{ path };
}
if (type & EWST_PROGRAM_X86)
{
std::string path = fmt::format("{}:/Program Files (x86)/{}",
i, path_child);
if (dl::File::IsExist(path))
return std::string{ path };
}
}
if (type & EWST_APPDATA_LOCAL)
{
std::string path = fmt::format("{}{}",
dl::File::PathStorageRoot(), path_child);
if (dl::File::IsExist(path))
return std::string{ path };
}
if (type & EWST_PROGRAM_DATA)
{
std::string path = fmt::format("{}{}",
dl::File::PathStorageRoot(), path_child);
if (dl::File::IsExist(path))
return std::string{ path };
}
// C:\Program Files\TortoiseSVN\bin
// C:\Program Files (x86)\TortoiseSVN\bin
// C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\33.0.2
// C:\Program Files (x86)\Android\android-sdk\build-tools\34.0.0
// D:\Program Files\Android\Android Studio\jbr\bin
// D:\Program Files\Android\Android Studio\jre\bin
return {};
}
Size GetWorkArea()
{
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
return toSize(rect.right - rect.left, rect.bottom - rect.top);
}
Size GetDesktopSize()
{
return toSize(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
}
}
#endif