Skip to content

Commit

Permalink
Fixes for shell extension
Browse files Browse the repository at this point in the history
* Properly implemented cleanup routines when injected into
applications other than Explorer
* Never EVER forget to specify a ThreadingModel for a COM object
in registry. That was the cause the DLL was making Microsoft Teams
crash on startup
* Do not free memory when the DLL is unloaded (DLL_PROCESS_DETACH)
and lpvReserved is non-NULL. It means the DLL is unloaded as the process
terminates, in which case all threads are dead, so it is not safe to call freeing
routines - let the operating system reclaim the memory on process exit
  • Loading branch information
valinet committed Nov 11, 2021
1 parent e2af83e commit 04616ce
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 154 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This document includes the same release notes as in the [Releases](https://github.com/valinet/ExplorerPatcher/releases) section on GitHub.

## 22000.318.36

Tested on build 22000.318.

#### Fixes

* Lots of bug and issue fixes for shell extension failing to work under certain circumstances; fixed #259

## 22000.318.35

Tested on build 22000.318.
Expand Down
8 changes: 4 additions & 4 deletions ExplorerPatcher/ExplorerPatcher.rc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 22000,318,35,0
PRODUCTVERSION 22000,318,35,0
FILEVERSION 22000,318,36,0
PRODUCTVERSION 22000,318,36,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -69,12 +69,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "VALINET Solutions SRL"
VALUE "FileDescription", "ExplorerPatcher"
VALUE "FileVersion", "22000.318.35.0"
VALUE "FileVersion", "22000.318.36.0"
VALUE "InternalName", "ExplorerPatcher.dll"
VALUE "LegalCopyright", "Copyright (C) 2006-2021 VALINET Solutions SRL. All rights reserved."
VALUE "OriginalFilename", "ExplorerPatcher.dll"
VALUE "ProductName", "ExplorerPatcher"
VALUE "ProductVersion", "22000.318.35.0"
VALUE "ProductVersion", "22000.318.36.0"
END
END
BLOCK "VarFileInfo"
Expand Down
152 changes: 91 additions & 61 deletions ExplorerPatcher/SettingsMonitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,113 @@
DWORD WINAPI MonitorSettings(SettingsChangeParameters* params)
{
BOOL bShouldExit = FALSE;
HANDLE* handles = NULL;
printf("[SettingsMonitor] Started %p\n", params->settings[0].hEvent);

while (TRUE)
{
HANDLE* handles = malloc(sizeof(HANDLE) * (params->size + 1));
if (!handles)
handles = calloc(sizeof(HANDLE), params->size);
if (handles)
{
return 0;
}
for (unsigned int i = 0; i < params->size; ++i)
{
params->settings[i].hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
if (!params->settings[i].hEvent)
{
return 0;
}
handles[i] = params->settings[i].hEvent;
if (RegCreateKeyExW(
params->settings[i].origin,
params->settings[i].name,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_READ,
NULL,
&(params->settings[i].hKey),
NULL
) != ERROR_SUCCESS)
for (unsigned int i = 0; i < params->size; ++i)
{
return 0;
if (i == 0)
{
if (params->settings[i].hEvent)
{
handles[i] = params->settings[i].hEvent;
continue;
}
else
{
InterlockedExchange(&(params->size), NULL);
return 0;
}
}
params->settings[i].hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
if (!params->settings[i].hEvent)
{
InterlockedExchange(&(params->size), 0);
return 0;
}
handles[i] = params->settings[i].hEvent;
if (RegCreateKeyExW(
params->settings[i].origin,
params->settings[i].name,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_READ,
NULL,
&(params->settings[i].hKey),
NULL
) != ERROR_SUCCESS)
{
InterlockedExchange(&(params->size), 0);
return 0;
}
if (RegNotifyChangeKeyValue(
params->settings[i].hKey,
FALSE,
REG_NOTIFY_CHANGE_LAST_SET,
params->settings[i].hEvent,
TRUE
) != ERROR_SUCCESS)
{
InterlockedExchange(&(params->size), 0);
return 0;
}
}
if (RegNotifyChangeKeyValue(
params->settings[i].hKey,
DWORD dwRes = WaitForMultipleObjects(
params->size,
handles,
FALSE,
REG_NOTIFY_CHANGE_LAST_SET,
params->settings[i].hEvent,
TRUE
) != ERROR_SUCCESS)
{
return 0;
}
}
handles[params->size] = params->hExitEvent;
DWORD dwRes = WaitForMultipleObjects(
params->size + (params->hExitEvent ? 1 : 0),
handles,
FALSE,
INFINITE
);
if (dwRes != WAIT_FAILED)
{
unsigned int i = dwRes - WAIT_OBJECT_0;
if (i >= 0 && i < params->size)
{
params->settings[i].callback(params->settings[i].data);
}
else if (i == params->size && params->hExitEvent)
INFINITE
);
if (dwRes != WAIT_FAILED)
{
bShouldExit = TRUE;
unsigned int i = dwRes - WAIT_OBJECT_0;
if (i >= 1 && i < params->size)
{
params->settings[i].callback(params->settings[i].data);
}
else if (i == 0)
{
bShouldExit = TRUE;
}
for (unsigned int j = 1; j < params->size; ++j)
{
if (WaitForSingleObject(handles[j], 0) == WAIT_OBJECT_0)
{
params->settings[j].callback(params->settings[j].data);
}
}
}
for (unsigned int j = 0; j < params->size; ++j)
free(handles);
for (unsigned int i = 1; i < params->size; ++i)
{
if (WaitForSingleObject(handles[j], 0) == WAIT_OBJECT_0)
if (params->settings[i].hEvent)
{
CloseHandle(params->settings[i].hEvent);
}
if (params->settings[i].hKey)
{
params->settings[j].callback(params->settings[j].data);
RegCloseKey(params->settings[i].hKey);
}
}
if (bShouldExit)
{
break;
}
}
free(handles);
for (unsigned int i = 0; i < params->size; ++i)
{
CloseHandle(params->settings[i].hEvent);
RegCloseKey(params->settings[i].hKey);
}
if (bShouldExit)
else
{
break;
InterlockedExchange(&(params->size), 0);
return 0;
}
}
printf("[SettingsMonitor] Ended %p\n", params->settings[0].hEvent);
InterlockedExchange(&(params->size), 0);
return 0;
}

3 changes: 2 additions & 1 deletion ExplorerPatcher/SettingsMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Windows.h>
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
#include <stdio.h>

typedef struct _Setting
{
Expand All @@ -17,7 +18,7 @@ typedef struct _SettingsChangeParameters
{
Setting* settings;
DWORD size;
HANDLE hExitEvent;
HANDLE hThread;
} SettingsChangeParameters;
DWORD WINAPI MonitorSettings(SettingsChangeParameters*);
#endif
Loading

0 comments on commit 04616ce

Please sign in to comment.