Skip to content

Commit 574001d

Browse files
author
Jan Wilmans
committed
fixed resource embedding and proper error handling when permissions are missing
1 parent b3367f8 commit 574001d

File tree

11 files changed

+88
-40
lines changed

11 files changed

+88
-40
lines changed

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
* text=auto eol=crlf
22
*.sh text=auto eol=lf
3+
4+
# Define binary files
5+
*.sys -text diff
6+
*.sln -text diff
7+
*.suo -text diff
8+
*.vcxproj -text diff
9+
*.gif -text diff
10+
*.ico -text diff
11+
*.jpeg -text diff
12+
*.jpg -text diff
13+
*.png -text diff

application/DebugViewpp/DebugView++.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
#include "atleverything.h"
1616

17+
#include <filesystem>
18+
#include <cstdlib> // for std::getenv
19+
1720
//#define ENABLE_CRASHPAD
1821
#ifdef ENABLE_CRASHPAD
1922
#include "crashpad.h"
@@ -88,25 +91,26 @@ int ForwardMessagesFromPipe(HANDLE hPipe)
8891
return 0;
8992
}
9093

91-
void WriteDriverFromResource()
94+
class DebugConsole
9295
{
93-
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_DBGV_DRIVER), RT_RCDATA);
94-
if (hRes)
96+
public:
97+
DebugConsole()
9598
{
96-
HGLOBAL hLoadedRes = LoadResource(NULL, hRes);
97-
if (hLoadedRes)
98-
{
99-
DWORD dwSize = SizeofResource(NULL, hRes);
100-
void* pLockedRes = LockResource(hLoadedRes);
101-
if (pLockedRes)
102-
{
103-
std::ofstream outFile("dbgv.sys", std::ios::binary);
104-
outFile.write(static_cast<const char*>(pLockedRes), dwSize);
105-
outFile.close();
106-
}
107-
}
99+
::AllocConsole();
100+
::freopen_s(&standardOut, "CONOUT$", "wb", stdout);
101+
std::cout.clear();
108102
}
109-
}
103+
104+
~DebugConsole()
105+
{
106+
fclose(standardOut);
107+
fclose(stdout);
108+
FreeConsole();
109+
}
110+
111+
private:
112+
FILE* standardOut = nullptr;
113+
};
110114

