Skip to content

Commit c433255

Browse files
author
Jan Wilmans
committed
actually start / stop the service
1 parent 779a0e3 commit c433255

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

application/DebugViewpp/MainFrame.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ void CMainFrame::OnLogGlobal(UINT /*uNotifyCode*/, int /*nID*/, CWindow /*wndCtl
15171517
UpdateTitle();
15181518
}
15191519

1520-
static const auto driver_name = "dbgv.sys";
1520+
static const auto driver_name = "C:\\Windows\\System32\\drivers\\dbgvpp.sys";
15211521

15221522
void WriteDriverFromResource()
15231523
{
@@ -1535,6 +1535,7 @@ void WriteDriverFromResource()
15351535
void* pLockedRes = LockResource(hLoadedRes);
15361536
if (pLockedRes)
15371537
{
1538+
std::cout << "write to " << driver_name << "\n";
15381539
std::ofstream outFile(driver_name, std::ios::binary);
15391540
outFile.write(static_cast<const char*>(pLockedRes), dwSize);
15401541
outFile.close();

application/DebugViewppLib/Debugview_kernel_client.cpp

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,76 @@
77

88
constexpr const char* DRIVER_SERVICE_NAME = "debugviewdriver";
99
constexpr const char* DRIVER_DISPLAY_NAME = "DbgView Kernel Message Driver";
10-
const std::string driverPath = "dbgv.sys";
10+
const std::string driverPath = "C:\\Windows\\System32\\drivers\\dbgvpp.sys";
11+
12+
bool FileExists(const std::string& path) {
13+
DWORD fileAttributes = GetFileAttributesA(path.c_str());
14+
return (fileAttributes != INVALID_FILE_ATTRIBUTES &&
15+
!(fileAttributes & FILE_ATTRIBUTE_DIRECTORY));
16+
}
17+
18+
bool StartDriverSvc()
19+
{
20+
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
21+
if (!hSCManager) return false;
22+
23+
SC_HANDLE hService = OpenServiceA(hSCManager, DRIVER_SERVICE_NAME, SERVICE_START);
24+
if (!hService) {
25+
CloseServiceHandle(hSCManager);
26+
return false;
27+
}
28+
29+
if (!StartService(hService, 0, NULL)) {
30+
if (GetLastError() == ERROR_SERVICE_ALREADY_RUNNING) {
31+
std::cout << "Service is already running.\n";
32+
CloseServiceHandle(hService);
33+
CloseServiceHandle(hSCManager);
34+
return true;
35+
}
36+
std::cout << "Failed to start service. Error: " << GetLastError() << std::endl;
37+
CloseServiceHandle(hService);
38+
CloseServiceHandle(hSCManager);
39+
return false;
40+
}
41+
42+
CloseServiceHandle(hService);
43+
CloseServiceHandle(hSCManager);
44+
return true;
45+
}
46+
47+
bool StopDriverSvc()
48+
{
49+
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
50+
if (!hSCManager) return false;
51+
52+
SC_HANDLE hService = OpenServiceA(hSCManager, DRIVER_SERVICE_NAME, SERVICE_STOP);
53+
if (!hService) {
54+
CloseServiceHandle(hSCManager);
55+
return false;
56+
}
57+
58+
SERVICE_STATUS status;
59+
if (!ControlService(hService, SERVICE_CONTROL_STOP, &status)) {
60+
DWORD err = GetLastError();
61+
if (err != ERROR_SERVICE_NOT_ACTIVE) {
62+
std::cout << "Failed to stop service. Error: " << err << std::endl;
63+
}
64+
}
65+
66+
CloseServiceHandle(hService);
67+
CloseServiceHandle(hSCManager);
68+
return true;
69+
}
1170

1271
void InstallKernelMessagesDriver()
1372
{
1473
// try to uninstall first, in case the driver is somehow still loaded.
1574
UninstallKernelMessagesDriver();
1675

76+
if (!FileExists(driverPath)) {
77+
std::cout << "Driver file not found at: " << driverPath << std::endl;
78+
}
79+
1780
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1881
if (!hSCManager) {
1982
std::cout << "Failed to open Service Control Manager. Error: " << GetLastError() << std::endl;
@@ -41,10 +104,14 @@ void InstallKernelMessagesDriver()
41104
}
42105
CloseServiceHandle(hService);
43106
CloseServiceHandle(hSCManager);
107+
108+
StartDriverSvc();
44109
}
45110

46111
void UninstallKernelMessagesDriver()
47112
{
113+
StopDriverSvc();
114+
48115
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
49116
if (!hSCManager)
50117
{

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-
printf("Could not connect to kernel messages driver");
27-
throw std::runtime_error("Could not connect to kernel messages driver");
26+
printf("Could not find the kernel messages driver device name");
27+
throw std::runtime_error("Could not find the kernel messages driver device name");
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-
throw std::runtime_error("Could not connect to kernel messages driver");
37+
throw std::runtime_error("Could not enable capturing kernel messages");
3838
}
3939

4040
m_handle = std::move(handle);

0 commit comments

Comments
 (0)