HRESULT UiTreeWalk::AppendUiAttributes(long left, long top, IUIAutomationElement* pCurUia, long nPos, std::wstring& wstr) { SAFEARRAY *rid; REQUIRE_SUCCESS_HR(pCurUia->GetRuntimeId(&rid)); LONG lbound; REQUIRE_SUCCESS_HR(SafeArrayGetLBound(rid, 1, &lbound)); LONG ubound; REQUIRE_SUCCESS_HR(SafeArrayGetUBound(rid, 1, &ubound)); CComBSTR bstrRuntimeId; LONG runtimeId = 0; WCHAR temp[16]; for (LONG i = lbound; i <= ubound; i++) { REQUIRE_SUCCESS_HR(SafeArrayGetElement(rid, &i, &runtimeId)); _ltow_s(runtimeId, temp, 10); REQUIRE_SUCCESS_HR(bstrRuntimeId.Append(temp)); if (i < ubound) { REQUIRE_SUCCESS_HR(bstrRuntimeId.Append(L".")); } } REQUIRE_SUCCESS_HR(SafeArrayDestroy(rid)); if (bstrRuntimeId == NULL) { wsprintf(temp, L"%lu", reinterpret_cast<std::uintptr_t>(pCurUia)); bstrRuntimeId = temp; } CComPtr<IUIAutomationElement> spUia(pCurUia); auto it = cachedAutoElements.find(bstrRuntimeId); if (it == cachedAutoElements.end()) { cachedAutoElements.insert(std::make_pair(bstrRuntimeId, spUia)); } else { it->second = spUia; } CComBSTR bstrClass; REQUIRE_SUCCESS_HR(pCurUia->get_CurrentClassName(&bstrClass)); bstrClass = bstrClass == NULL ? L"" : bstrClass; std::wstring shortClass(bstrClass, SysStringLen(bstrClass)); bool bStartWith = XmlEncode(shortClass, MaxNameLength); if (bStartWith == true || shortClass.length() >= MaxNameLength) { shortClass.insert(0, L"starts-with:"); } CComBSTR bstrName; REQUIRE_SUCCESS_HR(pCurUia->get_CurrentName(&bstrName)); bstrName = bstrName == NULL ? L"" : bstrName; std::wstring shortName(bstrName, SysStringLen(bstrName)); bStartWith = XmlEncode(shortName, MaxNameLength); if (bStartWith == true || shortName.length() >= MaxNameLength) { shortName.insert(0, L"starts-with:"); } CComBSTR bstrCtrlType; REQUIRE_SUCCESS_HR(pCurUia->get_CurrentLocalizedControlType(&bstrCtrlType)); bstrCtrlType = bstrCtrlType == NULL ? L"" : bstrCtrlType; CONTROLTYPEID cid; REQUIRE_SUCCESS_HR(pCurUia->get_CurrentControlType(&cid)); CComBSTR bstrProgrammaticName; if (cid >= UIA_ButtonControlTypeId && UIA_ButtonControlTypeId <= UIA_AppBarControlTypeId) { REQUIRE_SUCCESS_HR(bstrProgrammaticName.Append(gc_controlTypesTable[cid - UIA_ButtonControlTypeId].pszName)); } // CurrentLocalizedControlType can be empty: Cortana set reminder Time button's parent if (bstrProgrammaticName.Length() == 0) { bstrProgrammaticName = L"Unknown"; } CComBSTR bstrAutoId; REQUIRE_SUCCESS_HR(pCurUia->get_CurrentAutomationId(&bstrAutoId)); bstrAutoId = bstrAutoId == NULL ? L"" : bstrAutoId; std::wstring shortAutoId(bstrAutoId, SysStringLen(bstrAutoId)); bStartWith = XmlEncode(shortAutoId, MaxNameLength); if (bStartWith == true || shortAutoId.length() >= MaxNameLength) { shortAutoId.insert(0, L"starts-with:"); } RECT rect; REQUIRE_SUCCESS_HR(pCurUia->get_CurrentBoundingRectangle(&rect)); WCHAR chPos[16]; if (nPos <= 0) { wsprintf(chPos, L""); } else { wsprintf(chPos, L"%d", nPos + 1); // xpath index starts at 1 } wsprintf(UiTreeWalk::s_chBuffer, c_chNodeFormat, bstrProgrammaticName.m_str, bstrCtrlType.m_str, shortClass.c_str(), shortName.c_str(), shortAutoId.c_str(), rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, left - rect.left, top - rect.top, chPos, bstrRuntimeId.m_str); if (wcslen(UiTreeWalk::s_chBuffer) > 0) { wstr.append(UiTreeWalk::s_chBuffer); } return S_OK; }
long Cx_CfgRecord::SubmitRecord() { if (m_arrValue.empty()) return 0; std::vector<FieldValue> arrValue(m_arrValue); std::wostringstream sql, ssField, ssValues; m_arrValue.clear(); if (m_bAdd) { ASSERT(DbFunc::IsDBName(m_wstrSQL.c_str())); sql << L"INSERT INTO " << m_wstrSQL << L" ("; ssValues << L") VALUES ("; } else { ASSERT(StrStrIW(m_wstrSQL.c_str(), L"SELECT ") == m_wstrSQL.c_str()); const wchar_t* pszFrom = StrStrIW(m_wstrSQL.c_str(), L"FROM "); ASSERT_MESSAGE(pszFrom != NULL, "The SQL command must contains 'FROM' keyword."); std::wstring table(DbFunc::GetLevel1Name(pszFrom + 5)); sql << L"UPDATE " << table; ssValues << L" SET "; } std::vector<FieldValue>::iterator it = arrValue.begin(); for (long i = 0; it != arrValue.end(); ++it, ++i) { std::wstring wstrField(it->first); std::wstring wstrValue(it->second); ASSERT(!wstrValue.empty()); if (StrCmpW(L"@NEWID", wstrValue.c_str()) == 0) { ASSERT(m_bAdd); m_wstrKeyField = wstrField; m_nKeyNewID = 0; if (!m_pDB->GetRecordNewID(m_nKeyNewID, m_wstrSQL, wstrField)) { return 0; } wchar_t szNum[35]; _ltow_s(m_nKeyNewID, szNum, _countof(szNum), 10); wstrValue = szNum; } else if (L'@' == wstrValue[0]) { wstrValue = GetSQLFunc(wstrValue.c_str() + 1, m_pDB->GetSQLParser()); } if (i > 0) { ssField << L","; ssValues << L","; } if (m_bAdd) { ssField << wstrField; ssValues << wstrValue; } else { ssValues << wstrField << L"=" << wstrValue; } } if (m_bAdd) { sql << ssField.str() << ssValues.str() << L") "; } else { sql << ssValues.str(); const wchar_t* pszWhere = StrStrIW(m_wstrSQL.c_str(), L" WHERE"); if (pszWhere != NULL) { sql << pszWhere; } } bool bRet = false; long nAffected = 0; try { bRet = (m_pDB->ExecuteSQL(sql.str().c_str(), __FILE__, __LINE__) != NULL); nAffected = m_pDB->GetRecordsAffected(); } CATCH_DB_STR_ERROR; return nAffected; }
int _tmain(int argc, _TCHAR* argv[]) { CommandLineArguments args(argc, argv); vector<TCHAR> temp_file_name_buffer(MAX_PATH); vector<TCHAR> temp_path_buffer(MAX_PATH); // Gets the temp path env string (no guarantee it's a valid path). unsigned long temp_path_length = ::GetTempPath(MAX_PATH, &temp_path_buffer[0]); unsigned int error_code = ::GetTempFileName(&temp_path_buffer[0], TEMP_FILE_PREFIX, 0, &temp_file_name_buffer[0]); std::wstring temp_file_name(&temp_file_name_buffer[0]); if (!ExtractResource(IDR_DRIVER_LIBRARY, temp_file_name)) { std::wcout << L"Failed to extract the library to temp directory: " << temp_file_name; return ERR_DLL_EXTRACT_FAIL; } HMODULE module_handle = ::LoadLibrary(temp_file_name.c_str()); if (module_handle == NULL) { std::wcout << L"Failed to load the library from temp directory: " << temp_file_name; return ERR_DLL_LOAD_FAIL; } STARTSERVEREXPROC start_server_ex_proc = reinterpret_cast<STARTSERVEREXPROC>( ::GetProcAddress(module_handle, START_SERVER_EX_API_NAME)); STOPSERVERPROC stop_server_proc = reinterpret_cast<STOPSERVERPROC>( ::GetProcAddress(module_handle, STOP_SERVER_API_NAME)); if (start_server_ex_proc == NULL || stop_server_proc == NULL) { std::wcout << L"Could not find entry point in extracted library: " << temp_file_name; return ERR_FUNCTION_NOT_FOUND; } int port = atoi(args.GetValue(PORT_COMMAND_LINE_ARG, "5555").c_str()); std::string host_address = args.GetValue(HOST_COMMAND_LINE_ARG, ""); std::string log_level = args.GetValue(LOGLEVEL_COMMAND_LINE_ARG, ""); std::string log_file = args.GetValue(LOGFILE_COMMAND_LINE_ARG, ""); void* server_value = start_server_ex_proc(port, host_address, log_level, log_file); if (server_value == NULL) { std::cout << L"Failed to start the server with: " << L"port = '" << port << "', " << L"host = '" << host_address << "', " << L"log level = '" << log_level << "', " << L"log file = '" << log_file << "'"; ; return ERR_SERVER_START; } std::cout << "Started InternetExplorerDriver server" << " (" << GetProcessArchitectureDescription() << ")" << std::endl; std::cout << GetExecutableVersion() << std::endl; std::cout << "Listening on port " << port << std::endl; if (host_address.size() > 0) { std::cout << "Bound to network adapter with IP address " << host_address << std::endl; } if (log_level.size() > 0) { std::cout << "Log level is set to " << log_level << std::endl; } if (log_file.size() > 0) { std::cout << "Log file is set to " << log_file << std::endl; } // Create the shutdown event and wait for it to be signaled. DWORD process_id = ::GetCurrentProcessId(); vector<wchar_t> process_id_buffer(10); _ltow_s(process_id, &process_id_buffer[0], process_id_buffer.size(), 10); std::wstring process_id_string(&process_id_buffer[0]); std::wstring event_name = IESERVER_SHUTDOWN_EVENT_NAME + process_id_string; HANDLE event_handle = ::CreateEvent(NULL, TRUE, FALSE, event_name.c_str()); ::WaitForSingleObject(event_handle, INFINITE); ::CloseHandle(event_handle); stop_server_proc(); ::FreeLibrary(module_handle); ::DeleteFile(temp_file_name.c_str()); return 0; }
// // Log JIT method native load event to VTune // void VTuneChakraProfile::LogMethodNativeLoadEvent(Js::FunctionBody* body, Js::FunctionEntryPointInfo* entryPoint) { #if ENABLE_NATIVE_CODEGEN if (isJitProfilingActive) { iJIT_Method_Load methodInfo; memset(&methodInfo, 0, sizeof(iJIT_Method_Load)); const char16* methodName = body->GetExternalDisplayName(); // Append function line number info to method name so that VTune can distinguish between polymorphic methods char16 methodNameBuffer[_MAX_PATH]; ULONG lineNumber = body->GetLineNumber(); char16 numberBuffer[20]; _ltow_s(lineNumber, numberBuffer, 10); wcscpy_s(methodNameBuffer, methodName); if (entryPoint->GetJitMode() == ExecutionMode::SimpleJit) { wcscat_s(methodNameBuffer, _u(" Simple")); } wcscat_s(methodNameBuffer, _u(" {line:")); wcscat_s(methodNameBuffer, numberBuffer); wcscat_s(methodNameBuffer, _u("}")); size_t methodLength = wcslen(methodNameBuffer); Assert(methodLength < _MAX_PATH); size_t length = methodLength * 3 + 1; utf8char_t* utf8MethodName = HeapNewNoThrowArray(utf8char_t, length); if (utf8MethodName) { methodInfo.method_id = iJIT_GetNewMethodID(); utf8::EncodeIntoAndNullTerminate(utf8MethodName, methodNameBuffer, (charcount_t)methodLength); methodInfo.method_name = (char*)utf8MethodName; methodInfo.method_load_address = (void*)entryPoint->GetNativeAddress(); methodInfo.method_size = (uint)entryPoint->GetCodeSize(); // Size in memory - Must be exact LineNumberInfo numberInfo[1]; uint lineCount = (entryPoint->GetNativeOffsetMapCount()) * 2 + 1; // may need to record both .begin and .end for all elements LineNumberInfo* pLineInfo = HeapNewNoThrowArray(LineNumberInfo, lineCount); if (pLineInfo == NULL || Js::Configuration::Global.flags.DisableVTuneSourceLineInfo) { // resort to original implementation, attribute all samples to first line numberInfo[0].LineNumber = lineNumber; numberInfo[0].Offset = 0; methodInfo.line_number_size = 1; methodInfo.line_number_table = numberInfo; } else { int size = entryPoint->PopulateLineInfo(pLineInfo, body); methodInfo.line_number_size = size; methodInfo.line_number_table = pLineInfo; } size_t urlLength = 0; utf8char_t* utf8Url = GetUrl(body, &urlLength); methodInfo.source_file_name = (char*)utf8Url; OUTPUT_TRACE(Js::ProfilerPhase, _u("Method load event: %s\n"), methodNameBuffer); iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &methodInfo); HeapDeleteArray(lineCount, pLineInfo); if (urlLength > 0) { HeapDeleteArray(urlLength, utf8Url); } HeapDeleteArray(length, utf8MethodName); } } #endif }
bool Cx_CfgRecord::SetInt32(const wchar_t* pszEntry, long nValue) { wchar_t szNum[35]; _ltow_s(nValue, szNum, _countof(szNum), 10); return AddFieldValue(pszEntry, szNum); }
bool RegistryUtilities::GetRegistryValue(const HKEY root_key, const std::wstring& subkey, const std::wstring& value_name, std::wstring *value) { std::string root_key_description = "HKEY_CURRENT_USER"; if (root_key == HKEY_CLASSES_ROOT) { root_key_description = "HKEY_CLASSES_ROOT"; } else if (root_key == HKEY_LOCAL_MACHINE) { root_key_description = "HKEY_LOCAL_MACHINE"; } bool value_retrieved = false; DWORD required_buffer_size; DWORD value_type; HKEY key_handle; long registry_call_result = ::RegOpenKeyEx(root_key, subkey.c_str(), 0, KEY_QUERY_VALUE, &key_handle); if (ERROR_SUCCESS == registry_call_result) { registry_call_result = ::RegQueryValueEx(key_handle, value_name.c_str(), NULL, &value_type, NULL, &required_buffer_size); if (ERROR_SUCCESS == registry_call_result) { if (value_type == REG_SZ || value_type == REG_EXPAND_SZ || value_type == REG_MULTI_SZ) { std::vector<wchar_t> value_buffer(required_buffer_size); registry_call_result = ::RegQueryValueEx(key_handle, value_name.c_str(), NULL, &value_type, reinterpret_cast<LPBYTE>(&value_buffer[0]), &required_buffer_size); if (ERROR_SUCCESS == registry_call_result) { *value = &value_buffer[0]; value_retrieved = true; } } else if (value_type == REG_DWORD) { DWORD numeric_value = 0; registry_call_result = ::RegQueryValueEx(key_handle, value_name.c_str(), NULL, &value_type, reinterpret_cast<LPBYTE>(&numeric_value), &required_buffer_size); if (ERROR_SUCCESS == registry_call_result) { // Coerce the numeric value to a string to return back. // Assume 10 characters will be enough to hold the size // of the value. std::vector<wchar_t> numeric_value_buffer(10); _ltow_s(numeric_value, &numeric_value_buffer[0], numeric_value_buffer.size(), 10); *value = &numeric_value_buffer[0]; value_retrieved = true; } } else { LOG(WARN) << "Unexpected value type of " << value_type << " for RegQueryValueEx was found for value with name " << LOGWSTRING(value_name) << " in subkey " << LOGWSTRING(subkey) << " in hive " << root_key_description; } if (ERROR_SUCCESS != registry_call_result) { LOG(WARN) << "RegQueryValueEx failed with error code " << registry_call_result << " retrieving value with name " << LOGWSTRING(value_name) << " in subkey " << LOGWSTRING(subkey) << "in hive " << root_key_description; } } else { LOG(WARN) << "RegQueryValueEx failed with error code " << registry_call_result << " retrieving required buffer size for value with name " << LOGWSTRING(value_name) << " in subkey " << LOGWSTRING(subkey) << " in hive " << root_key_description; } ::RegCloseKey(key_handle); } else { LOG(WARN) << "RegOpenKeyEx failed with error code " << registry_call_result << " attempting to open subkey " << LOGWSTRING(subkey) << " in hive " << root_key_description; } return value_retrieved; }
//***************************************************************************** // Function: ValueToString // Purpose: Takes a variant, returns a string representation of that variant //***************************************************************************** WCHAR *ValueToString(CIMTYPE dwType, VARIANT *pValue, WCHAR **pbuf, WCHAR *fnHandler(VARIANT *pv)) { DWORD iTotBufSize; WCHAR *vbuf = NULL; WCHAR *buf = NULL; WCHAR lbuf[BLOCKSIZE]; switch (pValue->vt) { case VT_EMPTY: { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty>"); } break; } case VT_NULL: { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<null>"); } break; } case VT_BOOL: { VARIANT_BOOL b = pValue->boolVal; buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { if (!b) { StringCbCopyW(buf, BLOCKSIZE, L"FALSE"); } else { StringCbCopyW(buf, BLOCKSIZE, L"TRUE"); } } break; } case VT_I1: { char b = pValue->bVal; buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { if (b >= 32) { StringCbPrintfW(buf, BLOCKSIZE, L"'%c' (%hd, 0x%hX)", b, (signed char)b, b); } else { StringCbPrintfW(buf, BLOCKSIZE, L"%hd (0x%hX)", (signed char)b, b); } } break; } case VT_UI1: { unsigned char b = pValue->bVal; buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { if (b >= 32) { StringCbPrintfW(buf, BLOCKSIZE, L"'%c' (%hu, 0x%hX)", b, (unsigned char)b, b); } else { StringCbPrintfW(buf, BLOCKSIZE, L"%hu (0x%hX)", (unsigned char)b, b); } } break; } case VT_I2: { SHORT i = pValue->iVal; buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%hd (0x%hX)", i, i); } break; } case VT_UI2: { USHORT i = pValue->uiVal; buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%hu (0x%hX)", i, i); } break; } case VT_I4: { LONG l = pValue->lVal; buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%d (0x%X)", l, l); } break; } case VT_UI4: { ULONG l = pValue->ulVal; buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%u (0x%X)", l, l); } break; } case VT_R4: { float f = pValue->fltVal; buf = (WCHAR *)malloc(CVTBUFSIZE * sizeof(WCHAR)); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%10.4f", f); } break; } case VT_R8: { double d = pValue->dblVal; buf = (WCHAR *)malloc(CVTBUFSIZE * sizeof(WCHAR)); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%10.4f", d); } break; } case VT_BSTR: { if (dwType == CIM_SINT64) { // a little redundant, but it makes me feel better LPWSTR pWStr = pValue->bstrVal; __int64 l = _wtoi64(pWStr); buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%I64d", l); } } else if (dwType == CIM_UINT64) { // a little redundant, but it makes me feel better LPWSTR pWStr = pValue->bstrVal; __int64 l = _wtoi64(pWStr); buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"%I64u", l); } } else // string, datetime, reference { LPWSTR pWStr = pValue->bstrVal; buf = (WCHAR *)malloc((wcslen(pWStr) * sizeof(WCHAR)) + sizeof(WCHAR) + (2 * sizeof(WCHAR))); if (buf) { StringCbPrintfW(buf, BLOCKSIZE, L"\"%wS\"", pWStr); } } break; } case VT_BOOL|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, (iUBound - iLBound + 1) * BLOCKSIZE, L","); } else { bFirst = FALSE; } VARIANT_BOOL v; SafeArrayGetElement(pVec, &i, &v); if (v) { StringCbCatW(buf, (iUBound - iLBound + 1) * BLOCKSIZE, L"TRUE"); } else { StringCbCatW(buf, (iUBound - iLBound + 1) * BLOCKSIZE, L"FALSE"); } } } break; } case VT_I1|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } int bufSize = (iUBound - iLBound + 1) * BLOCKSIZE; buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); WCHAR *pos = buf; DWORD len; BYTE *pbstr; SafeArrayAccessData(pVec, (void HUGEP* FAR*)&pbstr); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCopyW(pos, bufSize - (pos - buf) * sizeof(WCHAR), L","); pos += 1; } else { bFirst = FALSE; } char v; // SafeArrayGetElement(pVec, &i, &v); v = pbstr[i]; if (v < 32) { len = StringCbPrintfW(lbuf, sizeof(lbuf), L"%hd (0x%X)", v, v); } else { len = StringCbPrintfW(lbuf, sizeof(lbuf), L"'%c' %hd (0x%X)", v, v, v); } StringCbCopyW(pos, bufSize - (pos - buf)*sizeof(WCHAR), lbuf); pos += len; } } SafeArrayUnaccessData(pVec); break; } case VT_UI1|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } int bufsize = (iUBound - iLBound + 1) * BLOCKSIZE; buf = (WCHAR *)malloc(bufsize); if (buf) { StringCbCopyW(buf, bufsize, L""); WCHAR *pos = buf; DWORD len; BYTE *pbstr; SafeArrayAccessData(pVec, (void HUGEP* FAR*)&pbstr); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCopyW(pos, bufsize - ((pos - buf)*sizeof(WCHAR)), L","); pos += 1; } else { bFirst = FALSE; } unsigned char v; // SafeArrayGetElement(pVec, &i, &v); v = pbstr[i]; if (v < 32) { len = StringCbPrintfW(lbuf, sizeof(lbuf), L"%hu (0x%X)", v, v); } else { len = StringCbPrintfW(lbuf, sizeof(lbuf), L"'%c' %hu (0x%X)", v, v, v); } StringCbCopyW(pos, bufsize - ((pos - buf)*sizeof(WCHAR)), lbuf); pos += len; } } SafeArrayUnaccessData(pVec); break; } case VT_I2|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L","); } else { bFirst = FALSE; } SHORT v; SafeArrayGetElement(pVec, &i, &v); StringCbPrintfW(lbuf, sizeof(lbuf), L"%hd", v); StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), lbuf); } } break; } case VT_UI2|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L","); } else { bFirst = FALSE; } USHORT v; SafeArrayGetElement(pVec, &i, &v); StringCbPrintfW(lbuf, sizeof(lbuf), L"%hu", v); StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), lbuf); } } break; } case VT_I4|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf,((iUBound - iLBound + 1) * BLOCKSIZE), L","); } else { bFirst = FALSE; } LONG v; SafeArrayGetElement(pVec, &i, &v); _ltow_s(v, lbuf, 10); StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), lbuf); } } break; } case VT_UI4|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L","); } else { bFirst = FALSE; } ULONG v; SafeArrayGetElement(pVec, &i, &v); _ultow_s(v, lbuf, 10); StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), lbuf); } } break; } case CIM_REAL32|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } int bufSize = (iUBound - iLBound + 1) * (CVTBUFSIZE * sizeof(WCHAR)); buf = (WCHAR *)malloc(bufSize); if (buf) { StringCbCopyW(buf, bufSize, L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, bufSize, L","); } else { bFirst = FALSE; } FLOAT v; SafeArrayGetElement(pVec, &i, &v); StringCbPrintfW(lbuf, sizeof(lbuf), L"%10.4f", v); StringCbCatW(buf, bufSize, lbuf); } } break; } case CIM_REAL64|VT_ARRAY: { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } int bufSize = (iUBound - iLBound + 1) * (CVTBUFSIZE * sizeof(WCHAR)); buf = (WCHAR *)malloc(bufSize); if (buf) { StringCbCopyW(buf, bufSize, L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, bufSize, L","); } else { bFirst = FALSE; } double v; SafeArrayGetElement(pVec, &i, &v); StringCbPrintfW(lbuf, sizeof(lbuf), L"%10.4f", v); StringCbCatW(buf, bufSize, lbuf); } } break; } case VT_BSTR|VT_ARRAY: { if (dwType == (CIM_UINT64|VT_ARRAY)) { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L","); } else { bFirst = FALSE; } BSTR v = NULL; SafeArrayGetElement(pVec, &i, &v); StringCbPrintfW(lbuf, sizeof(lbuf), L"%I64u", _wtoi64(v)); StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), lbuf); } } } else if (dwType == (CIM_SINT64|VT_ARRAY)) { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; BOOL bFirst = TRUE; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } buf = (WCHAR *)malloc((iUBound - iLBound + 1) * BLOCKSIZE); if (buf) { StringCbCopyW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L""); for (long i = iLBound; i <= iUBound; i++) { if (!bFirst) { StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), L","); } else { bFirst = FALSE; } BSTR v = NULL; SafeArrayGetElement(pVec, &i, &v); StringCbPrintfW(lbuf, sizeof(lbuf), L"%I64d", _wtoi64(v)); StringCbCatW(buf, ((iUBound - iLBound + 1) * BLOCKSIZE), lbuf); } } } else // string, datetime, reference { SAFEARRAY *pVec = pValue->parray; long iLBound, iUBound; DWORD iNeed; size_t iVSize; DWORD iCurBufSize; SafeArrayGetLBound(pVec, 1, &iLBound); SafeArrayGetUBound(pVec, 1, &iUBound); if ((iUBound - iLBound + 1) == 0) { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<empty array>"); } break; } iTotBufSize = (iUBound - iLBound + 1) * BLOCKSIZE; buf = (WCHAR *)malloc(iTotBufSize); if (buf) { buf[0] = L'\0'; iCurBufSize = 0; iVSize = BLOCKSIZE; vbuf = (WCHAR *)malloc(BLOCKSIZE); if (vbuf) { size_t iLen; WCHAR *Buffer; WCHAR *Buff; for (long i = iLBound; i <= iUBound; i++) { BSTR v = NULL; SafeArrayGetElement(pVec, &i, &v); iLen = (SysStringLen(v) + 1) * sizeof(WCHAR); if (iLen > iVSize) { Buffer = vbuf; vbuf = (WCHAR *)realloc(vbuf, iLen + sizeof(WCHAR)); iVSize = iLen; } // String size + (quotes + comma + null) if (vbuf) { iNeed = (StringCbPrintfW(vbuf, iVSize, L"%wS", v) + 4) * sizeof(WCHAR); if (iNeed + iCurBufSize > iTotBufSize) { iTotBufSize += (iNeed * 2); // Room enough for 2 more entries Buff = buf; buf = (WCHAR *)realloc(buf, iTotBufSize); } if (buf) { StringCbCatW(buf, iTotBufSize, L"\""); StringCbCatW(buf, iTotBufSize, vbuf); if (i + 1 <= iUBound) { StringCbCatW(buf, iTotBufSize, L"\","); } else { StringCbCatW(buf, iTotBufSize, L"\""); } iCurBufSize += iNeed; } else { buf = Buff; } } else { vbuf = Buffer; } SysFreeString(v); } free(vbuf); } } } break; } default: { if (fnHandler != NULL) { buf = fnHandler(pValue); } else { buf = (WCHAR *)malloc(BLOCKSIZE); if (buf) { StringCbCopyW(buf, BLOCKSIZE, L"<conversion error>"); } } break; } } if (!buf) { PrintErrorAndExit("ValueToString() out of memory", S_OK, 0); } *pbuf = buf; return buf; }
/* * @implemented */ int _itow_s(int value, wchar_t *str, size_t size, int radix) { return _ltow_s(value, str, size, radix); }
HRESULT touchmind::converter::NodeModelXMLEncoder::Encode(IN std::shared_ptr<touchmind::model::node::NodeModel> node, IN MSXML::IXMLDOMDocumentPtr pXMLDoc, OUT MSXML::IXMLDOMElementPtr &xmlNodeElement) { HRESULT hr = S_OK; try { // id std::wstring ws_idValue; touchmind::NodeIdToString(node->GetId(), ws_idValue); _variant_t v_idValue(ws_idValue.c_str()); xmlNodeElement->setAttribute(s_id, v_idValue); // position if (node->GetPosition() == NODE_SIDE_LEFT) { xmlNodeElement->setAttribute(s_position, v_positionLeftValue); } else if (node->GetPosition() == NODE_SIDE_RIGHT) { xmlNodeElement->setAttribute(s_position, v_positionRightValue); } // created time std::wstring ws_createdTime; touchmind::SystemtimeToString(&node->GetCreatedTime(), ws_createdTime); _variant_t v_createdTimeValue(ws_createdTime.c_str()); xmlNodeElement->setAttribute(s_createdTime, v_createdTimeValue); // modified time std::wstring ws_modifiedTime; touchmind::SystemtimeToString(&node->GetModifiedTime(), ws_modifiedTime); _variant_t v_modifiedTimeValue(ws_modifiedTime.c_str()); xmlNodeElement->setAttribute(s_modifiedTime, v_modifiedTimeValue); // width std::wstring ws_width; touchmind::SizeToString(node->GetWidth(), ws_width); _variant_t v_width(ws_width.c_str()); xmlNodeElement->setAttribute(s_width, v_width); // height std::wstring ws_height; touchmind::SizeToString(node->GetHeight(), ws_height); _variant_t v_height(ws_height.c_str()); xmlNodeElement->setAttribute(s_height, v_height); // background color if (!touchmind::util::ColorUtil::Equal(node->GetBackgroundColor(), D2D1::ColorF(D2D1::ColorF::White))) { std::wstring ws_backgroundColorValue; // touchmind::ColorrefToString(touchmind::util::ColorUtil::ToColorref(node->GetBackgroundColor()), // ws_backgroundColorValue); touchmind::ColorFToString(node->GetBackgroundColor(), ws_backgroundColorValue); _variant_t v_backgroundColorValue(ws_backgroundColorValue.c_str()); xmlNodeElement->setAttribute(s_backgroundColor, v_backgroundColorValue); } // shape if (node->GetNodeShape() != prop::NodeShape::GetDefaultNodeShape()) { std::wstring ws_nodeShape = prop::NodeShape::ToString(node->GetNodeShape()); _variant_t v_nodeShape(ws_nodeShape.c_str()); xmlNodeElement->setAttribute(s_shape, v_nodeShape); } xmlNodeElement->setAttribute(s_height, v_height); // text element MSXML::IXMLDOMElementPtr xmlTextElement = pXMLDoc->createElement(s_text); xmlNodeElement->appendChild(xmlTextElement); // text _bstr_t s_textValue(node->GetText().c_str()); MSXML::IXMLDOMTextPtr pText = pXMLDoc->createTextNode(s_textValue); xmlTextElement->appendChild(pText); if (node->GetFontAttributeCount() > 0) { // fontAttriutes element MSXML::IXMLDOMElementPtr xmlFontAttributesElement = pXMLDoc->createElement(s_fontAttributes); xmlTextElement->appendChild(xmlFontAttributesElement); for (size_t i = 0; i < node->GetFontAttributeCount(); ++i) { // fontAttribute MSXML::IXMLDOMElementPtr xmlFontAttributeElement = pXMLDoc->createElement(s_fontAttribute); xmlFontAttributesElement->appendChild(xmlFontAttributeElement); // start position wchar_t buf[1024]; _ltow_s(node->GetFontAttribute(i).startPosition, buf, 1024, 10); _variant_t v_startPositionValue(buf); xmlFontAttributeElement->setAttribute(s_startPosition, v_startPositionValue); // length _ltow_s(node->GetFontAttribute(i).length, buf, 1024, 10); _variant_t v_lengthValue(buf); xmlFontAttributeElement->setAttribute(s_length, v_lengthValue); // font family if (node->GetFontAttribute(i).fontFamilyName.length() > 0) { _variant_t v_fontFamilyValue(node->GetFontAttribute(i).fontFamilyName.c_str()); xmlFontAttributeElement->setAttribute(s_fontFamily, v_fontFamilyValue); } // font size if (node->GetFontAttribute(i).fontSize > 0.0f) { std::wstring ws_fontSizeValue; touchmind::FontSizeToString(node->GetFontAttribute(i).fontSize, ws_fontSizeValue); _variant_t v_fontSizeValue(ws_fontSizeValue.c_str()); xmlFontAttributeElement->setAttribute(s_fontSize, v_fontSizeValue); } // bold if (node->GetFontAttribute(i).bold) { xmlFontAttributeElement->setAttribute(s_bold, v_boolTrueValue); } // italic if (node->GetFontAttribute(i).italic) { xmlFontAttributeElement->setAttribute(s_italic, v_boolTrueValue); } // underline if (node->GetFontAttribute(i).underline) { xmlFontAttributeElement->setAttribute(s_underline, v_boolTrueValue); } // strikethrough if (node->GetFontAttribute(i).strikethrough) { xmlFontAttributeElement->setAttribute(s_strikethrough, v_boolTrueValue); } // foreground color if (node->GetFontAttribute(i).foregroundColor != static_cast<LONG>(touchmind::text::FontAttributeCommand::DEFAULT_FONT_COLOR)) { std::wstring ws_foregroundColorValue; touchmind::ColorrefToString(node->GetFontAttribute(i).foregroundColor, ws_foregroundColorValue); _variant_t v_foregroundColorValue(ws_foregroundColorValue.c_str()); xmlFontAttributeElement->setAttribute(s_foregroundColor, v_foregroundColorValue); } } } { bool hasOutput = false; // ***** path ***** auto path = node->GetPathModel(); // path MSXML::IXMLDOMElementPtr xmlPathElement = pXMLDoc->createElement(s_path); // path width if (path->GetWidth() != LINE_WIDTH_1) { std::wstring ws_pathWidth = prop::LineWidth::ToString(path->GetWidth()); _variant_t v_pathWidth(ws_pathWidth.c_str()); xmlPathElement->setAttribute(s_width, v_pathWidth); hasOutput = true; } // path color if (!touchmind::util::ColorUtil::Equal(path->GetColor(), D2D1::ColorF(D2D1::ColorF::Black))) { std::wstring ws_colorValue; touchmind::ColorFToString(path->GetColor(), ws_colorValue); _variant_t v_colorValue(ws_colorValue.c_str()); xmlPathElement->setAttribute(s_color, v_colorValue); hasOutput = true; } // path style if (path->GetStyle() != LINE_STYLE_SOLID) { std::wstring ws_pathStyle = prop::LineStyle::ToString(path->GetStyle()); _variant_t v_pathStyle(ws_pathStyle.c_str()); xmlPathElement->setAttribute(s_style, v_pathStyle); hasOutput = true; } if (hasOutput) { xmlNodeElement->appendChild(xmlPathElement); } } for (size_t i = 0; i < node->GetNumberOfLinks(); ++i) { auto wlink = node->GetLink(i); if (!wlink.expired()) { auto link = wlink.lock(); if (link->GetNode(EDGE_ID_1) == node) { MSXML::IXMLDOMElementPtr xmlLinkElement = pXMLDoc->createElement(s_link); xmlNodeElement->appendChild(xmlLinkElement); // destination std::wstring ws_idValue; touchmind::NodeIdToString(link->GetNode(EDGE_ID_2)->GetId(), ws_idValue); _variant_t v_idValue(ws_idValue.c_str()); xmlLinkElement->setAttribute(s_destination, v_idValue); // link width std::wstring ws_linkWidth = prop::LineWidth::ToString(link->GetLineWidth()); _variant_t v_linkWidth(ws_linkWidth.c_str()); xmlLinkElement->setAttribute(s_width, v_linkWidth); // link color if (!touchmind::util::ColorUtil::Equal(link->GetLineColor(), D2D1::ColorF(D2D1::ColorF::Black))) { std::wstring ws_colorValue; touchmind::ColorFToString(link->GetLineColor(), ws_colorValue); _variant_t v_colorValue(ws_colorValue.c_str()); xmlLinkElement->setAttribute(s_color, v_colorValue); } // link style std::wstring ws_lineStyle = prop::LineStyle::ToString(link->GetLineStyle()); _variant_t v_lineStyle(ws_lineStyle.c_str()); xmlLinkElement->setAttribute(s_style, v_lineStyle); // link edge style 1 std::wstring ws_edgeStyle1 = prop::LineEdgeStyle::ToString(link->GetEdge(EDGE_ID_1)->GetStyle()); _variant_t v_edgeStyle1(ws_edgeStyle1.c_str()); xmlLinkElement->setAttribute(s_startStyle, v_edgeStyle1); // link edge style 2 std::wstring ws_edgeStyle2 = prop::LineEdgeStyle::ToString(link->GetEdge(EDGE_ID_2)->GetStyle()); _variant_t v_edgeStyle2(ws_edgeStyle2.c_str()); xmlLinkElement->setAttribute(s_endStyle, v_edgeStyle2); // handle 1 std::wstring ws_handle1; HandleToString(link->GetEdge(EDGE_ID_1)->GetAngle(), link->GetEdge(EDGE_ID_1)->GetLength(), ws_handle1); _variant_t v_handle1(ws_handle1.c_str()); xmlLinkElement->setAttribute(s_startHandle, v_handle1); // handle 2 std::wstring ws_handle2; HandleToString(link->GetEdge(EDGE_ID_2)->GetAngle(), link->GetEdge(EDGE_ID_2)->GetLength(), ws_handle2); _variant_t v_handle2(ws_handle2.c_str()); xmlLinkElement->setAttribute(s_endHandle, v_handle2); } } } for (size_t i = 0; i < node->GetActualChildrenCount(); ++i) { std::shared_ptr<touchmind::model::node::NodeModel> child = node->GetChild(i); MSXML::IXMLDOMElementPtr xmlChildNodeElement = pXMLDoc->createElement(s_node); xmlNodeElement->appendChild(xmlChildNodeElement); Encode(child, pXMLDoc, xmlChildNodeElement); } } catch (_com_error &errorObject) { LOG(SEVERITY_LEVEL_ERROR) << "Exception thrown, HRESULT: " << errorObject.Error(); return errorObject.Error(); } return hr; }
wchar_t* ItoWStatic(int integer) { ASSERT_ZERORET(_ltow_s(integer, s_ItoWCacheBuff, MAX_PATH, 10), NULL); return s_ItoWCacheBuff; }