Skip to content

Commit 866a36c

Browse files
mgalliencamilasan
authored andcommitted
add logs to the files explorer integration DLL
Signed-off-by: Matthieu Gallien <[email protected]>
1 parent ca08119 commit 866a36c

11 files changed

+73
-27
lines changed

shell_integration/windows/NCContextMenu/NCClientInterface.cpp

+21-7
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,54 @@
2626
#include <sstream>
2727
#include <iterator>
2828
#include <unordered_set>
29+
#include <locale>
30+
#include <codecvt>
2931

3032
using namespace std;
3133

3234
#define PIPE_TIMEOUT 5*1000 //ms
3335

34-
NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstring &files)
36+
NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstring &files, std::ofstream &logger)
3537
{
3638
auto pipename = CommunicationSocket::DefaultPipePath();
3739

3840
CommunicationSocket socket;
3941
if (!WaitNamedPipe(pipename.data(), PIPE_TIMEOUT)) {
42+
logger << "error with WaitNamedPipe" << std::endl;
4043
return {};
4144
}
4245
if (!socket.Connect(pipename)) {
46+
logger << "error with Connect" << std::endl;
4347
return {};
4448
}
45-
socket.SendMsg(L"GET_STRINGS:CONTEXT_MENU_TITLE\n");
46-
socket.SendMsg((L"GET_MENU_ITEMS:" + files + L"\n").data());
49+
socket.SendMsg(L"GET_STRINGS:CONTEXT_MENU_TITLE\n", logger);
50+
socket.SendMsg((L"GET_MENU_ITEMS:" + files + L"\n").data(), logger);
4751

4852
ContextMenuInfo info;
4953
std::wstring response;
5054
int sleptCount = 0;
55+
5156
constexpr auto noReplyTimeout = 20;
5257
constexpr auto replyTimeout = 200;
5358
bool receivedReplyFromDesktopClient = false;
5459
while ((!receivedReplyFromDesktopClient && sleptCount < noReplyTimeout) || (receivedReplyFromDesktopClient && sleptCount < replyTimeout)) {
60+
logger << "trying to read a line" << std::endl;
5561
if (socket.ReadLine(&response)) {
62+
//logger << "received: " << converter.to_bytes(response) << std::endl;
5663
if (StringUtil::begins_with(response, wstring(L"REGISTER_PATH:"))) {
64+
logger << "received: REGISTER_PATH" << std::endl;
5765
wstring responsePath = response.substr(14); // length of REGISTER_PATH
5866
info.watchedDirectories.push_back(responsePath);
5967
}
6068
else if (StringUtil::begins_with(response, wstring(L"STRING:"))) {
69+
logger << "received: STRING" << std::endl;
6170
wstring stringName, stringValue;
6271
if (!StringUtil::extractChunks(response, stringName, stringValue))
6372
continue;
6473
if (stringName == L"CONTEXT_MENU_TITLE")
6574
info.contextMenuTitle = move(stringValue);
6675
} else if (StringUtil::begins_with(response, wstring(L"MENU_ITEM:"))) {
76+
logger << "received: MENU_ITEM" << std::endl;
6777
wstring commandName, flags, title;
6878
if (!StringUtil::extractChunks(response, commandName, flags, title))
6979
continue;
@@ -72,18 +82,22 @@ NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstri
7282
receivedReplyFromDesktopClient = true;
7383
continue;
7484
} else if (StringUtil::begins_with(response, wstring(L"GET_MENU_ITEMS:END"))) {
85+
logger << "received: GET_MENU_ITEMS:END" << std::endl;
7586
break; // Stop once we completely received the last sent request
87+
} else {
88+
logger << "received: another reply" << std::endl;
7689
}
77-
}
78-
else {
90+
} else {
91+
logger << "received nothing" << std::endl;
7992
Sleep(50);
8093
++sleptCount;
8194
}
8295
}
96+
8397
return info;
8498
}
8599

86-
void NCClientInterface::SendRequest(const wchar_t *verb, const std::wstring &path)
100+
void NCClientInterface::SendRequest(const wchar_t *verb, const std::wstring &path, std::ofstream &logger)
87101
{
88102
auto pipename = CommunicationSocket::DefaultPipePath();
89103

@@ -95,5 +109,5 @@ void NCClientInterface::SendRequest(const wchar_t *verb, const std::wstring &pat
95109
return;
96110
}
97111

98-
socket.SendMsg((verb + (L":" + path + L"\n")).data());
112+
socket.SendMsg((verb + (L":" + path + L"\n")).data(), logger);
99113
}

