[idd] helper: added new helper service
Some checks are pending
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Waiting to run
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Waiting to run
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Waiting to run
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Waiting to run
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Waiting to run
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Waiting to run
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Waiting to run
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Waiting to run
build / module (push) Waiting to run
build / host-linux (push) Waiting to run
build / host-windows-cross (push) Waiting to run
build / host-windows-native (push) Waiting to run
build / obs (clang) (push) Waiting to run
build / obs (gcc) (push) Waiting to run
build / docs (push) Waiting to run

As the IDD itself runs in a WUMDF sandbox, it doesn't have enough
access to perform interactive operations such as moving the cursor.

This helper service communicates with the IDD over a named pipe,
so that we can perform these things, as well as in the future provide
a configuration GUI.
This commit is contained in:
Geoffrey McRae
2025-03-28 12:05:02 +00:00
parent bf59e45118
commit 6a4edfc6b6
18 changed files with 1267 additions and 24 deletions

View File

@@ -0,0 +1,231 @@
/**
* Looking Glass
* Copyright <20> 2017-2025 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "CPipeClient.h"
#include "CDebug.h"
#include <setupapi.h>
#include <tchar.h>
CPipeClient g_pipe;
bool CPipeClient::Init()
{
DeInit();
if (!IsLGIddDeviceAttached())
{
DEBUG_ERROR("Looking Glass Indirect Display Device not found");
return false;
}
m_running = true;
m_thread.Attach(CreateThread(
NULL,
0,
_pipeThread,
(LPVOID)this,
0,
NULL));
if (!m_thread.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "Failed to create the pipe thread");
return false;
}
return true;
}
void CPipeClient::DeInit()
{
m_connected = false;
if (m_thread.IsValid())
{
m_running = false;
WaitForSingleObject(m_thread.Get(), INFINITE);
m_thread.Close();
}
if (m_pipe.IsValid())
{
FlushFileBuffers(m_pipe.Get());
m_pipe.Close();
}
}
bool CPipeClient::IsLGIddDeviceAttached()
{
HDEVINFO hDevInfo = SetupDiGetClassDevs(
NULL,
NULL,
NULL,
DIGCF_ALLCLASSES | DIGCF_PRESENT
);
if (hDevInfo == INVALID_HANDLE_VALUE)
return false;
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
bool found = false;
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
DWORD DataT;
TCHAR buffer[1024];
DWORD buffersize = 0;
if (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)buffer,
sizeof(buffer),
&buffersize))
continue;
for (LPCTSTR p = buffer; *p; p += _tcslen(p) + 1)
if (_tcsicmp(p, _T("Root\\LGIdd")) == 0)
{
found = true;
break;
}
if (found)
break;
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return found;
}
/* APIs like SetCursorPos are applied to the desktop our thread is
* attached to. If the user switches to the secure desktop (UAC, etc)
* then these functions will not work, so call this first to ensure
* the call is effective */
void CPipeClient::SetActiveDesktop()
{
HDESK desktop = NULL;
desktop = OpenInputDesktop(0, FALSE, GENERIC_READ);
if (!desktop)
DEBUG_ERROR_HR(GetLastError(), "OpenInputDesktop Failed");
else
{
if (!SetThreadDesktop(desktop))
DEBUG_ERROR_HR(GetLastError(), "SetThreadDesktop Failed");
CloseDesktop(desktop);
}
}
void CPipeClient::WriteMsg(const LGPipeMsg& msg)
{
DWORD written;
if (!WriteFile(m_pipe.Get(), &msg, sizeof(msg), &written, NULL))
{
DWORD err = GetLastError();
if (err == ERROR_BROKEN_PIPE)
{
DEBUG_WARN_HR(err, "Client disconnected, failed to write");
m_connected = false;
return;
}
DEBUG_WARN_HR(err, "WriteFile failed on the pipe");
return;
}
FlushFileBuffers(m_pipe.Get());
}
void CPipeClient::Thread()
{
DEBUG_INFO("Pipe thread started");
while (m_running)
{
if (!WaitNamedPipeA(LG_PIPE_NAME, 5000))
{
if (!IsLGIddDeviceAttached())
{
m_running = false;
DEBUG_ERROR("Device is no longer available, shutting down");
break;
}
continue;
}
m_pipe.Attach(CreateFileA(
LG_PIPE_NAME,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
));
if (!m_pipe.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "Failed to open the named pipe");
continue;
}
m_connected = true;
DEBUG_INFO("Pipe connected");
while (m_running && m_connected)
{
LGPipeMsg msg;
DWORD bytesRead;
if (!ReadFile(m_pipe.Get(), &msg, sizeof(msg), &bytesRead, NULL))
{
DEBUG_ERROR_HR(GetLastError(), "ReadFile Failed");
break;
}
if (bytesRead != sizeof(msg) || msg.size != sizeof(msg))
{
DEBUG_ERROR("Corrupted data");
break;
}
switch (msg.type)
{
case LGPipeMsg::SETCURSORPOS:
HandleSetCursorPos(msg);
break;
default:
DEBUG_ERROR("Unknown message type %d", msg.type);
break;
}
}
m_pipe.Close();
m_connected = false;
DEBUG_INFO("Pipe closed");
}
DEBUG_INFO("Pipe thread shutdown");
}
void CPipeClient::HandleSetCursorPos(const LGPipeMsg& msg)
{
SetActiveDesktop();
SetCursorPos(msg.curorPos.x, msg.curorPos.y);
}

