From 2204f27b01429b086f117cb15042e83a3d302ce2 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Thu, 27 Jul 2017 23:36:30 +0300 Subject: [PATCH] Hopefully fix a crash bug related to #32, #37 and #44 --- QuickLook.Native/QuickLook.Native32/DOpus.cpp | 2 +- .../QuickLook.Native32/HelperMethods.cpp | 2 +- .../QuickLook.Native32/Shell32.cpp | 7 +-- .../QuickLook.Native32/WoW64HookHelper.cpp | 6 +-- QuickLook/NativeMethods/QuickLook.cs | 53 +++++++++++++------ 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/QuickLook.Native/QuickLook.Native32/DOpus.cpp b/QuickLook.Native/QuickLook.Native32/DOpus.cpp index b0aa546..b914675 100644 --- a/QuickLook.Native/QuickLook.Native32/DOpus.cpp +++ b/QuickLook.Native/QuickLook.Native32/DOpus.cpp @@ -111,7 +111,7 @@ void DOpus::ParseXmlBuffer(PWCHAR buffer) void DOpus::PrepareMessageWindow() { - WNDCLASSEX wx = {'\0'}; + WNDCLASSEX wx = {sizeof wx}; wx.cbSize = sizeof(WNDCLASSEX); wx.lpfnWndProc = msgWindowProc; wx.lpszClassName = MSGWINDOW_CLASS; diff --git a/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp b/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp index 74c9fbb..2c35b27 100644 --- a/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp +++ b/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp @@ -42,7 +42,7 @@ void HelperMethods::GetSelectedInternal(CComQIPtr pwba, PWCHAR b void HelperMethods::ObtainFirstItem(CComPtr dao, PWCHAR buffer) { FORMATETC formatetc; - STGMEDIUM medium; + STGMEDIUM medium = {sizeof medium}; formatetc.cfFormat = CF_HDROP; formatetc.ptd = nullptr; diff --git a/QuickLook.Native/QuickLook.Native32/Shell32.cpp b/QuickLook.Native/QuickLook.Native32/Shell32.cpp index ab18a5f..2997009 100644 --- a/QuickLook.Native/QuickLook.Native32/Shell32.cpp +++ b/QuickLook.Native/QuickLook.Native32/Shell32.cpp @@ -106,12 +106,12 @@ void Shell32::getSelectedFromExplorer(PWCHAR buffer) for (auto i = 0; i < count; i++) { VARIANT vi; + VariantInit(&vi); V_VT(&vi) = VT_I4; V_I4(&vi) = i; CComPtr pdisp; - // ReSharper disable once CppSomeObjectMembersMightNotBeInitialized - if (FAILED(psw->Item(vi, &pdisp))) + if (S_OK != psw->Item(vi, &pdisp)) continue; CComQIPtr pwba; @@ -139,7 +139,8 @@ void Shell32::getSelectedFromDesktop(PWCHAR buffer) if (FAILED(psw.CoCreateInstance(CLSID_ShellWindows))) return; - VARIANT pvarLoc = {VT_EMPTY}; + VARIANT pvarLoc; + VariantInit(&pvarLoc); long phwnd; if (FAILED(psw->FindWindowSW(&pvarLoc, &pvarLoc, SWC_DESKTOP, &phwnd, SWFO_NEEDDISPATCH, reinterpret_cast(&pwba)))) return; diff --git a/QuickLook.Native/QuickLook.Native32/WoW64HookHelper.cpp b/QuickLook.Native/QuickLook.Native32/WoW64HookHelper.cpp index 158dd4f..1877944 100644 --- a/QuickLook.Native/QuickLook.Native32/WoW64HookHelper.cpp +++ b/QuickLook.Native/QuickLook.Native32/WoW64HookHelper.cpp @@ -51,7 +51,7 @@ bool WoW64HookHelper::Launch() auto p = wcsrchr(fullPath, L'\\'); memcpy(p, HELPER_FILE, wcslen(HELPER_FILE) * sizeof WCHAR); - STARTUPINFO si = {'\0'}; + STARTUPINFO si = {sizeof si}; PROCESS_INFORMATION pi = {nullptr}; si.cb = sizeof si; @@ -70,9 +70,9 @@ void WoW64HookHelper::createJob() hJob = CreateJobObject(nullptr, nullptr); - JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation = {'\0'}; + JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation = {sizeof BasicLimitInformation}; BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; - JOBOBJECT_EXTENDED_LIMIT_INFORMATION lpJobObjectInfo = {'\0'}; + JOBOBJECT_EXTENDED_LIMIT_INFORMATION lpJobObjectInfo = {sizeof lpJobObjectInfo}; lpJobObjectInfo.BasicLimitInformation = BasicLimitInformation; SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &lpJobObjectInfo, sizeof JOBOBJECT_EXTENDED_LIMIT_INFORMATION); diff --git a/QuickLook/NativeMethods/QuickLook.cs b/QuickLook/NativeMethods/QuickLook.cs index 5b1d5be..9af69e5 100644 --- a/QuickLook/NativeMethods/QuickLook.cs +++ b/QuickLook/NativeMethods/QuickLook.cs @@ -15,6 +15,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +using System; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; @@ -23,7 +25,7 @@ namespace QuickLook.NativeMethods { internal static class QuickLook { - private const int MaxPath = 260; + private const int MaxPath = 8192; [DllImport("QuickLook.Native32.dll", EntryPoint = "Init", CallingConvention = CallingConvention.Cdecl)] @@ -51,30 +53,51 @@ namespace QuickLook.NativeMethods internal static void Init() { - if (App.Is64Bit) - Init_64(); - else - Init_32(); + try + { + if (App.Is64Bit) + Init_64(); + else + Init_32(); + } + catch (Exception e) + { + Debug.WriteLine(e); + } } internal static FocusedWindowType GetFocusedWindowType() { - return App.Is64Bit ? GetFocusedWindowTypeNative_64() : GetFocusedWindowTypeNative_32(); + try + { + return App.Is64Bit ? GetFocusedWindowTypeNative_64() : GetFocusedWindowTypeNative_32(); + } + catch (Exception e) + { + Debug.WriteLine(e); + return FocusedWindowType.Invalid; + } } internal static string GetCurrentSelection() { StringBuilder sb = null; - // communicate with COM in a separate thread - Task.Run(() => + try { - sb = new StringBuilder(MaxPath); - if (App.Is64Bit) - GetCurrentSelectionNative_64(sb); - else - GetCurrentSelectionNative_32(sb); - }).Wait(); - + // communicate with COM in a separate thread + Task.Run(() => + { + sb = new StringBuilder(MaxPath); + if (App.Is64Bit) + GetCurrentSelectionNative_64(sb); + else + GetCurrentSelectionNative_32(sb); + }).Wait(); + } + catch (Exception e) + { + Debug.WriteLine(e); + } return sb?.ToString() ?? string.Empty; }