shell_integration/windows/NCContextMenu/NCClientInterface.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <mutex>
3838
#include <atomic>
3939
#include <condition_variable>
40+
#include <fstream>
4041

4142
class CommunicationSocket;
4243

@@ -52,8 +53,8 @@ class NCClientInterface
5253
};
5354
std::vector<MenuItem> menuItems;
5455
};
55-
static ContextMenuInfo FetchInfo(const std::wstring &files);
56-
static void SendRequest(const wchar_t *verb, const std::wstring &path);
56+
static ContextMenuInfo FetchInfo(const std::wstring &files, std::ofstream &logger);
57+
static void SendRequest(const wchar_t *verb, const std::wstring &path, std::ofstream &logger);
5758
};
5859

5960
#endif //ABSTRACTSOCKETHANDLER_H

shell_integration/windows/NCContextMenu/NCContextMenu.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@
2121
#include <StringUtil.h>
2222
#include <strsafe.h>
2323

24+
#include <iostream>
25+
#include <fstream>
26+
2427
extern long g_cDllRef;
2528

2629
NCContextMenu::NCContextMenu(void)
2730
: m_cRef(1)
2831
{
2932
InterlockedIncrement(&g_cDllRef);
33+
m_logger.open("c:\\test.log");
34+
m_logger << "hello world" << std::endl;
3035
}
3136

3237
NCContextMenu::~NCContextMenu(void)
3338
{
39+
m_logger << "NCContextMenu::~NCContextMenu" << std::endl;
3440
InterlockedDecrement(&g_cDllRef);
3541
}
3642

