diff --git a/cpp/implsapi.cpp b/cpp/implsapi.cpp index 53e59bd4..fe1112fa 100644 --- a/cpp/implsapi.cpp +++ b/cpp/implsapi.cpp @@ -1,4 +1,7 @@ +wchar_t SPCAT_VOICES_7[] = LR"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices)"; +wchar_t SPCAT_VOICES_10[] = LR"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices)"; + std::optional> _Speak(std::wstring &Content, const wchar_t *token, int voiceid, int rate, int volume) { std::optional> ret = {}; diff --git a/cpp/shareddllproxy/aspatch.cpp b/cpp/shareddllproxy/aspatch.cpp index 4552cd90..024a6f4f 100644 --- a/cpp/shareddllproxy/aspatch.cpp +++ b/cpp/shareddllproxy/aspatch.cpp @@ -39,7 +39,7 @@ std::wstring stolower(const std::wstring &s1) std::vector EnumerateProcesses(const std::wstring &exe) { - HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + AutoHandle hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot == INVALID_HANDLE_VALUE) { return {}; @@ -50,7 +50,6 @@ std::vector EnumerateProcesses(const std::wstring &exe) if (!Process32First(hSnapshot, &pe32)) { - CloseHandle(hSnapshot); return {}; } std::vector pids; @@ -60,7 +59,6 @@ std::vector EnumerateProcesses(const std::wstring &exe) pids.push_back(pe32.th32ProcessID); } while (Process32Next(hSnapshot, &pe32)); - CloseHandle(hSnapshot); return pids; } enum diff --git a/cpp/shareddllproxy/neospeech.cpp b/cpp/shareddllproxy/neospeech.cpp index 7605a8fb..d10ab8e5 100644 --- a/cpp/shareddllproxy/neospeech.cpp +++ b/cpp/shareddllproxy/neospeech.cpp @@ -1,10 +1,11 @@ std::optional> _Speak(std::wstring &Content, const wchar_t *token, int voiceid, int rate, int volume); - std::vector _List(const wchar_t *token); +extern wchar_t SPCAT_VOICES_7[]; +extern wchar_t SPCAT_VOICES_10[]; int neospeechlist(int argc, wchar_t *argv[]) { FILE *f = _wfopen(argv[1], L"wb"); - for (auto key : {L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices", L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech_OneCore\\Voices"}) + for (auto key : {SPCAT_VOICES_7, SPCAT_VOICES_10}) { auto speechs = _List(key); for (int i = 0; i < speechs.size(); i++) diff --git a/cpp/version.cmake b/cpp/version.cmake index 252b6183..cf1e127e 100644 --- a/cpp/version.cmake +++ b/cpp/version.cmake @@ -1,7 +1,7 @@ set(VERSION_MAJOR 6) set(VERSION_MINOR 16) -set(VERSION_PATCH 3) +set(VERSION_PATCH 4) set(VERSION_REVISION 0) set(LUNA_VERSION "{${VERSION_MAJOR},${VERSION_MINOR},${VERSION_PATCH},${VERSION_REVISION}}") add_library(VERSION_DEF ${CMAKE_CURRENT_LIST_DIR}/version_def.cpp) diff --git a/cpp/winsharedutils/MWebBrowser.cpp b/cpp/winsharedutils/MWebBrowser.cpp index 2f1aec0e..52193b42 100644 --- a/cpp/winsharedutils/MWebBrowser.cpp +++ b/cpp/winsharedutils/MWebBrowser.cpp @@ -163,15 +163,9 @@ IWebBrowser2 *MWebBrowser::GetIWebBrowser2() return m_web_browser2; } -IHTMLDocument2 *MWebBrowser::GetIHTMLDocument2() +HRESULT MWebBrowser::GetIHTMLDocument2(IHTMLDocument2 **p) { - IDispatch *pDisp; - m_web_browser2->get_Document(&pDisp); - if (pDisp) - { - return static_cast(pDisp); - } - return NULL; + return m_web_browser2->get_Document((IDispatch **)p); } HWND MWebBrowser::GetControlWindow() @@ -221,7 +215,7 @@ HRESULT MWebBrowser::CreateBrowser(HWND hwndParent) RECT rc; ::SetRectEmpty(&rc); CHECK_FAILURE(m_ole_object->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, this, 0, - m_hwndParent, &rc)); + m_hwndParent, &rc)); CHECK_FAILURE(m_ole_object.QueryInterface(&m_web_browser2)); HWND hwnd = GetControlWindow(); @@ -1002,30 +996,20 @@ HRESULT MWebBrowser::GetTypeInfoCount(UINT *pctinfo) { return E_FAIL; } HRESULT MWebBrowser::GetTypeInfo(UINT, LCID, ITypeInfo **) { return E_FAIL; } HRESULT MWebBrowser::GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) { return E_FAIL; } -void AddCustomObject(IHTMLDocument2 *doc, IDispatch *custObj, std::wstring name) +HRESULT MWebBrowser::AddCustomObject(IDispatch *custObj, std::wstring name) { - CComPtr _doc = doc; - if (doc == NULL) - return; + CComPtr doc; + CHECK_FAILURE(GetIHTMLDocument2(&doc)); CComPtr win = NULL; - _doc->get_parentWindow(&win); - - if (!win) - return; + CHECK_FAILURE(doc->get_parentWindow(&win)); CComPtr winEx; - HRESULT hr = win.QueryInterface(&winEx); - - if (FAILED(hr) || !winEx) - return; + CHECK_FAILURE(win.QueryInterface(&winEx)); DISPID dispid; CComBSTR bname = name.c_str(); - hr = winEx->GetDispID(bname, fdexNameEnsure, &dispid); - - if (FAILED(hr)) - return; + CHECK_FAILURE(winEx->GetDispID(bname, fdexNameEnsure, &dispid)); DISPID namedArgs[] = {DISPID_PROPERTYPUT}; DISPPARAMS params; @@ -1036,10 +1020,7 @@ void AddCustomObject(IHTMLDocument2 *doc, IDispatch *custObj, std::wstring name) params.cArgs = 1; params.cNamedArgs = 1; - hr = winEx->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, ¶ms, NULL, NULL, NULL); - - if (FAILED(hr)) - return; + return winEx->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, ¶ms, NULL, NULL, NULL); } HRESULT MWebBrowser::Invoke(DISPID dispIdMember, REFIID, LCID, WORD, DISPPARAMS *pDispParams, VARIANT *pVarResult, @@ -1048,9 +1029,8 @@ HRESULT MWebBrowser::Invoke(DISPID dispIdMember, REFIID, LCID, WORD, if (dispIdMember == DISPID_DOCUMENTCOMPLETE) return OnCompleted(pDispParams); else if (dispIdMember == DISPID_NAVIGATECOMPLETE2) - return AddCustomObject(GetIHTMLDocument2(), jsobj, L"LUNAJSObject"), S_OK; - else - return S_OK; + return AddCustomObject(jsobj, L"LUNAJSObject"), S_OK; + return S_OK; } HRESULT MWebBrowser::OnCompleted(DISPPARAMS *args) { diff --git a/cpp/winsharedutils/MWebBrowser.hpp b/cpp/winsharedutils/MWebBrowser.hpp index 1c5f3a9a..cef7c468 100644 --- a/cpp/winsharedutils/MWebBrowser.hpp +++ b/cpp/winsharedutils/MWebBrowser.hpp @@ -78,10 +78,10 @@ public: void Destroy(); BOOL TranslateAccelerator(LPMSG pMsg); IWebBrowser2 *GetIWebBrowser2(); - IHTMLDocument2 *GetIHTMLDocument2(); + HRESULT GetIHTMLDocument2(IHTMLDocument2**); void AllowInsecure(BOOL bAllow); HRESULT Quit(); - + HRESULT AddCustomObject(IDispatch *custObj, std::wstring name); HRESULT get_Application(IDispatch **ppApplication) const; HRESULT get_LocationURL(BSTR *bstrURL) const; HRESULT put_Silent(VARIANT_BOOL bSilent); diff --git a/cpp/winsharedutils/SimpleBrowser.cpp b/cpp/winsharedutils/SimpleBrowser.cpp index 9ae85134..67591c8e 100644 --- a/cpp/winsharedutils/SimpleBrowser.cpp +++ b/cpp/winsharedutils/SimpleBrowser.cpp @@ -12,23 +12,13 @@ BOOL GetIEVersion(LPWSTR pszVersion, DWORD cchVersionMax) { - pszVersion[0] = 0; - HKEY hKey = NULL; - RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Internet Explorer", 0, - KEY_READ, &hKey); - if (hKey) - { - DWORD cb = cchVersionMax * sizeof(WCHAR); - LONG ret = RegQueryValueExW(hKey, L"svcVersion", NULL, NULL, (LPBYTE)pszVersion, &cb); - if (ret != ERROR_SUCCESS) - { - ret = RegQueryValueExW(hKey, L"Version", NULL, NULL, (LPBYTE)pszVersion, &cb); - } - RegCloseKey(hKey); - - return ret == ERROR_SUCCESS; - } - + CRegKey hKey; + if (ERROR_SUCCESS != hKey.Open(HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Internet Explorer)", KEY_READ)) + return FALSE; + DWORD cb = cchVersionMax * sizeof(WCHAR); + if ((ERROR_SUCCESS == hKey.QueryStringValue(L"svcVersion", pszVersion, &cb)) || + (ERROR_SUCCESS == hKey.QueryStringValue(L"Version", pszVersion, &cb))) + return TRUE; return FALSE; } static DWORD getemulation() @@ -69,42 +59,29 @@ static DWORD getemulation() BOOL DoSetBrowserEmulation(DWORD dwValue) { static const TCHAR s_szFeatureControl[] = - TEXT("SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl"); + TEXT(R"(SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl)"); TCHAR szPath[MAX_PATH], *pchFileName; GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)); pchFileName = PathFindFileName(szPath); - BOOL bOK = FALSE; - HKEY hkeyControl = NULL; - RegOpenKeyEx(HKEY_CURRENT_USER, s_szFeatureControl, 0, KEY_ALL_ACCESS, &hkeyControl); - if (hkeyControl) + CRegKey hkeyControl; + if (ERROR_SUCCESS != hkeyControl.Open(HKEY_CURRENT_USER, s_szFeatureControl, KEY_ALL_ACCESS)) + return FALSE; + + CRegKey hkeyEmulation; + if (ERROR_SUCCESS != hkeyEmulation.Create(hkeyControl, TEXT("FEATURE_BROWSER_EMULATION"), 0, 0, KEY_ALL_ACCESS, NULL, NULL)) + return FALSE; + + if (dwValue) { - HKEY hkeyEmulation = NULL; - RegCreateKeyEx(hkeyControl, TEXT("FEATURE_BROWSER_EMULATION"), 0, NULL, 0, - KEY_ALL_ACCESS, NULL, &hkeyEmulation, NULL); - if (hkeyEmulation) - { - if (dwValue) - { - DWORD value = dwValue, size = sizeof(value); - LONG result = RegSetValueEx(hkeyEmulation, pchFileName, 0, - REG_DWORD, (LPBYTE)&value, size); - bOK = (result == ERROR_SUCCESS); - } - else - { - RegDeleteValue(hkeyEmulation, pchFileName); - bOK = TRUE; - } - - RegCloseKey(hkeyEmulation); - } - - RegCloseKey(hkeyControl); + return ERROR_SUCCESS == hkeyEmulation.SetValue(dwValue, pchFileName); + } + else + { + hkeyEmulation.DeleteValue(pchFileName); + return TRUE; } - - return bOK; } class MWebBrowserEx : public MWebBrowser { @@ -215,14 +192,14 @@ DECLARE_API void html_release(void *web) // ww->Release(); Destroy减少引用计数,自动del } -DECLARE_API const wchar_t *html_get_current_url(void *web) +DECLARE_API void html_get_current_url(void *web, void (*cb)(LPCWSTR)) { if (!web) - return L""; + return; auto ww = static_cast(web); - wchar_t *_u; - ww->get_LocationURL(&_u); - return _u; + CComBSTR _u; + CHECK_FAILURE_NORET(ww->get_LocationURL(&_u)); + cb(_u); } DECLARE_API void html_set_html(void *web, wchar_t *html) @@ -243,40 +220,20 @@ DECLARE_API void html_add_menu(void *web, int index, int command, const wchar_t auto ptr = ww->menuitems.begin() + index; ww->menuitems.insert(ptr, {_label, command}); } -BSTR GetSelectedText(IHTMLDocument2 *pHTMLDoc2) -{ - CComPtr pSelectionObj = nullptr; - HRESULT hr = pHTMLDoc2->get_selection(&pSelectionObj); - if (FAILED(hr) || pSelectionObj == nullptr) - { - return nullptr; - } - - CComPtr pTxtRange = nullptr; - hr = pSelectionObj->createRange((IDispatch **)&pTxtRange); - if (FAILED(hr) || pTxtRange == nullptr) - { - return nullptr; - } - - BSTR selectedText = nullptr; - hr = pTxtRange->get_text(&selectedText); - - return selectedText; -} -DECLARE_API const wchar_t *html_get_select_text(void *web) +DECLARE_API void html_get_select_text(void *web, void (*cb)(LPCWSTR)) { if (!web) - return L""; + return; auto ww = static_cast(web); - - if (CComPtr pDocument = ww->GetIHTMLDocument2()) - { - auto text = GetSelectedText(pDocument); - // 不需要free,free会崩溃 - return text; - } - return L""; + CComPtr pDocument; + CHECK_FAILURE_NORET(ww->GetIHTMLDocument2(&pDocument)); + CComPtr pSelectionObj; + CHECK_FAILURE_NORET(pDocument->get_selection(&pSelectionObj)); + CComPtr pTxtRange; + CHECK_FAILURE_NORET(pSelectionObj->createRange((IDispatch **)&pTxtRange)); + CComBSTR selectedText; + CHECK_FAILURE_NORET(pTxtRange->get_text(&selectedText)); + cb(selectedText); } DECLARE_API void html_bind_function(void *web, const wchar_t *name, void (*function)(wchar_t **, int)) @@ -300,18 +257,14 @@ DECLARE_API void html_eval(void *web, const wchar_t *js) if (!web) return; auto ww = static_cast(web); - CComPtr pDocument = ww->GetIHTMLDocument2(); - if (!pDocument) - return; + CComPtr pDocument; + CHECK_FAILURE_NORET(ww->GetIHTMLDocument2(&pDocument)); CComPtr scriptDispatch; - if (FAILED(pDocument->get_Script(&scriptDispatch))) - return; + CHECK_FAILURE_NORET(pDocument->get_Script(&scriptDispatch)); DISPID dispid; CComBSTR evalStr = L"eval"; - if (scriptDispatch->GetIDsOfNames(IID_NULL, &evalStr, 1, - LOCALE_SYSTEM_DEFAULT, &dispid) != S_OK) - - return; + CHECK_FAILURE_NORET(scriptDispatch->GetIDsOfNames(IID_NULL, &evalStr, 1, + LOCALE_SYSTEM_DEFAULT, &dispid) != S_OK); DISPPARAMS params; VARIANT arg; diff --git a/cpp/winsharedutils/sapi_dll.cpp b/cpp/winsharedutils/sapi_dll.cpp index 43ab769f..e6fb5f70 100644 --- a/cpp/winsharedutils/sapi_dll.cpp +++ b/cpp/winsharedutils/sapi_dll.cpp @@ -1,15 +1,11 @@  - std::optional> _Speak(std::wstring &Content, const wchar_t *token, int voiceid, int rate, int volume); - std::vector _List(const wchar_t *token); - +extern wchar_t SPCAT_VOICES_7[]; +extern wchar_t SPCAT_VOICES_10[]; namespace SAPI { - constexpr wchar_t SPCAT_VOICES_7[] = L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices"; - constexpr wchar_t SPCAT_VOICES_10[] = L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech_OneCore\\Voices"; - std::vector List(int version) { if (version == 7) diff --git a/cpp/winsharedutils/theme.cpp b/cpp/winsharedutils/theme.cpp index 2346d0ab..4fbce1b5 100644 --- a/cpp/winsharedutils/theme.cpp +++ b/cpp/winsharedutils/theme.cpp @@ -200,19 +200,14 @@ DECLARE_API void _SetTheme( DECLARE_API bool isDark() { #ifndef WINXP - HKEY hKey; - const char *subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; - if (RegOpenKeyExA(HKEY_CURRENT_USER, subKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS) - { - DWORD value; - DWORD dataSize = sizeof(DWORD); - if (RegQueryValueExA(hKey, "AppsUseLightTheme", 0, NULL, (LPBYTE)&value, &dataSize) == ERROR_SUCCESS) - { - RegCloseKey(hKey); - return 1 - value; - } - RegCloseKey(hKey); - } + constexpr auto subKey = LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)"; + CRegKey hKey; + if (ERROR_SUCCESS != hKey.Open(HKEY_CURRENT_USER, subKey, KEY_READ)) + return false; + + DWORD value; + if (ERROR_SUCCESS != hKey.QueryDWORDValue(L"AppsUseLightTheme", value)) + return false; + return 1 - value; #endif - return false; } \ No newline at end of file diff --git a/py/LunaTranslator/winsharedutils.py b/py/LunaTranslator/winsharedutils.py index d17a397a..34cee1b0 100644 --- a/py/LunaTranslator/winsharedutils.py +++ b/py/LunaTranslator/winsharedutils.py @@ -126,16 +126,32 @@ html_resize = utilsdll.html_resize html_resize.argtypes = c_void_p, c_uint, c_uint, c_uint, c_uint html_release = utilsdll.html_release html_release.argtypes = (c_void_p,) -html_get_current_url = utilsdll.html_get_current_url -html_get_current_url.argtypes = (c_void_p,) -html_get_current_url.restype = c_wchar_p +_html_get_current_url = utilsdll.html_get_current_url +_html_get_current_url.argtypes = (c_void_p, c_void_p) html_set_html = utilsdll.html_set_html html_set_html.argtypes = (c_void_p, c_wchar_p) html_add_menu = utilsdll.html_add_menu html_add_menu.argtypes = (c_void_p, c_int, c_int, c_wchar_p) -html_get_select_text = utilsdll.html_get_select_text -html_get_select_text.argtypes = (c_void_p,) -html_get_select_text.restype = c_wchar_p +_html_get_select_text = utilsdll.html_get_select_text +_html_get_select_text.argtypes = (c_void_p, c_void_p) + + +def html_get_current_url(__): + _ = [] + _html_get_current_url(__, CFUNCTYPE(None, c_wchar_p)(_.append)) + if _: + return _[0] + return "" + + +def html_get_select_text(__): + _ = [] + _html_get_select_text(__, CFUNCTYPE(None, c_wchar_p)(_.append)) + if _: + return _[0] + return "" + + html_bind_function_FT = CFUNCTYPE(None, POINTER(c_wchar_p), c_int) html_bind_function = utilsdll.html_bind_function html_bind_function.argtypes = c_void_p, c_wchar_p, html_bind_function_FT