void CFunctionProfiler::GetRuntimeFullMethodSignature(FunctionID funcID, COR_PRF_FRAME_INFO func, std::wstring& methodName) { ClassID funcClass; CComPtr<IMetaDataImport2> metaDataImport2; mdToken funcToken; GetClassIDForFunctionID(funcID, func, funcClass, funcToken, &metaDataImport2); std::wstring className; CFunctionProfiler::g_pProfiler->GetRuntimeClassSignature(funcClass, className); CFunctionProfiler::g_pProfiler->GetRuntimeMethodSignature(funcID, NULL, metaDataImport2, methodName); methodName.insert(0, L"::"); methodName.insert(0, className); }
std::wstring getAppDataPath(std::wstring extra) { #ifdef NIX #if defined(USE_XDG_DIRS) std::string configPath = getenv("XDG_CONFIG_HOME"); configPath.append("/desura"); #elif defined(USE_SINGLE_HOME_DIR) std::string configPath = getenv("HOME"); configPath.append("/.desura"); #elif defined(USE_PORTABLE_DIR) std::string configPath = UTIL::STRING::toStr(getCurrentDir(L"config")); #endif if (extra.size() > 0) extra.insert(0, DIRS_WSTR); return UTIL::STRING::toWStr(configPath) + extra; #else wchar_t path[MAX_PATH]; getSystemPath(CSIDL_COMMON_APPDATA, path); gcWString out(path); out += DIRS_WSTR; out += COMMONAPP_PATH_W; if (extra.size() > 0) { out += DIRS_WSTR; out += extra; } return out; #endif }
//Get infomation of service size_t __stdcall GetServiceInfo() { //Get service information SC_HANDLE SCM = nullptr, Service = nullptr; DWORD nResumeHandle = 0; if((SCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == nullptr) return EXIT_FAILURE; Service = OpenService(SCM, LOCALSERVERNAME, SERVICE_ALL_ACCESS); if(Service == nullptr) return EXIT_FAILURE; LPQUERY_SERVICE_CONFIG ServicesInfo = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, PACKET_MAXSIZE*4); //Max buffer of QueryServiceConfig() is 8KB/8192 Bytes. if (ServicesInfo == nullptr) return EXIT_FAILURE; if (QueryServiceConfig(Service, ServicesInfo, PACKET_MAXSIZE*4, &nResumeHandle) == FALSE) { LocalFree(ServicesInfo); return EXIT_FAILURE; } Path = ServicesInfo->lpBinaryPathName; LocalFree(ServicesInfo); //Path process size_t Index = Path.rfind(_T("\\")); static const WCHAR wBackslash[] = _T("\\"); Path.erase(Index + 1, Path.length()); for (Index = 0;Index < Path.length();Index++) { if (Path[Index] == _T('\\')) { Path.insert(Index, wBackslash); Index++; } } //Get path of error log file and delete the old one ErrorLogPath.append(Path); ErrorLogPath.append(_T("Error.log")); DeleteFile(ErrorLogPath.c_str()); Parameter.PrintError = true; //Winsock initialization WSADATA WSAData = {0}; if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0 || LOBYTE(WSAData.wVersion) != 2 || HIBYTE(WSAData.wVersion) != 2) { PrintError(Winsock_Error, _T("Winsock initialization failed"), WSAGetLastError(), NULL); WSACleanup(); return EXIT_FAILURE; } return EXIT_SUCCESS; }
//Get path of program from the main function parameter and Winsock initialization inline size_t __fastcall FileInit(const PWSTR wPath) { /* Get path of program from server information. //Prepare SC_HANDLE SCM = nullptr, Service = nullptr; DWORD nResumeHandle = 0; if ((SCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == nullptr) return EXIT_FAILURE; Service = OpenService(SCM, LOCAL_SERVICENAME, SERVICE_ALL_ACCESS); if (Service == nullptr) return EXIT_FAILURE; LPQUERY_SERVICE_CONFIG ServicesInfo = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, QUERY_SERVICE_CONFIG_BUFFER_MAXSIZE); if (ServicesInfo == nullptr) return EXIT_FAILURE; if (QueryServiceConfig(Service, ServicesInfo, QUERY_SERVICE_CONFIG_BUFFER_MAXSIZE, &nResumeHandle) == FALSE) { LocalFree(ServicesInfo); return EXIT_FAILURE; } Path = ServicesInfo->lpBinaryPathName; LocalFree(ServicesInfo); */ //Path process. Path = wPath; Path.erase(Path.rfind(L"\\") + 1U); for (size_t Index = 0;Index < Path.length();Index++) { if (Path[Index] == L'\\') { Path.insert(Index, L"\\"); Index++; } } //Get path of error log file and delete the old one. ErrorLogPath = Path; ErrorLogPath.append(L"Error.log"); DeleteFileW(ErrorLogPath.c_str()); Parameter.PrintError = true; //Winsock initialization WSADATA WSAInitialization = {0}; if (WSAStartup(MAKEWORD(2, 2), &WSAInitialization) != 0 || LOBYTE(WSAInitialization.wVersion) != 2 || HIBYTE(WSAInitialization.wVersion) != 2) { PrintError(WINSOCK_ERROR, L"Winsock initialization error", WSAGetLastError(), nullptr, NULL); WSACleanup(); return EXIT_FAILURE; } return EXIT_SUCCESS; }
/* ** Escapes reserved PCRE regex metacharacters. */ void EscapeRegExp(std::wstring& str) { size_t start = 0; while ((start = str.find_first_of(L"\\^$|()[{.+*?", start)) != std::wstring::npos) { str.insert(start, L"\\"); start += 2; } }
bool CWIN32Util::AddExtraLongPathPrefix(std::wstring& path) { const wchar_t* const str = path.c_str(); if (path.length() < 4 || str[0] != L'\\' || str[1] != L'\\' || str[3] != L'\\' || str[2] != L'?') { path.insert(0, L"\\\\?\\"); return true; } return false; }
void text::replace(std::wstring &str, const std::wstring &find, const std::wstring &replace) { std::wstring::size_type pos=0; while((pos=str.find(find, pos))!=std::wstring::npos) { str.erase(pos, find.length()); str.insert(pos, replace); pos+=replace.length(); } }
size_t Launch(std::wstring FullPathToExe, std::wstring Parameters, size_t SecondsToWait) { size_t iMyCounter = 0, iReturnVal = 0, iPos = 0; DWORD dwExitCode = 0; std::wstring sTempStr = L""; /* Add a space to the beginning of the Parameters */ if (Parameters.size() != 0) { if (Parameters[0] != L' ') { Parameters.insert(0,L" "); } } /* The first parameter needs to be the exe itself */ sTempStr = FullPathToExe; iPos = sTempStr.find_last_of(L"\\"); sTempStr.erase(0, iPos +1); Parameters = sTempStr.append(Parameters); /* CreateProcessW can modify Parameters thus we allocate needed memory */ wchar_t * pwszParam = new wchar_t[Parameters.size() + 1]; if (pwszParam == 0) return 1; const wchar_t* pchrTemp = Parameters.c_str(); wcscpy_s(pwszParam, Parameters.size() + 1, pchrTemp); /* CreateProcess API initialization */ STARTUPINFOW siStartupInfo; PROCESS_INFORMATION piProcessInfo; memset(&siStartupInfo, 0, sizeof(siStartupInfo)); memset(&piProcessInfo, 0, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); if (CreateProcessW(const_cast<LPCWSTR>(FullPathToExe.c_str()), pwszParam, 0, 0, false, CREATE_NO_WINDOW, 0, 0, &siStartupInfo, &piProcessInfo) != false) /* Watch the process. */ dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait * 1000)); else /* CreateProcess failed */ iReturnVal = GetLastError(); /* Free memory */ delete[]pwszParam; pwszParam = 0; /* Release handles */ CloseHandle(piProcessInfo.hProcess); CloseHandle(piProcessInfo.hThread); return iReturnVal; }
/////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// // // // Add the specified char to the left of a string until // its length reaches the desidered value // // /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// std::wstring dataHandlerDateTimeBase::padLeft(std::wstring source, std::wstring fillChar, size_t length) { PUNTOEXE_FUNCTION_START(L"dataHandlerDateTimeBase::padLeft"); while(source.size() < length) { source.insert(0, fillChar); } return source; PUNTOEXE_FUNCTION_END(); }
/* ** Escapes reserved URL characters. */ void EncodeUrl(std::wstring& str) { size_t pos = 0; while ((pos = str.find_first_of(L" !*'();:@&=+$,/?#[]", pos)) != std::wstring::npos) { WCHAR buffer[3]; _snwprintf_s(buffer, _countof(buffer), L"%.2X", str[pos]); str[pos] = L'%'; str.insert(pos + 1, buffer); pos += 3; } }
void RecipientsHandler::ReplaceRecipientInString( const std::wstring& recipient, const int recipientNumber, const RecipientMatch& recipientMatch, std::wstring& recipients) { int currentRecipientNumber = 1; // exclude the first recipient - it should start searching from zero std::string::size_type startOfSearchIndex = 0; while(recipientNumber > currentRecipientNumber) { startOfSearchIndex = recipients.find_first_of(L",;", startOfSearchIndex); currentRecipientNumber++; } std::string::size_type startReplaceIndex = recipients.find(recipient, startOfSearchIndex); if(std::string::npos == startReplaceIndex) return; std::string::size_type endReplaceIndex = recipients.find_first_of(L",;", startReplaceIndex); if(std::string::npos == endReplaceIndex) endReplaceIndex = recipients.size(); recipients = recipients.erase(startReplaceIndex, endReplaceIndex - startReplaceIndex); std::wstring emailAddress; if (recipientMatch.GetMailSystem() == AbstractRecipient::NOTES_MAILSYSTEM && !recipientMatch.GetFullName().empty()) { emailAddress = recipientMatch.GetFullName(); } else if(!recipientMatch.GetInternetAddress().empty()) { emailAddress = recipientMatch.GetInternetAddress(); } else if(!recipientMatch.GetMailAddress().empty()) { emailAddress = recipientMatch.GetMailAddress(); } if(!emailAddress.empty()) { std::wostringstream msg; msg << "Replacing recipient [" << recipient.c_str() << "] with [" << emailAddress.c_str() << "]." << std::ends; LOG_WS_INFO(msg.str().c_str()); recipients = recipients.insert(startReplaceIndex, emailAddress.c_str()); return; } LOG_WS_ERROR(L"Error: Cannot replace recipient with empty value!"); }
extern "C" const WCHAR *EncodeWithNickname(const char *string, const WCHAR *szNick, UINT codePage) { static std::wstring msg; wchar_t stringW[256]; int mark = 0; MultiByteToWideChar(codePage, 0, string, -1, stringW, 256); stringW[255] = 0; msg.assign(stringW); if((mark = msg.find(L"%nick%")) != msg.npos) { msg.erase(mark, 6); msg.insert(mark, szNick, lstrlenW(szNick)); } return msg.c_str(); }
void insertText(const std::wstring& newText) { // Stop IME composition. composition.clear(); // Delete (overwrite) previous selection. if (caretPos != selectionStart) { unsigned min = std::min(caretPos, selectionStart); unsigned max = std::max(caretPos, selectionStart); text.erase(text.begin() + min, text.begin() + max); caretPos = selectionStart = min; } text.insert(text.begin() + caretPos, newText.begin(), newText.end()); caretPos += newText.size(); selectionStart = caretPos; }
std::wstring getAppInstallPath(std::wstring extra) { #ifdef NIX #if defined(USE_XDG_DIRS) std::string installPath = getenv("XDG_DATA_HOME"); installPath.append("/desura"); #elif defined(USE_SINGLE_HOME_DIR) std::string installPath = getenv("HOME"); installPath.append("/.desura/games"); #elif defined(USE_PORTABLE_DIR) std::string installPath = UTIL::STRING::toStr(getCurrentDir(L"games")); #endif if (extra.size() > 0) extra.insert(0, DIRS_WSTR); return UTIL::STRING::toWStr(installPath) + extra; #else #error NOT IMPLEMENTED #endif }
std::wstring getCachePath(std::wstring extra) { #ifdef NIX #if defined(USE_XDG_DIRS) std::string cachePath = getenv("XDG_CACHE_HOME"); cachePath.append("/desura"); #elif defined(USE_SINGLE_HOME_DIR) std::string cachePath = getenv("HOME"); cachePath.append("/.desura/cache"); #elif defined(USE_PORTABLE_DIR) std::string cachePath = UTIL::STRING::toStr(getCurrentDir(L"cache")); #endif if (extra.size() > 0) extra.insert(0, DIRS_WSTR); return UTIL::STRING::toWStr(cachePath) + extra; #else return L""; // #error NOT IMPLEMENTED #endif }
// create strings const wchar_t* CreateHelpString(const char *pszStringID) { static std::wstring wsTmp; wsTmp = LoadString(pszStringID); uint32 nFirst = wsTmp.find_first_of(L"|"); while (nFirst != std::wstring::npos) { uint32 nNext = wsTmp.find_first_of(L"|", nFirst+1); if (nNext != std::wstring::npos && nNext) { uint32 nLen = nNext-nFirst; std::wstring wsAction; if (nLen >= 2) { wsAction = wsTmp.substr(nFirst+1,nLen-1); } wsTmp.erase(nFirst,nLen+1); CUserProfile *pProfile = g_pProfileMgr->GetCurrentProfile(); int nCommand = GetCommandID( MPW2A(wsAction.c_str()).c_str() ); wchar_t szTriggerName[32] = L""; LTStrCpy(szTriggerName,pProfile->GetTriggerNameFromCommandID(nCommand),LTARRAYSIZE(szTriggerName)); if (LTStrEmpty(szTriggerName)) { LTStrCpy(szTriggerName,LoadString("Control_Unassigned"),LTARRAYSIZE(szTriggerName)); } wsTmp.insert(nFirst,szTriggerName); } nFirst = wsTmp.find_first_of(L"|"); } return wsTmp.c_str(); }
// --------------------------------------------------------------------------- void vspPackageWrapper::outputBuildMessage(std::wstring& messageString, bool outputOnlyToLog, std::wstring filePathAndName, int line) { // Add to log: VSCORE(vscPrintDebugMsgToDebugLog)(messageString.c_str()); if (!outputOnlyToLog) { IVsOutputWindowPane* spOutputWindowPane = getBuildPane(); if (spOutputWindowPane != NULL) { HRESULT rc; rc = spOutputWindowPane->Activate(); VSP_ASSERT(rc == S_OK); vspDTEConnector::instance().ShowOutputWindow(); // Add CodeXL prefix to the message (so that the message is identified in the output pane as CodeXL's message) // handle a case where the string begins with \n\n\n\n- we don't want to have many "CodeXL-" with no messege std::size_t found1 = messageString.find(VS_STR_NewLine); if (found1 == 0) { for (;;) { std::size_t found2 = messageString.find(VS_STR_NewLine, found1 + 2); if ((found2 != std::string::npos) && (found1 + 2) == found2) { found1 = found2; continue; } else { break; } } messageString.insert(found1 + 1, VS_STR_CodeXLPrefix); } else { messageString.insert(0, VS_STR_CodeXLPrefix); } // when we have a message that's few lines, align lines with codeXL prefix size std::size_t found = messageString.find(VS_STR_NewLine); std::size_t lastPos = messageString.length() - 1; while (found > 0 && found < lastPos) { messageString.insert(found + 1, VS_STR_CodeXLPrefixIndentation); found = messageString.find(VS_STR_NewLine, found + 1); lastPos = messageString.length() - 1; } // verify message ends with new line if (lastPos < messageString.length() && (messageString.compare(lastPos, 1, VS_STR_NewLine) != 0)) { messageString += VS_STR_NewLine; } // Add the message to the debug pane: BSTR oleMessageString = SysAllocString(messageString.c_str()); if (!filePathAndName.empty()) { // message is of the format PATH_TO_FILE\file.cl, line 123:message and will be connected to the actual file and line number // connect message to it's location: file and line HRESULT res = spOutputWindowPane->OutputTaskItemString(oleMessageString, TP_NORMAL, CAT_BUILDCOMPILE, L"", BMP_COMPILE, filePathAndName.c_str(), line, L""); VSP_ASSERT(res == S_OK); res = spOutputWindowPane->FlushToTaskList(); VSP_ASSERT(res == S_OK); } else { spOutputWindowPane->OutputString(oleMessageString); } SysFreeString(oleMessageString); // Reduce the reference count spOutputWindowPane->Release(); } } }
size_t ExecuteProcess(_EXCEPTION_POINTERS *pep,std::wstring FullPathToExe, std::wstring Parameters, size_t SecondsToWait) { size_t iMyCounter = 0, iReturnVal = 0, iPos = 0; DWORD dwExitCode = 0; std::wstring sTempStr = L""; /* - NOTE - You should check here to see if the exe even exists */ /* Add a space to the beginning of the Parameters */ if (Parameters.size() != 0) { if (Parameters[0] != L' ') { Parameters.insert(0,L" "); } } /* The first parameter needs to be the exe itself */ sTempStr = FullPathToExe; iPos = sTempStr.find_last_of(L"\\"); sTempStr.erase(0, iPos +1); Parameters = sTempStr.append(Parameters); /* CreateProcessW can modify Parameters thus we allocate needed memory */ wchar_t * pwszParam = new wchar_t[Parameters.size() + 1]; if (pwszParam == 0) { return 1; } const wchar_t* pchrTemp = Parameters.c_str(); wcscpy_s(pwszParam, Parameters.size() + 1, pchrTemp); /* CreateProcess API initialization */ STARTUPINFOW siStartupInfo; PROCESS_INFORMATION piProcessInfo; memset(&siStartupInfo, 0, sizeof(siStartupInfo)); memset(&piProcessInfo, 0, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); CreatePipe(); if (CreateProcessW(const_cast<LPCWSTR>(FullPathToExe.c_str()), pwszParam, 0, 0, false, CREATE_DEFAULT_ERROR_MODE /*| CREATE_NEW_CONSOLE | CREATE_NO_WINDOW*/, 0, 0, &siStartupInfo, &piProcessInfo) != false) { // Wait for the process to end //printf("Creating pipe and sending message...\n"); WriteIntoPipe(pep, GetCurrentThreadId()); //printf("Done with pipe creation.\n"); //printf("ExecutionHandler: Waiting for the process to end...\n"); dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait * 1000)); //printf("ExecutionHandler: Process ended (or timeout met)\n"); } else { // Create Process failed - oh no... // TODO: Create a log file describing this iReturnVal = GetLastError(); } delete[]pwszParam; pwszParam = 0; CloseHandle(piProcessInfo.hProcess); CloseHandle(piProcessInfo.hThread); return iReturnVal; }