#include "pch.h" #include #include #include #include #include #include #include #include #include #include // C++11ÀÇ °íǰÁú ·£´ý ¶óÀ̺귯¸® #include // ½Ã°£ ±â¹ÝÀÇ ½Ãµå°ª »ý¼ºÀ» À§ÇØ »ç¿ë #include // Tool-Help ¶óÀ̺귯¸® #pragma warning(disable:4996) // // convert_ansi_to_unicode_string. // DWORD convert_ansi_to_unicode_string( __out std::wstring& unicode, __in const char* ansi, __in const size_t ansi_size ) { DWORD error = 0; do { if ((NULL == ansi) || (0 == ansi_size)) { error = ERROR_INVALID_PARAMETER; break; } unicode.clear(); // // getting required cch. // int required_cch = ::MultiByteToWideChar( CP_ACP, 0, ansi, static_cast(ansi_size), NULL, 0 ); if (0 == required_cch) { error = ::GetLastError(); break; } unicode.resize(required_cch); // // convert. // if (0 == ::MultiByteToWideChar( CP_ACP, 0, ansi, static_cast(ansi_size), const_cast(unicode.c_str()), static_cast(unicode.size()) )) { error = ::GetLastError(); break; } } while (false); return error; } // // convert_unicode_to_ansi_string. // DWORD convert_unicode_to_ansi_string( __out std::string& ansi, __in const wchar_t* unicode, __in const size_t unicode_size ) { DWORD error = 0; do { if ((NULL == unicode) || (0 == unicode_size)) { error = ERROR_INVALID_PARAMETER; break; } ansi.clear(); // // getting required cch. // int required_cch = ::WideCharToMultiByte( CP_ACP, 0, unicode, static_cast(unicode_size), NULL, 0, NULL, NULL ); if (0 == required_cch) { error = ::GetLastError(); break; } // // allocate. // ansi.resize(required_cch); // // convert. // if (0 == ::WideCharToMultiByte( CP_ACP, 0, unicode, static_cast(unicode_size), const_cast(ansi.c_str()), static_cast(ansi.size()), NULL, NULL )) { error = ::GetLastError(); break; } } while (false); return error; } // // convert_unicode_to_utf8_string // DWORD convert_unicode_to_utf8_string( __out std::string& utf8, __in const wchar_t* unicode, __in const size_t unicode_size ) { DWORD error = 0; do { if ((NULL == unicode) || (0 == unicode_size)) { error = ERROR_INVALID_PARAMETER; break; } utf8.clear(); // // getting required cch. // int required_cch = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, unicode, static_cast(unicode_size), NULL, 0, NULL, NULL ); if (0 == required_cch) { error = ::GetLastError(); break; } // // allocate. // utf8.resize(required_cch); // // convert. // if (0 == ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, unicode, static_cast(unicode_size), const_cast(utf8.c_str()), static_cast(utf8.size()), NULL, NULL )) { error = ::GetLastError(); break; } } while (false); return error; } // // convert_utf8_to_unicode_string // DWORD convert_utf8_to_unicode_string( __out std::wstring& unicode, __in const char* utf8, __in const size_t utf8_size ) { DWORD error = 0; do { if ((NULL == utf8) || (0 == utf8_size)) { error = ERROR_INVALID_PARAMETER; break; } unicode.clear(); // // getting required cch. // int required_cch = ::MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, static_cast(utf8_size), NULL, 0 ); if (0 == required_cch) { error = ::GetLastError(); break; } // // allocate. // unicode.resize(required_cch); // // convert. // if (0 == ::MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, static_cast(utf8_size), const_cast(unicode.c_str()), static_cast(unicode.size()) )) { error = ::GetLastError(); break; } } while (false); return error; } DWORD convert_utf8_to_unicode_pointer( __out wchar_t ** unicode, __in const char* utf8, __in const size_t utf8_size ) { DWORD error = 0; WCHAR * buff = NULL; do { if ((NULL == utf8) || (0 == utf8_size)) { error = ERROR_INVALID_PARAMETER; break; } // // getting required cch. // int required_cch = ::MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, static_cast(utf8_size), NULL, 0 ); if (0 == required_cch) { error = ::GetLastError(); break; } // // allocate. // buff = (wchar_t *)malloc(required_cch * sizeof(wchar_t) + sizeof(wchar_t)); memset(buff, 0, required_cch * sizeof(wchar_t) + sizeof(wchar_t)); // // convert. // if (0 == ::MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, static_cast(utf8_size), const_cast(buff), static_cast(required_cch) )) { error = ::GetLastError(); break; } *unicode = buff; } while (false); return error; } DWORD convert_utf8_to_ansi_pointer(string &utf8_to_ansi) { DWORD error; wstring uni; string ansi; error = convert_utf8_to_unicode_string(uni, utf8_to_ansi.c_str(), utf8_to_ansi.length()); if (error) return error; utf8_to_ansi = ""; error = convert_unicode_to_ansi_string(ansi, uni.c_str(), uni.length()); if (error) return error; utf8_to_ansi = ansi; return error; } DWORD convert_ansi_to_utf8_pointer(string& ansi_to_utf8) { wstring uni; string ansi; DWORD error = convert_ansi_to_unicode_string(uni, ansi_to_utf8.c_str(), ansi_to_utf8.length()); if (error) return error; error = convert_unicode_to_utf8_string(ansi_to_utf8, uni.c_str(), uni.length()); if (error) return error; return error; } DWORD GetTime() { time_t local_now; time(&local_now); return (DWORD)local_now; } BOOL UtilRunProcessNoWait(const wchar_t* path, bool wait) { BOOL bRet = FALSE; WCHAR szCommandCode[MAX_PATH] = { 0, }; StringCbPrintfW(szCommandCode, sizeof(szCommandCode), L"%s", path); BSONE_INFOW(L"(%s)", szCommandCode); wchar_t workingDir[MAX_PATH]; wcscpy_s(workingDir, MAX_PATH, szCommandCode); // 3. Àüü °æ·Î¿¡¼­ ÆÄÀÏ À̸§(MyCSharpApp.exe)À» Á¦°ÅÇÏ¿© µð·ºÅ͸® °æ·Î¸¸ ³²±è // ÀÌ ÀÛ¾÷ÀÇ °á°ú·Î workingDir¿¡´Â "C:\MyCSharpApp"¸¸ ³²°Ô µË´Ï´Ù. PathRemoveFileSpecW(workingDir); STARTUPINFOW si = {0, }; PROCESS_INFORMATION pi = { 0, }; si.cb = sizeof(si); BOOL bExecuteRet = CreateProcessW(NULL, szCommandCode, NULL, NULL, FALSE, 0, NULL, workingDir, &si, &pi); if (!bExecuteRet) { BSONE_INFOW(L"CreateProcess ErrorCode(%d)", GetLastError()); return FALSE; } if (wait == true) { WaitForSingleObject(pi.hProcess, INFINITE); BSONE_INFOW(L"WaitForSingleObject--="); } CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return bExecuteRet; } /** * @brief ÁöÁ¤µÈ Ä¿¸Çµå ¶óÀÎÀ» ½ÇÇàÇϰí, ÇØ´ç ÇÁ·Î¼¼½º°¡ Á¾·áµÉ ¶§±îÁö ´ë±âÇÕ´Ï´Ù. * @param hParentWnd ºÎ¸ð À©µµ¿ì ÇÚµé. ºñȰ¼ºÈ­/Ȱ¼ºÈ­ 󸮸¦ À§ÇØ ÇÊ¿äÇÕ´Ï´Ù. * @param szCommandLine ½ÇÇàÇÒ ÇÁ·Î±×·¥ÀÇ Ä¿¸Çµå ¶óÀÎÀÔ´Ï´Ù. * @return ÀÚ½Ä ÇÁ·Î¼¼½ºÀÇ Á¾·á Äڵ带 ¹ÝȯÇÕ´Ï´Ù. ÇÁ·Î¼¼½º »ý¼º ½ÇÆÐ ½Ã -1À» ¹ÝȯÇÕ´Ï´Ù. */ DWORD UtilCreateProcessAsModal(LPCWSTR szCommandLine) { HWND hParentWnd = GetActiveWindow(); // 1. ºÎ¸ð À©µµ¿ì ºñȰ¼ºÈ­ EnableWindow(hParentWnd, FALSE); STARTUPINFOW si = { sizeof(si) }; PROCESS_INFORMATION pi; // 2. ÀÚ½Ä ÇÁ·Î¼¼½º ½ÇÇà BOOL bSuccess = CreateProcess( NULL, (LPWSTR)szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ); if (!bSuccess) { //MessageBox(hParentWnd, TEXT("ÇÁ·Î¼¼½º »ý¼º¿¡ ½ÇÆÐÇß½À´Ï´Ù."), TEXT("¿À·ù"), MB_OK); EnableWindow(hParentWnd, TRUE); return -1; // ½ÇÆÐ ÄÚµå } // 3. ÀÚ½Ä ÇÁ·Î¼¼½º°¡ ³¡³¯ ¶§±îÁö ´ë±â WaitForSingleObject(pi.hProcess, INFINITE); // 4. ÀÚ½Ä ÇÁ·Î¼¼½ºÀÇ Á¾·á ÄÚµå °¡Á®¿À±â DWORD dwExitCode = 0; GetExitCodeProcess(pi.hProcess, &dwExitCode); // 5. ÇÚµé Á¤¸® CloseHandle(pi.hProcess); CloseHandle(pi.hThread); // 6. ºÎ¸ð À©µµ¿ì ´Ù½Ã Ȱ¼ºÈ­ EnableWindow(hParentWnd, TRUE); SetForegroundWindow(hParentWnd); return dwExitCode; } DWORD GetParentID(DWORD p_ID) { PROCESSENTRY32 pEntry; BOOL bRes = FALSE; HANDLE hSnapShot = NULL; HANDLE hProcess = NULL; DWORD ret = 0; hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL); pEntry.dwSize = sizeof(pEntry); Process32First(hSnapShot, &pEntry); while (1) { bRes = Process32Next(hSnapShot, &pEntry); if (!bRes) break; if (pEntry.th32ProcessID == p_ID) { ret = pEntry.th32ParentProcessID; break; } } CloseHandle(hSnapShot); return ret; } BOOL IsWinVistaOSAbove() { OSVERSIONINFO osvi; memset(&osvi, 0x00, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); if (osvi.dwMajorVersion >= 6) return TRUE; return FALSE; } BOOL UtilExistRegKey(HKEY root, const wchar_t* key) { HKEY hKey; if (RegOpenKey(root, key, &hKey) != ERROR_SUCCESS) return FALSE; return TRUE; } wstring UtilGetRegValue(HKEY root, const wchar_t * key, const wchar_t* name) { HKEY hKey; wchar_t tmp_buffer[1024]; memset(tmp_buffer, 0, 1024); DWORD tmp_size = 1024; DWORD type; RegOpenKey(root, key, &hKey); RegQueryValueEx(hKey, name, 0, &type, (BYTE*)tmp_buffer, &tmp_size); RegCloseKey(hKey); return tmp_buffer; } BOOL IsWin8OSAbove() { OSVERSIONINFO osvi; memset(&osvi, 0x00, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); // Windows 10 if (osvi.dwMajorVersion > 6) return TRUE; // Windows 8 (6.2) , Windows 7 (6.1) if ((osvi.dwMajorVersion == 6) && (osvi.dwMinorVersion >= 2)) return TRUE; return FALSE; } typedef BOOL(__stdcall* LPFN_ISWOW64PROCESS) (HANDLE hProcess, PBOOL Wow64Process); BOOL IsWow64() { //MOD5198 BOOL bIsWow64 = FALSE; HMODULE hKernel = GetModuleHandle(_T("kernel32")); if (hKernel) { LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(hKernel, "IsWow64Process"); if (NULL != fnIsWow64Process) { if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64)) { // handle error #ifdef MAXUP_DEBUG DbgPrint("[KDFINJ] IsWow64Process Error: %u", GetLastError()); #endif } } fnIsWow64Process = NULL; FreeLibrary(hKernel); hKernel = NULL; } return bIsWow64; } std::wstring UtilGetUserWindowID() { std::wstring str; wchar_t name[MAX_PATH] = { 0, }; DWORD size = ARRAYSIZE(name); GetUserNameW(name, &size); str = name; return str; } std::wstring UtilGetIP() { char chName[255] = { 0, }; //CString sAddress; PHOSTENT pHostEntry; IN_ADDR inAddr; if (gethostname(chName, 255) != 0) { return L""; } else { if ((pHostEntry = gethostbyname(chName)) == NULL) { return L""; } else { memcpy(&inAddr, pHostEntry->h_addr, 4); string addr = inet_ntoa(inAddr); wstring waddr; convert_ansi_to_unicode_string(waddr, addr.c_str(), addr.length()); return waddr; } } return L""; } BOOL UtilGetIPMac(wstring& strIP, wstring& strMac) { PHOSTENT hostInfo = NULL; char szHost[100] = { 0, }; int iResult = 0; WSADATA wsaData = { 0, }; ULONG pulMac[2] = { 0, }; ULONG dwdstip = 0; IPAddr udstaddr = { 0, }; ULONG ulLen = 6; DWORD dwRetSARP = 0; BYTE gwmac[6] = { 0, }; WCHAR macAddress[MAX_PATH] = { 0, }; wchar_t wszPCName[MAX_PATH] = { 0, }; DWORD dwBufferSize = MAX_PATH; string pcname = ""; iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { //BSONE_INFOA("WSAStartup Error(%d)", WSAGetLastError()); return FALSE; } GetComputerNameW(wszPCName, &dwBufferSize); convert_unicode_to_ansi_string(pcname, wszPCName, wcslen(wszPCName)); //BSONE_INFOA("szPCName[%s]", pcname.c_str()); if ((hostInfo = gethostbyname(pcname.c_str())) != NULL) { if (*hostInfo->h_addr_list != NULL) { StringCbPrintfA(szHost, sizeof(szHost), "%s", inet_ntoa(*(struct in_addr*)*hostInfo->h_addr_list)); convert_ansi_to_unicode_string(strIP, szHost, strlen(szHost)); //strIP = CA2T(szHost); //K_DEBUG(_T("GetLocalIPMac, szHost[%s]"), strIP); //BSONE_INFOA("ip : ", strIP.c_str()); dwdstip = inet_addr(szHost); udstaddr = dwdstip; if (dwdstip != INADDR_NONE) { memset(pulMac, 0xff, sizeof(pulMac)); if ((dwRetSARP = SendARP(udstaddr, 0, pulMac, &ulLen)) == NO_ERROR) { memcpy(gwmac, pulMac, sizeof(gwmac)); //ULONG(8byte) 6byte¸¸Å­¸¸ write StringCbPrintfW(macAddress, ARRAYSIZE(macAddress), L"%02X%02X%02X%02X%02X%02X", gwmac[0], gwmac[1], gwmac[2], gwmac[3], gwmac[4], gwmac[5]); strMac = macAddress; //K_DEBUG(_T("GetLocalIPMac, strGWMac[%s]"), strMac.GetBuffer(0)); WSACleanup(); return TRUE; } } } } WSACleanup(); return FALSE; } #include typedef struct _ASTAT_ { ADAPTER_STATUS adapt; NAME_BUFFER NameBuff[30]; } ASTAT, * PASTAT; std::wstring UtilGetMac() { std::wstring macaddr = L""; NCB Ncb; ASTAT Adapter; UCHAR uRetCode; LANA_ENUM lenum; int i; memset(&Ncb, 0, sizeof(Ncb)); Ncb.ncb_command = NCBENUM; Ncb.ncb_buffer = (UCHAR*)&lenum; Ncb.ncb_length = sizeof(lenum); uRetCode = Netbios(&Ncb); for (i = 0; i < lenum.length; i++) { memset(&Ncb, 0, sizeof(Ncb)); Ncb.ncb_command = NCBRESET; Ncb.ncb_lana_num = lenum.lana[i]; uRetCode = Netbios(&Ncb); memset(&Ncb, 0, sizeof(Ncb)); Ncb.ncb_command = NCBASTAT; Ncb.ncb_lana_num = lenum.lana[i]; strcpy((char*)Ncb.ncb_callname, "* "); Ncb.ncb_buffer = (unsigned char*)&Adapter; Ncb.ncb_length = sizeof(Adapter); uRetCode = Netbios(&Ncb); if (uRetCode == 0) { wchar_t tmp[255] = { 0, }; _snwprintf(tmp, sizeof(tmp), L"%02x%02x%02x%02x%02x%02x", Adapter.adapt.adapter_address[0], Adapter.adapt.adapter_address[1], Adapter.adapt.adapter_address[2], Adapter.adapt.adapter_address[3], Adapter.adapt.adapter_address[4], Adapter.adapt.adapter_address[5] ); macaddr = tmp; } } return macaddr; } // CString -> std::wstring ¸â¹ö·Î º¯°æ ÇÊ¿ä // ¿¹½Ã: std::wstring dmPrinterName; // ¾Æ·¡ ÇÔ¼öµé¿¡¼­ CString °ü·Ã ºÎºÐÀ» std::wstringÀ¸·Î º¯°æ // ¹®ÀÚ¿­ ´ë¼Ò¹®ÀÚ º¯È¯ std::wstring ToUpper(const std::wstring& str) { std::wstring result = str; std::transform(result.begin(), result.end(), result.begin(), ::towupper); return result; } std::wstring ToLower(const std::wstring& str) { std::wstring result = str; std::transform(result.begin(), result.end(), result.begin(), ::towlower); return result; } std::string ToUpperA(const std::string& str) { std::string result = str; std::transform(result.begin(), result.end(), result.begin(), ::towupper); return result; } std::string ToLowerA(const std::string& str) { std::string result = str; std::transform(result.begin(), result.end(), result.begin(), ::towlower); return result; } // ¹®ÀÚ¿­ ºñ±³ (¿ÞÂÊ n±ÛÀÚ) bool StartsWith(const std::wstring& str, const std::wstring& prefix) { return str.compare(0, prefix.size(), prefix) == 0; } // ¹®ÀÚ¿­ Æ÷ÇÔ ¿©ºÎ bool Contains(const std::wstring& str, const std::wstring& substr) { return str.find(substr) != std::wstring::npos; } // ¹®ÀÚ¿­ µ¡¼À std::wstring operator+(const std::wstring& a, const char* b) { std::wstring wb; size_t len = strlen(b); wb.resize(len); mbstowcs(&wb[0], b, len); return a + wb; } // ÁÂ¿ì °ø¹éÀ» Á¦°ÅÇÏ´Â Trim ÇÔ¼ö std::wstring Trim(const std::wstring& str) { auto begin = std::find_if_not(str.begin(), str.end(), iswspace); auto end = std::find_if_not(str.rbegin(), str.rend(), iswspace).base(); if (begin >= end) return L""; return std::wstring(begin, end); } void ReplaceAll(std::wstring& message, const std::wstring& pattern, const std::wstring& replace) { if (pattern.empty()) return; size_t pos = 0; while ((pos = message.find(pattern, pos)) != std::wstring::npos) { message.replace(pos, pattern.length(), replace); pos += replace.length(); // ¹Ù²ï ºÎºÐ ÀÌÈĺÎÅÍ ´Ù½Ã °Ë»ö } } /** * @brief ÇöÀç ½Ã°£À» ISO 8601 Çü½ÄÀÇ ¹®ÀÚ¿­·Î ¹ÝȯÇÏ´Â API ÇÔ¼ö * @return "YYYY-MM-DDTHH:MM:SS.ms+HH:MM" Çü½ÄÀÇ std::string */ std::string UtilGetCurrentTimestampISO8601(tm &tm_info) { auto now = std::chrono::system_clock::now(); auto now_c = std::chrono::system_clock::to_time_t(now); //std::tm tm_info; #ifdef _WIN32 localtime_s(&tm_info, &now_c); #else localtime_r(&now_c, &tm_info); #endif auto ms_part = std::chrono::duration_cast(now.time_since_epoch()) % 1000; int tz_hours = 0; int tz_minutes = 0; #ifdef _WIN32 TIME_ZONE_INFORMATION tzInfo; GetTimeZoneInformation(&tzInfo); long offset_minutes = -tzInfo.Bias; tz_hours = offset_minutes / 60; tz_minutes = abs(offset_minutes % 60); #else long offset_seconds = tm_info.tm_gmtoff; tz_hours = offset_seconds / 3600; tz_minutes = abs((offset_seconds % 3600) / 60); #endif // ¹®ÀÚ¿­À» ÀúÀåÇÒ ¹öÆÛ¸¦ ÃæºÐÇÑ Å©±â·Î Áغñ char buffer[100] = { 0, }; // snprintf¸¦ »ç¿ëÇØ ÇÑ ¹ø¿¡ ¹®ÀÚ¿­ Æ÷¸ËÆÃ // chrono::milliseconds::count()´Â long long ŸÀÔÀ» ¹ÝȯÇÒ ¼ö ÀÖÀ¸¹Ç·Î %lld »ç¿ë snprintf(buffer, sizeof(buffer), "%04d-%02d-%02dT%02d:%02d:%02d.%03lld%+03d:%02d", tm_info.tm_year + 1900, tm_info.tm_mon + 1, tm_info.tm_mday, tm_info.tm_hour, tm_info.tm_min, tm_info.tm_sec, (long long)ms_part.count(), // C++11 Ç¥ÁØ¿¡ µû¶ó long longÀ¸·Î ij½ºÆÃ tz_hours, tz_minutes); return std::string(buffer); } struct EnumData { DWORD dwProcessId; // [ÀÔ·Â] ãÀ¸·Á´Â ÇÁ·Î¼¼½º ID HWND hWnd; // [Ãâ·Â] ãÀº À©µµ¿ì ÇÚµé }; BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { EnumData* pData = (EnumData*)lParam; DWORD dwProcessId = 0; // À©µµ¿ì ÇÚµé·ÎºÎÅÍ ÇÁ·Î¼¼½º ID¸¦ ¾ò½À´Ï´Ù. GetWindowThreadProcessId(hWnd, &dwProcessId); // ãÀ¸·Á´Â ÇÁ·Î¼¼½º ID¿Í ÀÏÄ¡ÇÏ´ÂÁö È®ÀÎÇÕ´Ï´Ù. if (dwProcessId == pData->dwProcessId) { // Ãß°¡ Á¶°Ç: È­¸é¿¡ º¸À̰í, ¼ÒÀ¯ÀÚ°¡ ¾ø´Â âÀ» '¸ÞÀÎ À©µµ¿ì'·Î °£ÁÖÇÕ´Ï´Ù. if (IsWindowVisible(hWnd) && GetWindow(hWnd, GW_OWNER) == NULL) { pData->hWnd = hWnd; // ã¾ÒÀ¸¹Ç·Î ´õ ÀÌ»ó ¿­°Å¸¦ °è¼ÓÇÒ Çʿ䰡 ¾øÀ½ (FALSE ¹Ýȯ) return FALSE; } } // °è¼Ó ´ÙÀ½ À©µµ¿ì¸¦ °Ë»ö (TRUE ¹Ýȯ) return TRUE; } HWND FindMainWindow(DWORD dwProcessId) { EnumData data; data.dwProcessId = dwProcessId; data.hWnd = NULL; // ¸ðµç ÃÖ»óÀ§ À©µµ¿ì¸¦ ¿­°ÅÇϸç Äݹé ÇÔ¼ö(EnumWindowsProc)¸¦ ½ÇÇàÇÕ´Ï´Ù. EnumWindows(EnumWindowsProc, (LPARAM)&data); return data.hWnd; } /** * @brief ÁöÁ¤µÈ ±æÀÌÀÇ ·£´ý ¹®ÀÚ¿­ »ý¼º * @return "123456678" Çü½ÄÀÇ std::wstring */ std::wstring UtilGenerateRandomString(int length) { // ·£´ý ¹®ÀÚ¿­¿¡ »ç¿ëÇÒ ¹®ÀÚ ÁýÇÕ (¾ËÆÄºª ´ë¼Ò¹®ÀÚ + ¼ýÀÚ) const std::wstring charset = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // °íǰÁúÀÇ ºñ°áÁ¤Àû ½Ãµå(seed)¸¦ »ý¼ºÇÕ´Ï´Ù. // std::time(0)º¸´Ù ¿¹Ãø °¡´É¼ºÀÌ ÈξÀ ³·½À´Ï´Ù. std::mt19937 rng(static_cast( std::chrono::steady_clock::now().time_since_epoch().count() )); // 0ºÎÅÍ (¹®ÀÚ ÁýÇÕÀÇ Å©±â - 1) »çÀÌÀÇ ±ÕÀÏÇÑ ºÐÆ÷¸¦ ¼³Á¤ÇÕ´Ï´Ù. std::uniform_int_distribution distribution(0, charset.length() - 1); std::wstring result; result.reserve(length); // ¼º´ÉÀ» À§ÇØ ¹Ì¸® ¸Þ¸ð¸®¸¦ ÇÒ´çÇÕ´Ï´Ù. for (int i = 0; i < length; ++i) { result += charset[distribution(rng)]; } return result; } /** * @brief ÁöÁ¤µÈ À¯´ÏÄÚµå °æ·ÎÀÇ ¸ðµç Æú´õ¸¦ ¼øÂ÷ÀûÀ¸·Î »ý¼ºÇÕ´Ï´Ù. (Windows Àü¿ë) * ¿¹: C:\Temp\»ç¿ëÀÚ\Å×½ºÆ® °æ·Î ÀÔ·Â ½Ã Temp, »ç¿ëÀÚ, Å×½ºÆ® Æú´õ¸¦ Â÷·Ê·Î »ý¼ºÇÕ´Ï´Ù. * @param path »ý¼ºÇÒ Àüü µð·ºÅ丮 °æ·Î (wstring) ÀÔ´Ï´Ù. * @return true ¸ðµç µð·ºÅ丮 »ý¼º¿¡ ¼º°øÇ߰ųª ÀÌ¹Ì Á¸ÀçÇÏ´Â °æ¿ì. * @return false µð·ºÅ丮 »ý¼º Áß ½ÇÆÐÇ߰ųª, °æ·Î¿¡ °°Àº À̸§ÀÇ ÆÄÀÏÀÌ ÀÖ´Â °æ¿ì. */ bool CreateNestedDirectories(const std::wstring& path) { wchar_t full_path[MAX_PATH]; if (!GetFullPathNameW(path.c_str(), MAX_PATH, full_path, nullptr)) { std::wcerr << L"¿¡·¯: À¯È¿ÇÏÁö ¾ÊÀº °æ·ÎÀÔ´Ï´Ù. Code: " << GetLastError() << std::endl; return false; } wchar_t* p = wcschr(full_path, L'\\'); if (p == nullptr) { return true; } p++; while (p != nullptr && *p != L'\0') { p = wcschr(p, L'\\'); if (p != nullptr) { *p = L'\0'; // ¿ÍÀ̵å NULL ¹®ÀÚ·Î ¹®ÀÚ¿­ Àӽà Á¾·á } DWORD attributes = GetFileAttributesW(full_path); if (attributes == INVALID_FILE_ATTRIBUTES) { if (!CreateDirectoryW(full_path, NULL)) { std::wcerr << L"¿¡·¯: '" << full_path << L"' µð·ºÅ丮 »ý¼º¿¡ ½ÇÆÐÇß½À´Ï´Ù. Code: " << GetLastError() << std::endl; if (p != nullptr) *p = L'\\'; // ¹®ÀÚ¿­ º¹¿ø return false; } std::wcout << L"»ý¼ºµÊ: " << full_path << std::endl; } else if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) { std::wcerr << L"¿¡·¯: '" << full_path << L"' À̸§À¸·Î ÆÄÀÏÀÌ ÀÌ¹Ì Á¸ÀçÇÕ´Ï´Ù." << std::endl; if (p != nullptr) *p = L'\\'; // ¹®ÀÚ¿­ º¹¿ø return false; } if (p != nullptr) { *p = L'\\'; // Àß¶ú´ø ¹®ÀÚ¿­ º¹¿ø p++; } } return true; } /** * @brief ÁöÁ¤µÈ À̸§ÀÇ ÇÁ·Î¼¼½º¸¦ ¸ðµÎ ã¾Æ °­Á¦ Á¾·áÇÕ´Ï´Ù. * @param processName Á¾·áÇÒ ÇÁ·Î¼¼½ºÀÇ À̸§ (¿¹: L"notepad.exe") * @return Á¾·á¸¦ ½ÃµµÇÑ ÇÁ·Î¼¼½º°¡ Çϳª¶óµµ ÀÖÀ¸¸é true, ÇØ´ç À̸§ÀÇ ÇÁ·Î¼¼½º¸¦ ãÁö ¸øÇßÀ¸¸é false¸¦ ¹ÝȯÇÕ´Ï´Ù. */ bool UtilTerminateProcessByName(const std::wstring& processName, uint32_t pid) { // ÇöÀç ½Ã½ºÅÛÀÇ ÇÁ·Î¼¼½º ½º³À¼¦À» °¡Á®¿É´Ï´Ù. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapShot == INVALID_HANDLE_VALUE) { std::wcerr << L"CreateToolhelp32Snapshot failed. Error: " << GetLastError() << std::endl; return false; } PROCESSENTRY32W pe; // ÇÁ·Î¼¼½º Á¤º¸¸¦ ´ãÀ» ±¸Á¶Ã¼ pe.dwSize = sizeof(PROCESSENTRY32W); bool bProcessFound = false; // ù ¹øÂ° ÇÁ·Î¼¼½º Á¤º¸¸¦ °¡Á®¿É´Ï´Ù. if (Process32FirstW(hSnapShot, &pe)) { do { // ÇÁ·Î¼¼½º À̸§À» ´ë¼Ò¹®ÀÚ ±¸ºÐ ¾øÀÌ ºñ±³ÇÕ´Ï´Ù. if (_wcsicmp(pe.szExeFile, processName.c_str()) == 0 && pe.th32ProcessID != pid) { bProcessFound = true; HANDLE hProcess = NULL; // Á¾·á ±ÇÇÑÀ» °¡Áö°í ÇÁ·Î¼¼½º ÇÚµéÀ» ¿±´Ï´Ù. hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe.th32ProcessID); if (hProcess != NULL) { // ÇÁ·Î¼¼½º¸¦ °­Á¦ Á¾·áÇÕ´Ï´Ù. (Á¾·á ÄÚµå: 1) if (TerminateProcess(hProcess, 1)) { std::wcout << L"Process " << processName << L" (PID: " << pe.th32ProcessID << L") terminated." << std::endl; } else { std::wcerr << L"Failed to terminate process " << processName << L" (PID: " << pe.th32ProcessID << L"). Error: " << GetLastError() << std::endl; } CloseHandle(hProcess); // ÇÚµéÀ» ²À ´Ý¾ÆÁÝ´Ï´Ù. } else { std::wcerr << L"Failed to open process " << processName << L" (PID: " << pe.th32ProcessID << L"). Error: " << GetLastError() << std::endl; } } } while (Process32NextW(hSnapShot, &pe)); // ´ÙÀ½ ÇÁ·Î¼¼½º Á¤º¸¸¦ °¡Á®¿É´Ï´Ù. } CloseHandle(hSnapShot); // ½º³À¼¦ ÇÚµéÀ» ²À ´Ý¾ÆÁÝ´Ï´Ù. if (!bProcessFound) { std::wcout << L"Process " << processName << L" not found." << std::endl; } return bProcessFound; } #include std::wstring UtilGetCommandLineArgment1() { std::wstring commandLine = L""; LPWSTR cmdLine = GetCommandLineW(); if (!cmdLine) { return L""; } int numArgs = 0; LPWSTR* argList = CommandLineToArgvW(cmdLine, &numArgs); if (!argList) { return L""; } if (numArgs > 1) { commandLine = argList[1]; } LocalFree(argList); return commandLine; } BOOL WINAPI NtNameToDosNameW(LPWSTR dosName, SIZE_T size, LPWSTR ntName) { WCHAR drives[1024] = { 0, }; WCHAR deviceName[1024] = { 0, }; WCHAR* name = drives, tmp[3]; int n = GetLogicalDriveStringsW(sizeof(drives), drives); if (n >= sizeof(drives) || n == 0) { return FALSE; } while (*name) { Sleep(1); tmp[0] = name[0]; tmp[1] = name[1]; tmp[2] = L'\0'; if (!QueryDosDeviceW(tmp, deviceName, sizeof(deviceName))) continue; if (wcsncmp(deviceName, ntName, lstrlenW(deviceName)) == 0) { StringCbCopyW(dosName, size, tmp); StringCbCatW(dosName, size, ntName + lstrlenW(deviceName)); return TRUE; } name += lstrlenW(name) + 1; } return FALSE; }