Skip to content

Commit 36ba356

Browse files
GurliGebisdonho
authored andcommitted
Fix double entries in some applications (Winzip)
Changed logic to use an enum instead of a counter, making the code more stable and cleaner. Fix notepad-plus-plus/notepad-plus-plus#13499 Close #26
1 parent ca0dcca commit 36ba356

10 files changed

+125
-10
lines changed

BaseNppExplorerCommandHandler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern HMODULE g_module;
1313
BaseNppExplorerCommandHandler::BaseNppExplorerCommandHandler()
1414
{
1515
counter = make_unique<SharedCounter>();
16+
state = make_unique<SharedState>();
1617
}
1718

1819
const wstring BaseNppExplorerCommandHandler::GetNppExecutableFullPath()

BaseNppExplorerCommandHandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "ExplorerCommandBase.h"
33
#include "SharedCounter.h"
4+
#include "SharedState.h"
45

56
using namespace NppShell::Helpers;
67

@@ -24,5 +25,6 @@ namespace NppShell::CommandHandlers
2425

2526
protected:
2627
unique_ptr<SharedCounter> counter;
28+
unique_ptr<SharedState> state;
2729
};
2830
}

ClassicEditWithNppExplorerCommandHandler.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#include "pch.h"
22
#include "ClassicEditWithNppExplorerCommandHandler.h"
33

4+
#include "SharedState.h"
5+
46
using namespace NppShell::CommandHandlers;
57

68
const EXPCMDSTATE ClassicEditWithNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
79
{
810
UNREFERENCED_PARAMETER(psiItemArray);
911

10-
int state = counter->GetValue();
12+
// First we get the current state, before we clear it.
13+
CounterState currentState = state->GetState();
14+
state->SetState(NotSet);
1115

12-
if (state == 3 || state == 5)
16+
// If it is set, it means the State function has been called in the Modern command handler last, which means we should hide this one.
17+
if (currentState == CounterState::Set)
1318
{
14-
// This is on Windows 11, with both the modern and classic being shows, so we hide this one.
1519
return ECS_HIDDEN;
1620
}
1721

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#include "pch.h"
22
#include "ModernEditWithNppExplorerCommandHandler.h"
33

4+
#include "SharedState.h"
5+
46
using namespace NppShell::CommandHandlers;
7+
using namespace NppShell::Helpers;
58

69
const EXPCMDSTATE ModernEditWithNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
710
{
811
UNREFERENCED_PARAMETER(psiItemArray);
912

13+
state->SetState(Set);
14+
1015
return ECS_ENABLED;
1116
}

NppShell.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
<ClInclude Include="pch.h" />
274274
<ClInclude Include="resource.h" />
275275
<ClInclude Include="SharedCounter.h" />
276+
<ClInclude Include="SharedState.h" />
276277
<ClInclude Include="RegistryKey.h" />
277278
<ClInclude Include="SimpleFactory.h" />
278279
<ClInclude Include="ThreadUILanguageChanger.h" />
@@ -295,6 +296,7 @@
295296
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Create</PrecompiledHeader>
296297
</ClCompile>
297298
<ClCompile Include="SharedCounter.cpp" />
299+
<ClCompile Include="SharedState.cpp" />
298300
<ClCompile Include="RegistryKey.cpp" />
299301
<ClCompile Include="ThreadUILanguageChanger.cpp" />
300302
</ItemGroup>

NppShell.vcxproj.filters

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<Filter Include="Registry">
2323
<UniqueIdentifier>{2de5b87c-2f6d-4a04-bd61-1e1be9a91fdf}</UniqueIdentifier>
2424
</Filter>
25+
<Filter Include="Resources">
26+
<UniqueIdentifier>{31c51ab1-0502-40df-b93e-2932b487656e}</UniqueIdentifier>
27+
</Filter>
2528
</ItemGroup>
2629
<ItemGroup>
2730
<ClInclude Include="Installer.h">
@@ -38,7 +41,6 @@
3841
<ClInclude Include="AclHelper.h">
3942
<Filter>Helpers</Filter>
4043
</ClInclude>
41-
<ClInclude Include="SharedCounter.h" />
4244
<ClInclude Include="ExplorerCommandBase.h">
4345
<Filter>CommandHandlers\Base</Filter>
4446
</ClInclude>
@@ -54,10 +56,18 @@
5456
<ClInclude Include="RegistryKey.h">
5557
<Filter>Registry</Filter>
5658
</ClInclude>
57-
<ClInclude Include="resource.h" />
5859
<ClInclude Include="ThreadUILanguageChanger.h">
5960
<Filter>Helpers</Filter>
6061
</ClInclude>
62+
<ClInclude Include="SharedState.h">
63+
<Filter>Helpers</Filter>
64+
</ClInclude>
65+
<ClInclude Include="resource.h">
66+
<Filter>Resources</Filter>
67+
</ClInclude>
68+
<ClInclude Include="SharedCounter.h">
69+
<Filter>Helpers</Filter>
70+
</ClInclude>
6171
</ItemGroup>
6272
<ItemGroup>
6373
<ClCompile Include="Installer.cpp">
@@ -71,7 +81,6 @@
7181
<ClCompile Include="AclHelper.cpp">
7282
<Filter>Helpers</Filter>
7383
</ClCompile>
74-
<ClCompile Include="SharedCounter.cpp" />
7584
<ClCompile Include="ExplorerCommandBase.cpp">
7685
<Filter>CommandHandlers\Base</Filter>
7786
</ClCompile>
@@ -90,6 +99,12 @@
9099
<ClCompile Include="ThreadUILanguageChanger.cpp">
91100
<Filter>Helpers</Filter>
92101
</ClCompile>
102+
<ClCompile Include="SharedState.cpp">
103+
<Filter>Helpers</Filter>
104+
</ClCompile>
105+
<ClCompile Include="SharedCounter.cpp">
106+
<Filter>Helpers</Filter>
107+
</ClCompile>
93108
</ItemGroup>
94109
<ItemGroup>
95110
<None Include="packages.config" />
@@ -99,6 +114,8 @@
99114
<Natvis Include="$(MSBuildThisFileDirectory)..\..\natvis\wil.natvis" />
100115
</ItemGroup>
101116
<ItemGroup>
102-
<ResourceCompile Include="NppShell.rc" />
117+
<ResourceCompile Include="NppShell.rc">
118+
<Filter>Resources</Filter>
119+
</ResourceCompile>
103120
</ItemGroup>
104121
</Project>

SharedCounter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ SharedCounter::SharedCounter()
3939
ReleaseMutex(hMutex);
4040
}
4141