111115
int Main(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR /*lpstrCmdLine*/, int cmdShow)
112116
{
@@ -121,13 +125,11 @@ int Main(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR /*lpstrCmdLine
121125

122126
AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
123127

124-
#ifdef CONSOLE_DEBUG
125-
FILE* standardOut;
126-
AllocConsole();
127-
freopen_s(&standardOut, "CONOUT$", "wb", stdout);
128-
auto fileGuard = make_guard([standardOut] { fclose(standardOut); });
129-
std::cout.clear();
130-
#endif
128+
std::unique_ptr<DebugConsole> debugConsole;
129+
if (std::getenv("DEBUGVIEWPP_CONSOLE_DEBUG") != nullptr)
130+
{
131+
debugConsole = std::make_unique<DebugConsole>();
132+
}
131133

132134
CAppModuleInitialization moduleInit(_Module, hInstance);
133135

application/DebugViewpp/MainFrame.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ CMainFrame::CMainFrame() :
192192
m_notifyIconData.cbSize = 0;
193193
}
194194

195-
CMainFrame::~CMainFrame() = default;
195+
CMainFrame::~CMainFrame()
196+
{
197+
RemoveDriver();
198+
}
196199

197200
void CMainFrame::SetLogging()
198201
{
@@ -284,11 +287,6 @@ void CMainFrame::OnClose()
284287
Shell_NotifyIcon(NIM_DELETE, &m_notifyIconData);
285288
m_notifyIconData.cbSize = 0;
286289
}
287-
288-
#ifdef CONSOLE_DEBUG
289-
fclose(stdout);
290-
FreeConsole();
291-
#endif
292290
}
293291

294292
LRESULT CMainFrame::OnQueryEndSession(WPARAM /*unused*/, LPARAM /*unused*/)
@@ -1519,18 +1517,51 @@ void CMainFrame::OnLogGlobal(UINT /*uNotifyCode*/, int /*nID*/, CWindow /*wndCtl
15191517
UpdateTitle();
15201518
}
15211519

1520+
static const auto driver_name = "dbgv.sys";
1521+
1522+
void WriteDriverFromResource()
1523+
{
1524+
if (std::filesystem::exists(driver_name))
1525+
{
1526+
return;
1527+
}
1528+
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_DBGV_DRIVER), RT_RCDATA);
1529+
if (hRes)
1530+
{
1531+
HGLOBAL hLoadedRes = LoadResource(NULL, hRes);
1532+
if (hLoadedRes)
1533+
{
1534+
DWORD dwSize = SizeofResource(NULL, hRes);
1535+
void* pLockedRes = LockResource(hLoadedRes);
1536+
if (pLockedRes)
1537+
{
1538+
std::ofstream outFile(driver_name, std::ios::binary);
1539+
outFile.write(static_cast<const char*>(pLockedRes), dwSize);
1540+
outFile.close();
1541+
}
1542+
}
1543+
}
1544+
}
1545+
1546+
void RemoveDriver()
1547+
{
1548+
std::filesystem::remove(driver_name);
1549+
}
1550+
15221551
void CMainFrame::OnLogKernel(UINT /*uNotifyCode*/, int /*nID*/, CWindow /*wndCtl*/)
15231552
{
15241553
m_tryKernel = (m_pKernelReader == nullptr);
15251554

15261555
if (m_tryKernel)
15271556
{
1557+
WriteDriverFromResource();
15281558
Resume();
15291559
}
15301560
else
15311561
{
15321562
m_logSources.Remove(m_pKernelReader);
15331563
m_pKernelReader = nullptr;
1564+
RemoveDriver();
15341565
}
15351566
UpdateTitle();
15361567
}

application/DebugViewpp/MainFrame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class DbgviewReader;
4444

4545
using SelectedTabItem = CLogViewTabItem;
4646

47+
void RemoveDriver();
48+
4749
class CMainFrame : public CTabbedFrameImpl<CMainFrame, CDotNetTabCtrl<SelectedTabItem>>,
4850
public CUpdateUI<CMainFrame>,
4951
public ExceptionHandler<CMainFrame, std::exception>,
22.8 KB
Binary file not shown.

application/DebugViewpp/resource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,4 @@
141141
#define _APS_NEXT_SYMED_VALUE 107
142142
#endif
143143
#endif
144+

application/DebugViewpp/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#define VERSION 1,9,0,58
2-
#define VERSION_STR "1.9.0.58"
1+
#define VERSION 1,9,0,63
2+
#define VERSION_STR "1.9.0.63"

application/DebugViewpp/version.wxi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<?define ProductVersion.Major="1" ?>
44
<?define ProductVersion.Minor="9" ?>
55
<?define ProductVersion.Revision="0" ?>
6-
<?define ProductVersion.Build="58" ?>
7-
<?define ProductVersion="1.9.0.58" ?>
6+
<?define ProductVersion.Build="63" ?>
7+
<?define ProductVersion="1.9.0.63" ?>
88
</Include>

application/DebugViewppLib/Debugview_kernel_client.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ void UninstallKernelMessagesDriver()
4848
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
4949
if (!hSCManager)
5050
{
51-
std::cout << "Failed to open Service Control Manager. Error: " << GetLastError() << std::endl;
51+
std::cout << "Failed to open Service Control Manager for uninstall. Error: " << GetLastError() << std::endl;
5252
return;
5353
}
5454

5555
SC_HANDLE hService = OpenServiceA(hSCManager, DRIVER_SERVICE_NAME, DELETE);
5656
if (!hService)
5757
{
58-
std::cout << "Failed to open service. Error: " << GetLastError() << std::endl;
58+
// this happens when the service is first loaded, because we always uninstall before installing
59+
// std::cout << "Failed to open service for uninstall. Error: " << GetLastError() << std::endl;
5960
CloseServiceHandle(hSCManager);
6061
return;
6162
}

application/DebugViewppLib/KernelReader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ void KernelReader::StartListening()
2323
NULL));
2424
if (handle.get() == INVALID_HANDLE_VALUE)
2525
{
26-
AddMessage(0, "internal", "Could not connected to kernel messages driver");
27-
return;
26+
printf("Could not connect to kernel messages driver");
27+
throw std::runtime_error("Could not connect to kernel messages driver");
2828
}
2929

3030
// enable capture
@@ -34,7 +34,7 @@ void KernelReader::StartListening()
3434
if (!bRet)
3535
{
3636
printf("DBGV_CAPTURE_KERNEL failed, err=%d\n", ::GetLastError());
37-
return;
37+
throw std::runtime_error("Could not connect to kernel messages driver");
3838
}
3939

4040
m_handle = std::move(handle);

0 commit comments

Comments
 (0)