Skip to content

Commit

Permalink
[balloon] clean up pre-Win10 code (usermode part).
Browse files Browse the repository at this point in the history
Signed-off-by: Vadim Rozenfeld <[email protected]>
  • Loading branch information
vrozenfe committed Jan 17, 2024
1 parent 91a6857 commit 74e209f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 150 deletions.
12 changes: 6 additions & 6 deletions Balloon/app/blnsvr.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;UNIVERSAL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\sys\</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand All @@ -173,7 +173,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;UNIVERSAL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\sys\</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand All @@ -194,7 +194,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;UNIVERSAL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\sys\</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand All @@ -215,7 +215,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;UNIVERSAL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\sys\</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand All @@ -236,7 +236,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;UNIVERSAL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\sys\</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand All @@ -257,7 +257,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;UNIVERSAL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\sys\</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand Down
92 changes: 2 additions & 90 deletions Balloon/app/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ VOID CDevice::Fini()

DWORD CDevice::Run()
{
PWCHAR DevicePath = GetDevicePath((LPGUID)&GUID_DEVINTERFACE_BALLOON);
PWCHAR DevicePath = GetDevicePath((LPGUID)&GUID_DEVINTERFACE_BALLOON);
if (DevicePath == NULL) {
PrintMessage("File not found.");
return ERROR_FILE_NOT_FOUND;
Expand All @@ -85,7 +85,7 @@ DWORD CDevice::Run()
return GetLastError();
}

NOTIFY_HANDLE devnotify = m_pService->RegisterDeviceHandleNotification(
HCMNOTIFICATION devnotify = m_pService->RegisterDeviceHandleNotification(
hDevice);

if (!devnotify) {
Expand Down Expand Up @@ -192,92 +192,6 @@ DWORD WINAPI CDevice::DeviceThread(LPDWORD lParam)
return pDev->Run();
}

#ifndef UNIVERSAL

PTCHAR CDevice::GetDevicePath( IN LPGUID InterfaceGuid )
{
HDEVINFO HardwareDeviceInfo;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData = NULL;
ULONG Length, RequiredLength = 0;
BOOL bResult;
PTCHAR DevicePath;

HardwareDeviceInfo = SetupDiGetClassDevs(
InterfaceGuid,
NULL,
NULL,
(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)
);

if (HardwareDeviceInfo == INVALID_HANDLE_VALUE) {
PrintMessage("Cannot get class devices");
return NULL;
}

DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

bResult = SetupDiEnumDeviceInterfaces(
HardwareDeviceInfo,
0,
InterfaceGuid,
0,
&DeviceInterfaceData
);

if (bResult == FALSE) {
PrintMessage("Cannot get enumerate device interfaces");
SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
return NULL;
}

SetupDiGetDeviceInterfaceDetail(
HardwareDeviceInfo,
&DeviceInterfaceData,
NULL,
0,
&RequiredLength,
NULL
);

DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LMEM_FIXED, RequiredLength);

if (DeviceInterfaceDetailData == NULL) {
PrintMessage("Cannot allocate memory");
SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
return NULL;
}

DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

Length = RequiredLength;

bResult = SetupDiGetDeviceInterfaceDetail(
HardwareDeviceInfo,
&DeviceInterfaceData,
DeviceInterfaceDetailData,
Length,
&RequiredLength,
NULL
);

if (bResult == FALSE) {
PrintMessage("Cannot get device interface details");
SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
LocalFree(DeviceInterfaceDetailData);
return NULL;
}

DevicePath = _tcsdup(DeviceInterfaceDetailData->DevicePath);

SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
LocalFree(DeviceInterfaceDetailData);

return DevicePath;
}

#else // UNIVERSAL

PTCHAR CDevice::GetDevicePath(IN LPGUID InterfaceGuid)
{
PWSTR DeviceInterfaceList = NULL;
Expand Down Expand Up @@ -327,5 +241,3 @@ PTCHAR CDevice::GetDevicePath(IN LPGUID InterfaceGuid)

return DevicePath;
}

#endif // UNIVERSAL
43 changes: 5 additions & 38 deletions Balloon/app/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ void CService::GetStatus(SC_HANDLE service)
SendStatusToSCM(CurrentState, NO_ERROR, 0, 0, 0);
}

#ifdef UNIVERSAL

DWORD WINAPI CService::DeviceNotificationCallback(HCMNOTIFICATION Notify,
PVOID Context, CM_NOTIFY_ACTION Action, PCM_NOTIFY_EVENT_DATA EventData,
DWORD EventDataSize)
Expand Down Expand Up @@ -296,13 +294,10 @@ VOID WINAPI UnregisterNotificationWork(PTP_CALLBACK_INSTANCE Instance,
CloseThreadpoolWork(Work);
}

#endif // UNIVERSAL