View File

@@ -0,0 +1,60 @@
/**
* Looking Glass
* Copyright <20> 2017-2025 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include <windows.h>
#include <stdint.h>
#include <wrl.h>
#include "PipeMsg.h"
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace Microsoft::WRL::Wrappers::HandleTraits;
class CPipeClient
{
private:
HandleT<HANDLENullTraits> m_pipe;
HandleT<HANDLENullTraits> m_thread;
bool m_running = false;
bool m_connected = false;
static DWORD WINAPI _pipeThread(LPVOID lpParam) { ((CPipeClient*)lpParam)->Thread(); return 0; }
void Thread();
void WriteMsg(const LGPipeMsg& msg);
void SetActiveDesktop();
void HandleSetCursorPos(const LGPipeMsg& msg);
public:
~CPipeClient() { DeInit(); }
static bool IsLGIddDeviceAttached();
bool Init();
void DeInit();
bool IsRunning() { return m_running; }
};
extern CPipeClient g_pipe;

View File

@@ -0,0 +1,188 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSBuilder.Git.0.3.0\build\MSBuilder.Git.props" Condition="Exists('..\packages\MSBuilder.Git.0.3.0\build\MSBuilder.Git.props')" />
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{0045d7ad-3f26-4b87-81cb-78d18839596d}</ProjectGuid>
<RootNamespace>LGIddHelper</RootNamespace>
<WindowsTargetPlatformVersion>10.0.22621.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)LGCommon;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)LGCommon;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>Default</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
<AdditionalIncludeDirectories>$(SolutionDir)LGCommon;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>%(AdditionalDependencies);Rpcrt4.lib;Userenv.lib;setupapi.lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>if not exist "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd" mkdir "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd"
copy /Y "$(TargetPath)" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd\"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>Default</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
<AdditionalIncludeDirectories>$(SolutionDir)LGCommon;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>%(AdditionalDependencies);Rpcrt4.lib;Userenv.lib;setupapi.lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>if not exist "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd" mkdir "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd"
copy /Y "$(TargetPath)" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd\"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="$(SolutionDir)LGCommon\*.cpp" />
<ClCompile Include="CPipeClient.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<CLInclude Include="$(SolutionDir)LGCommon\*.h" />
<ClInclude Include="CPipeClient.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
<Target Name="GenerateVersionInfo" BeforeTargets="ClCompile">
<Exec Command="&quot;$(Git)&quot; describe --always --abbrev=10 --dirty=+ --tags" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitVersion" />
</Exec>
<ItemGroup>
<VersionInfoLines Include="#define LG_VERSION_STR &quot;$(GitVersion)&quot;" />
<VersionInfoLines Include="#define LG_CURRENT_YEAR $(CurrentYear)" />
</ItemGroup>
<WriteLinesToFile File="VersionInfo.h" Lines="@(VersionInfoLines)" Overwrite="true" />
</Target>
<Target Name="Build" DependsOnTargets="$(BuildDependsOn)" />
<PropertyGroup>
<CurrentYear>$([System.DateTime]::Now.ToString("yyyy"))</CurrentYear>
<BuildDependsOn>
GenerateVersionInfo;
$(BuildDependsOn);
</BuildDependsOn>
</PropertyGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSBuilder.Git.0.3.0\build\MSBuilder.Git.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuilder.Git.0.3.0\build\MSBuilder.Git.props'))" />
</Target>
</Project>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="$(SolutionDir)LGCommon\*.cpp" />
</ItemGroup>
<ItemGroup>
<CLInclude Include="$(SolutionDir)LGCommon\*.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,2 @@
#define LG_VERSION_STR "B7-25-gbf59e45118+"
#define LG_CURRENT_YEAR 2025

421
idd/LGIddHelper/main.cpp Normal file
View File

@@ -0,0 +1,421 @@
#include <Windows.h>
#include <wrl.h>
#include <UserEnv.h>
#include <vector>
#include <string>
using namespace Microsoft::WRL::Wrappers;
using namespace Microsoft::WRL::Wrappers::HandleTraits;
#include "CDebug.h"
#include "VersionInfo.h"
#include "CPipeClient.h"
#define SVCNAME "Looking Glass (IDD Helper)"
static SERVICE_STATUS_HANDLE l_svcStatusHandle;
static SERVICE_STATUS l_svcStatus;
static HandleT<EventTraits> l_svcStopEvent;
bool HandleService();
static void WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv);
static void WINAPI SvcCtrlHandler(DWORD dwControl);
static void ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint);
static std::string l_executable;
static HandleT<HANDLENullTraits> l_process;
static HandleT<EventTraits> l_exitEvent;
static std::string l_exitEventName;
static void Launch();
LRESULT CALLBACK DummyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_debug.Init("looking-glass-iddhelper");
DEBUG_INFO("Looking Glass IDD Helper (" LG_VERSION_STR ")");
char buffer[MAX_PATH];
DWORD result = GetModuleFileNameA(NULL, buffer, MAX_PATH);
if (result == 0)
{
DEBUG_ERROR("Failed to get the executable path");
return EXIT_FAILURE;
}
l_executable = buffer;
int argc = 0;
LPWSTR * wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
std::vector<std::string> args;
args.reserve(argc);
for (int i = 0; i < argc; ++i)
{
size_t len = wcslen(wargv[i]);
size_t bufSize = (len + 1) * 2;
std::vector<char> buffer(bufSize);
size_t converted = 0;
errno_t err = wcstombs_s(&converted, buffer.data(), bufSize, wargv[i], bufSize - 1);
if (err != 0)
{
DEBUG_ERROR("Conversion failed");
return EXIT_FAILURE;
}
args.emplace_back(buffer.data());
}
LocalFree(wargv);
if (argc == 1)
{
if (!HandleService())
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
// child process
if (argc != 2)
{
// the one and only value we should see is the exit event name
DEBUG_ERROR("Invalid invocation");
return EXIT_FAILURE;
}
l_exitEvent.Attach(OpenEvent(SYNCHRONIZE, FALSE, args[1].c_str()));
if (!l_exitEvent.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "Failed to open the exit event: %s", args[1].c_str());
return EXIT_FAILURE;
}
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = DummyWndProc;
wx.hInstance = hInstance;
wx.lpszClassName = "DUMMY_CLASS";
wx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wx.hCursor = LoadCursor(NULL, IDC_ARROW);
wx.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
ATOM aclass;
if (!(aclass = RegisterClassEx(&wx)))
{
DEBUG_ERROR("Failed to register message window class");
return EXIT_FAILURE;
}
HWND msgWnd = CreateWindowExA(0, MAKEINTATOM(aclass), NULL,
0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
bool running = g_pipe.Init();
while (running)
{
switch (MsgWaitForMultipleObjects(1, l_exitEvent.GetAddressOf(),
FALSE, INFINITE, QS_ALLINPUT))
{
case WAIT_OBJECT_0:
running = false;
break;
case WAIT_OBJECT_0 + 1:
{
MSG msg;
if (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
break;
}
case WAIT_FAILED:
DEBUG_ERROR_HR(GetLastError(), "MsgWaitForMultipleObjects Failed");
running = false;
break;
}
}
g_pipe.DeInit();
DestroyWindow(msgWnd);
return EXIT_SUCCESS;
}
bool HandleService()
{
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ (char *)SVCNAME, SvcMain },
{ NULL, NULL }
};
if (StartServiceCtrlDispatcher(DispatchTable) == FALSE)
{
DEBUG_ERROR_HR(GetLastError(), "StartServiceCtrlDispatcher Failed");
return false;
}
return true;
}
static void WINAPI SvcCtrlHandler(DWORD dwControl)
{
switch (dwControl)
{
case SERVICE_CONTROL_STOP:
ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
SetEvent(l_svcStopEvent.Get());
return;
default:
break;
}
ReportSvcStatus(l_svcStatus.dwCurrentState, NO_ERROR, 0);
}
static void WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
l_svcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
l_svcStatus.dwWin32ExitCode = 0;
l_svcStatusHandle = RegisterServiceCtrlHandler(SVCNAME, SvcCtrlHandler);
if (!l_svcStatusHandle)
{
DEBUG_ERROR_HR(GetLastError(), "RegisterServiceCtrlHandler Failed");
return;
}
if (!CPipeClient::IsLGIddDeviceAttached())
{
DEBUG_INFO("Looking Glass Indirect Display Device not found, not starting.");
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
return;
}
ReportSvcStatus(SERVICE_START_PENDING, NO_ERROR, 0);
l_svcStopEvent.Attach(CreateEvent(NULL, TRUE, FALSE, NULL));
if (!l_svcStopEvent.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "CreateEvent Failed");
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
return;
}
UUID uuid;
RPC_CSTR uuidStr;
RPC_STATUS status = UuidCreate(&uuid);
if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY && status != RPC_S_UUID_NO_ADDRESS)
{
DEBUG_ERROR("UuidCreate Failed: 0x%x", status);
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
return;
}
if (UuidToString(&uuid, &uuidStr) == RPC_S_OK)
{
l_exitEventName = "Global\\";
l_exitEventName += (const char *)uuidStr;
RpcStringFree(&uuidStr);
l_exitEvent.Attach(CreateEvent(NULL, FALSE, FALSE, l_exitEventName.c_str()));
if (!l_exitEvent.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "CreateEvent Failed");
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
return;
}
}
ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
bool running = true;
while (running)
{
ULONGLONG launchTime = 0ULL;
DWORD interactiveSession = WTSGetActiveConsoleSessionId();
if (interactiveSession != 0 && interactiveSession != 0xFFFFFFFF)
{
if (!CPipeClient::IsLGIddDeviceAttached())
{
DEBUG_INFO("Looking Glass Indirect Display Device has gone away");
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
return;
}
Launch();
launchTime = GetTickCount64();
}
HANDLE waitOn[] = { l_svcStopEvent.Get(), l_process.Get()};
DWORD count = 2;
DWORD duration = INFINITE;
if (!l_process.IsValid())
{
count = 1;
duration = 1000;
}
switch (WaitForMultipleObjects(count, waitOn, FALSE, duration))
{
// stop requested by the service manager
case WAIT_OBJECT_0:
running = false;
break;
// child application exited
case WAIT_OBJECT_0 + 1:
{
DWORD code;
if (!GetExitCodeProcess(l_process.Get(), &code))
{
DEBUG_ERROR_HR(GetLastError(), "GetExitCodeProcess Failed");
break;
}
DEBUG_INFO("Child process exited with code 0x%lx", code);
l_process.Close();
break;
}
case WAIT_FAILED:
DEBUG_ERROR_HR(GetLastError(), "Failed to WaitForMultipleObjects");
running = false;
break;
}
if (!running)
break;
Sleep(1000);
}
SetEvent(l_exitEvent.Get());
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
}
static void ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 0;
l_svcStatus.dwCurrentState = dwCurrentState;
l_svcStatus.dwWin32ExitCode = dwWin32ExitCode;
l_svcStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
l_svcStatus.dwControlsAccepted = 0;
else
l_svcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
if ((dwCurrentState == SERVICE_RUNNING) || (dwCurrentState == SERVICE_STOPPED))
l_svcStatus.dwCheckPoint = 0;
else
l_svcStatus.dwCheckPoint = ++dwCheckPoint;
SetServiceStatus(l_svcStatusHandle, &l_svcStatus);
}
//static void
static bool EnablePriv(const char * name)
{
return true;
}
static void DisablePriv(const char * name)
{
}
static void Launch()
{
l_process.Close();
HandleT<HANDLENullTraits> sysToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE |
TOKEN_ASSIGN_PRIMARY | TOKEN_ADJUST_SESSIONID | TOKEN_ADJUST_DEFAULT,
sysToken.GetAddressOf()))
{
DEBUG_ERROR_HR(GetLastError(), "OpenProcessToken failed");
return;
}
HandleT<HANDLENullTraits> token;
if (!DuplicateTokenEx(sysToken.Get(), 0, NULL, SecurityAnonymous,
TokenPrimary, token.GetAddressOf()))
{
DEBUG_ERROR_HR(GetLastError(), "DuplicateTokenEx failed");
return;
}
DWORD origSessionID, targetSessionID, returnedLen;
GetTokenInformation(token.Get(), TokenSessionId, &origSessionID,
sizeof(origSessionID), &returnedLen);
targetSessionID = WTSGetActiveConsoleSessionId();
if (origSessionID != targetSessionID)
{
if (!SetTokenInformation(token.Get(), TokenSessionId,
&targetSessionID, sizeof(targetSessionID)))
{
DEBUG_ERROR_HR(GetLastError(), "SetTokenInformation failed");
return;
}
}
LPVOID env = NULL;
if (!CreateEnvironmentBlock(&env, token.Get(), TRUE))
{
DEBUG_ERROR_HR(GetLastError(), "CreateEnvironmentBlock failed");
return;
}
if (!EnablePriv(SE_INCREASE_QUOTA_NAME))
{
DEBUG_ERROR("Failed to enable %s", SE_INCREASE_QUOTA_NAME);
return;
}
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
si.lpDesktop = (LPSTR)"WinSta0\\Default";
char * cmdLine = NULL;
char cmdBuf[128];
if (l_exitEvent.IsValid())
{
snprintf(cmdBuf, sizeof(cmdBuf), "LGIddHelper.exe %s",
l_exitEventName.c_str());
cmdLine = cmdBuf;
}
if (!CreateProcessAsUserA(
token.Get(),
l_executable.c_str(),
cmdLine,
NULL,
NULL,
FALSE,
DETACHED_PROCESS | HIGH_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
env,
NULL,
&si,
&pi
))
{
DEBUG_ERROR_HR(GetLastError(), "CreateProcessAsUser failed");
return;
}
DisablePriv(SE_INCREASE_QUOTA_NAME);
l_process.Attach(pi.hProcess);
CloseHandle(pi.hThread);
}

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSBuilder.Git" version="0.3.0" targetFramework="native" />
</packages>