@@ -39,6 +45,7 @@ NCContextMenu::~NCContextMenu(void)
3945
// Query to the interface the component supported.
4046
IFACEMETHODIMP NCContextMenu::QueryInterface(REFIID riid, void **ppv)
4147
{
48+
m_logger << "NCContextMenu::QueryInterface" << std::endl;
4249
static const QITAB qit[] =
4350
{
4451
QITABENT(NCContextMenu, IContextMenu),
@@ -51,12 +58,14 @@ IFACEMETHODIMP NCContextMenu::QueryInterface(REFIID riid, void **ppv)
5158
// Increase the reference count for an interface on an object.
5259
IFACEMETHODIMP_(ULONG) NCContextMenu::AddRef()
5360
{
61+
m_logger << "NCContextMenu::AddRef" << std::endl;
5462
return InterlockedIncrement(&m_cRef);
5563
}
5664

5765
// Decrease the reference count for an interface on an object.
5866
IFACEMETHODIMP_(ULONG) NCContextMenu::Release()
5967
{
68+
m_logger << "NCContextMenu::Release" << std::endl;
6069
ULONG cRef = InterlockedDecrement(&m_cRef);
6170
if (0 == cRef) {
6271
delete this;
@@ -74,6 +83,7 @@ IFACEMETHODIMP_(ULONG) NCContextMenu::Release()
7483
IFACEMETHODIMP NCContextMenu::Initialize(
7584
LPCITEMIDLIST, LPDATAOBJECT pDataObj, HKEY)
7685
{
86+
m_logger << "NCContextMenu::Initialize" << std::endl;
7787
m_selectedFiles.clear();
7888

7989
if (!pDataObj) {
@@ -129,17 +139,20 @@ void InsertSeperator(HMENU hMenu, UINT indexMenu)
129139

130140
IFACEMETHODIMP NCContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
131141
{
142+
m_logger << "NCContextMenu::QueryContextMenu" << std::endl;
132143
// If uFlags include CMF_DEFAULTONLY then we should not do anything.
133144
if (CMF_DEFAULTONLY & uFlags)
134145
{
135146
return MAKE_HRESULT(SEVERITY_SUCCESS, 0, USHORT(0));
136147
}
137148

138-
m_info = NCClientInterface::FetchInfo(m_selectedFiles);
149+
m_info = NCClientInterface::FetchInfo(m_selectedFiles, m_logger);
139150
if (m_info.menuItems.empty()) {
151+
m_logger << "NCContextMenu::QueryContextMenu " << "empty info" << std::endl;
140152
return MAKE_HRESULT(SEVERITY_SUCCESS, 0, USHORT(0));
141153
}
142154

155+
m_logger << "NCContextMenu::QueryContextMenu" << "insert separator" << std::endl;
143156
InsertSeperator(hMenu, indexMenu++);
144157

145158
HMENU hSubmenu = CreateMenu();
@@ -153,6 +166,7 @@ IFACEMETHODIMP NCContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT
153166
if (!InsertMenuItem(hMenu, indexMenu++, TRUE, &mii))
154167
return HRESULT_FROM_WIN32(GetLastError());
155168
}
169+
m_logger << "NCContextMenu::QueryContextMenu" << "insert separator" << std::endl;
156170
InsertSeperator(hMenu, indexMenu++);
157171

158172
UINT indexSubMenu = 0;
@@ -179,6 +193,7 @@ IFACEMETHODIMP NCContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT
179193

180194
IFACEMETHODIMP NCContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
181195
{
196+
m_logger << "NCContextMenu::InvokeCommand" << std::endl;
182197
std::wstring command;
183198

184199
CMINVOKECOMMANDINFOEX *piciEx = nullptr;
@@ -215,13 +230,14 @@ IFACEMETHODIMP NCContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
215230
return E_FAIL;
216231
}
217232

218-
NCClientInterface::SendRequest(command.data(), m_selectedFiles);
233+
NCClientInterface::SendRequest(command.data(), m_selectedFiles, m_logger);
219234
return S_OK;
220235
}
221236

222237
IFACEMETHODIMP NCContextMenu::GetCommandString(UINT_PTR idCommand,
223238
UINT uFlags, UINT *pwReserved, LPSTR pszName, UINT cchMax)
224239
{
240+
m_logger << "NCContextMenu::GetCommandString" << std::endl;
225241
if (idCommand < m_info.menuItems.size() && uFlags == GCS_VERBW) {
226242
return StringCchCopyW(reinterpret_cast<PWSTR>(pszName), cchMax,
227243
m_info.menuItems[idCommand].command.data());

shell_integration/windows/NCContextMenu/NCContextMenu.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
#define NCCONTEXTMENU_H
1717

1818
#pragma once
19+
#include "NCClientInterface.h"
20+
1921
#include <shlobj.h> // For IShellExtInit and IContextMenu
22+
2023
#include <string>
21-
#include "NCClientInterface.h"
24+
#include <fstream>
25+
#include <iostream>
2226

2327
class NCContextMenu : public IShellExtInit, public IContextMenu
2428
{
@@ -48,6 +52,8 @@ class NCContextMenu : public IShellExtInit, public IContextMenu
4852
// The name of the selected files (separated by '\x1e')
4953
std::wstring m_selectedFiles;
5054
NCClientInterface::ContextMenuInfo m_info;
55+
56+
std::ofstream m_logger;
5157
};
5258

5359
#endif //NCCONTEXTMENU_H

shell_integration/windows/NCOverlays/NCOverlay.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ namespace {
3535

3636
unique_ptr<RemotePathChecker> s_instance;
3737

38-
RemotePathChecker *getGlobalChecker()
38+
RemotePathChecker *getGlobalChecker(ofstream &logger)
3939
{
4040
// On Vista we'll run into issue #2680 if we try to create the thread+pipe connection
4141
// on any DllGetClassObject of our registered classes.
4242
// Work around the issue by creating the static RemotePathChecker only once actually needed.
4343
static once_flag s_onceFlag;
44-
call_once(s_onceFlag, [] { s_instance.reset(new RemotePathChecker); });
44+
call_once(s_onceFlag, [&logger] { s_instance.reset(new RemotePathChecker{logger}); });
4545

4646
return s_instance.get();
4747
}
4848

4949
}
50-
NCOverlay::NCOverlay(int state)
50+
NCOverlay::NCOverlay(int state)
5151
: _referenceCount(1)
5252
, _state(state)
5353
{
54+
m_logger.open("c:\\testOverlay.log");
55+
m_logger << "hello world" << std::endl;
5456
}
5557

5658
NCOverlay::~NCOverlay(void)
@@ -120,7 +122,7 @@ IFACEMETHODIMP NCOverlay::GetPriority(int *pPriority)
120122

121123
IFACEMETHODIMP NCOverlay::IsMemberOf(PCWSTR pwszPath, DWORD dwAttrib)
122124
{
123-
RemotePathChecker* checker = getGlobalChecker();
125+
RemotePathChecker* checker = getGlobalChecker(m_logger);
124126
std::shared_ptr<const std::vector<std::wstring>> watchedDirectories = checker->WatchedDirectories();
125127

126128
if (watchedDirectories->empty()) {

shell_integration/windows/NCOverlays/NCOverlay.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
#pragma once
1919

2020
#include <shlobj.h>
21+
#include <fstream>
2122

2223
class NCOverlay : public IShellIconOverlayIdentifier
2324

2425
{
2526
public:
26-
NCOverlay(int state);
27+
explicit NCOverlay(int state);
2728

2829
IFACEMETHODIMP_(ULONG) AddRef();
2930
IFACEMETHODIMP GetOverlayInfo(PWSTR pwszIconFile, int cchMax, int *pIndex, DWORD *pdwFlags);
@@ -38,6 +39,7 @@ class NCOverlay : public IShellIconOverlayIdentifier
3839
private:
3940
long _referenceCount;
4041
int _state;
42+
std::ofstream m_logger;
4143
};
4244

43-
#endif
45+
#endif

shell_integration/windows/NCUtil/CommunicationSocket.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ bool CommunicationSocket::Connect(const std::wstring &pipename)
8181
return true;
8282
}
8383

84-
bool CommunicationSocket::SendMsg(const wchar_t* message) const
84+
bool CommunicationSocket::SendMsg(const wchar_t* message, std::ofstream &logger) const
8585
{
86+
logger << "CommunicationSocket::SendMsg: " << (*message) << std::endl;
87+
8688
auto utf8_msg = StringUtil::toUtf8(message);
8789

8890
DWORD numBytesWritten = 0;

shell_integration/windows/NCUtil/CommunicationSocket.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <string>
2323
#include <vector>
24+
#include <fstream>
2425
#include <WinSock2.h>
2526

2627
class __declspec(dllexport) CommunicationSocket
@@ -34,7 +35,7 @@ class __declspec(dllexport) CommunicationSocket
3435
bool Connect(const std::wstring& pipename);
3536
bool Close();
3637

37-
bool SendMsg(const wchar_t*) const;
38+
bool SendMsg(const wchar_t*, std::ofstream &logger) const;
3839
bool ReadLine(std::wstring*);
3940

4041
HANDLE Event() { return _pipe; }

shell_integration/windows/NCUtil/RemotePathChecker.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
using namespace std;
3131

3232
// This code is run in a thread
33-
void RemotePathChecker::workerThreadLoop()
33+
void RemotePathChecker::workerThreadLoop(ofstream &logger)
3434
{
3535
auto pipename = CommunicationSocket::DefaultPipePath();
3636
bool connected = false;
@@ -62,7 +62,7 @@ void RemotePathChecker::workerThreadLoop()
6262
lock.unlock();
6363
if (!asked.count(filePath)) {
6464
asked.insert(filePath);
65-
socket.SendMsg(wstring(L"RETRIEVE_FILE_STATUS:" + filePath + L'\n').data());
65+
socket.SendMsg(wstring(L"RETRIEVE_FILE_STATUS:" + filePath + L'\n').data(), logger);
6666
}
6767
lock.lock();
6868
}
@@ -162,12 +162,12 @@ void RemotePathChecker::workerThreadLoop()
162162

163163

164164

165-
RemotePathChecker::RemotePathChecker()
165+
RemotePathChecker::RemotePathChecker(ofstream &logger)
166166
: _stop(false)
167167
, _watchedDirectories(make_shared<const vector<wstring>>())
168168
, _connected(false)
169169
, _newQueries(CreateEvent(nullptr, FALSE, FALSE, nullptr))
170-
, _thread([this]{ this->workerThreadLoop(); })
170+
, _thread([this, &logger]{ this->workerThreadLoop(logger); })
171171
{
172172
}
173173

shell_integration/windows/NCUtil/RemotePathChecker.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <memory>
2323
#include <mutex>
2424
#include <atomic>
25+
#include <fstream>
2526
#include <condition_variable>
2627

2728
#pragma once
@@ -36,7 +37,7 @@ class RemotePathChecker {
3637
StateWarning,
3738
StateNone
3839
};
39-
RemotePathChecker();
40+
explicit RemotePathChecker(std::ofstream &logger);
4041
~RemotePathChecker();
4142
std::shared_ptr<const std::vector<std::wstring>> WatchedDirectories() const;
4243
bool IsMonitoredPath(const wchar_t* filePath, int* state);
@@ -64,7 +65,7 @@ class RemotePathChecker {
6465
HANDLE _newQueries;
6566

6667
std::thread _thread;
67-
void workerThreadLoop();
68+
void workerThreadLoop(std::ofstream &logger);
6869
};
6970

70-
#endif
71+
#endif

0 commit comments

Comments
 (0)