NOTIFY_HANDLE CService::RegisterDeviceInterfaceNotification()
HCMNOTIFICATION CService::RegisterDeviceInterfaceNotification()
{
NOTIFY_HANDLE handle = NULL;
HCMNOTIFICATION handle = NULL;

#ifdef UNIVERSAL
CM_NOTIFY_FILTER filter;
CONFIGRET cr;

Expand All @@ -319,26 +314,13 @@ NOTIFY_HANDLE CService::RegisterDeviceInterfaceNotification()
SetLastError(CM_MapCrToWin32Err(cr, ERROR_NOT_SUPPORTED));
}

#else // UNIVERSAL
DEV_BROADCAST_DEVICEINTERFACE filter;

ZeroMemory(&filter, sizeof(filter));
filter.dbcc_size = sizeof(filter);
filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filter.dbcc_classguid = GUID_DEVINTERFACE_BALLOON;

handle = ::RegisterDeviceNotification(m_StatusHandle, &filter,
DEVICE_NOTIFY_SERVICE_HANDLE);
#endif // UNIVERSAL

return handle;
}

NOTIFY_HANDLE CService::RegisterDeviceHandleNotification(HANDLE DeviceHandle)
HCMNOTIFICATION CService::RegisterDeviceHandleNotification(HANDLE DeviceHandle)
{
NOTIFY_HANDLE handle = NULL;
HCMNOTIFICATION handle = NULL;

#ifdef UNIVERSAL
CM_NOTIFY_FILTER filter;
CONFIGRET cr;

Expand All @@ -354,28 +336,16 @@ NOTIFY_HANDLE CService::RegisterDeviceHandleNotification(HANDLE DeviceHandle)
{
SetLastError(CM_MapCrToWin32Err(cr, ERROR_NOT_SUPPORTED));
}
#else // UNIVERSAL
DEV_BROADCAST_HANDLE filter;

ZeroMemory(&filter, sizeof(filter));
filter.dbch_size = sizeof(filter);
filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
filter.dbch_handle = DeviceHandle;

handle = ::RegisterDeviceNotification(m_StatusHandle, &filter,
DEVICE_NOTIFY_SERVICE_HANDLE);
#endif // UNIVERSAL

return handle;
}

BOOL CService::UnregisterNotification(NOTIFY_HANDLE Handle)
BOOL CService::UnregisterNotification(HCMNOTIFICATION Handle)
{
BOOL ret = FALSE;

if (Handle)
{
#ifdef UNIVERSAL
PTP_WORK work;

work = CreateThreadpoolWork(UnregisterNotificationWork, Handle, NULL);
Expand All @@ -385,9 +355,6 @@ BOOL CService::UnregisterNotification(NOTIFY_HANDLE Handle)
}

ret = TRUE;
#else // UNIVERSAL
ret = ::UnregisterDeviceNotification(Handle);
#endif // UNIVERSAL
}

return ret;
Expand Down
16 changes: 4 additions & 12 deletions Balloon/app/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
#include <windows.h>
#include <dbt.h>

#ifdef UNIVERSAL
#define NOTIFY_HANDLE HCMNOTIFICATION
#else
#define NOTIFY_HANDLE HDEVNOTIFY
#endif

class CDevice;

class CService
Expand All @@ -23,15 +17,13 @@ class CService
static void __stdcall ServiceMainThunk(CService* service, DWORD argc, TCHAR* argv[]);
SERVICE_STATUS_HANDLE m_StatusHandle;

NOTIFY_HANDLE RegisterDeviceInterfaceNotification();
NOTIFY_HANDLE RegisterDeviceHandleNotification(HANDLE DeviceHandle);
BOOL UnregisterNotification(NOTIFY_HANDLE Handle);
HCMNOTIFICATION RegisterDeviceInterfaceNotification();
HCMNOTIFICATION RegisterDeviceHandleNotification(HANDLE DeviceHandle);
BOOL UnregisterNotification(HCMNOTIFICATION Handle);

#ifdef UNIVERSAL
static DWORD WINAPI DeviceNotificationCallback(HCMNOTIFICATION Notify,
PVOID Context, CM_NOTIFY_ACTION Action,
PCM_NOTIFY_EVENT_DATA EventData, DWORD EventDataSize);
#endif

private:
BOOL SendStatusToSCM(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint);
Expand All @@ -42,7 +34,7 @@ class CService
DWORD ServiceHandleDeviceChange(DWORD evtype);
DWORD ServiceHandlePowerEvent(DWORD evtype, DWORD flags);

NOTIFY_HANDLE m_hDevNotify;
HCMNOTIFICATION m_hDevNotify;
HANDLE m_evTerminate;
BOOL m_bRunningService;
DWORD m_Status;
Expand Down
4 changes: 0 additions & 4 deletions Balloon/app/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <strsafe.h>

#ifdef UNIVERSAL
#include <cfgmgr32.h>
#endif // UNIVERSAL

#include "targetver.h"
#include "utils.h"
#include "service.h"
Expand Down

0 comments on commit 74e209f

Please sign in to comment.