42-
SharedCounter::~SharedCounter() {
42+
SharedCounter::~SharedCounter()
43+
{
4344
// Decrement the shared counter
4445
WaitForSingleObject(hMutex, INFINITE);
4546
*pCounter -= 1;
@@ -53,6 +54,7 @@ SharedCounter::~SharedCounter() {
5354
CloseHandle(hFileMapping);
5455
}
5556

56-
int SharedCounter::GetValue() const {
57+
int SharedCounter::GetValue() const
58+
{
5759
return localValue;
5860
}

SharedCounter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace NppShell::Helpers
44
{
5-
class SharedCounter {
5+
class SharedCounter
6+
{
67
public:
78
SharedCounter();
89
~SharedCounter();

SharedState.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "pch.h"
2+
#include "SharedState.h"
3+
4+
using namespace NppShell::Helpers;
5+
6+
SharedState::SharedState()
7+
{
8+
// Create or open the shared memory mapped file
9+
hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(int), L"Local\\BaseNppExplorerCommandHandlerSharedStateMemory");
10+
if (hFileMapping == NULL)
11+
{
12+
MessageBox(NULL, L"Failed to create or open shared memory mapped file", L"SharedState", MB_OK | MB_ICONERROR);
13+
return;
14+
}
15+
16+
// Create a mutex to synchronize access to the shared memory
17+
hMutex = CreateMutex(NULL, FALSE, L"Local\\BaseNppExplorerCommandHandlerSharedStateMutex");
18+
if (hMutex == NULL)
19+
{
20+
MessageBox(NULL, L"Failed to create mutex", L"SharedState", MB_OK | MB_ICONERROR);
21+
CloseHandle(hFileMapping);
22+
return;
23+
}
24+
25+
// Map the shared memory into the current process's address space
26+
pState = (CounterState*)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(CounterState));
27+
if (pState == NULL)
28+
{
29+
MessageBox(NULL, L"Failed to map shared memory", L"SharedState", MB_OK | MB_ICONERROR);
30+
CloseHandle(hMutex);
31+
CloseHandle(hFileMapping);
32+
return;
33+
}
34+
}
35+
36+
SharedState::~SharedState()
37+
{
38+
// Unmap the shared memory from the current process's address space
39+
UnmapViewOfFile(pState);
40+
41+
// Close the mutex and the shared memory mapped file
42+
CloseHandle(hMutex);
43+
CloseHandle(hFileMapping);
44+
}
45+
46+
CounterState SharedState::GetState() const
47+
{
48+
return *pState;
49+
}
50+
51+
void SharedState::SetState(CounterState state)
52+
{
53+
WaitForSingleObject(hMutex, INFINITE);
54+
*pState = state;
55+
ReleaseMutex(hMutex);
56+
}

SharedState.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
namespace NppShell::Helpers
4+
{
5+
enum CounterState
6+
{
7+
NotSet,
8+
Set
9+
};
10+
11+
class SharedState
12+
{
13+
public:
14+
SharedState();
15+
~SharedState();
16+
17+
CounterState GetState() const;
18+
void SetState(CounterState state);
19+
20+
private:
21+
HANDLE hFileMapping;
22+
HANDLE hMutex;
23+
CounterState* pState;
24+
};
25+
}

0 commit comments

Comments
 (0)