Skip to content

Commit

Permalink
Can now specify devices using their device instance ID
Browse files Browse the repository at this point in the history
  • Loading branch information
briankendall committed Dec 28, 2024
1 parent 652f567 commit bc7f96d
Show file tree
Hide file tree
Showing 18 changed files with 575 additions and 62 deletions.
173 changes: 173 additions & 0 deletions DeviceLister/DIDeviceInputId/DIDeviceInputId.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<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>{98d64f89-718f-4828-a906-c9499b4a8679}</ProjectGuid>
<RootNamespace>DIDeviceInputId</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</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" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)\DeviceLister\bin\Debug\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)\DeviceLister\bin\Release\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;DIDEVICEINPUTID_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>dinput8.lib;dxguid.lib;Cfgmgr32.lib;rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;DIDEVICEINPUTID_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>dinput8.lib;dxguid.lib;Cfgmgr32.lib;rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;DIDEVICEINPUTID_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;DIDEVICEINPUTID_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="framework.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
33 changes: 33 additions & 0 deletions DeviceLister/DIDeviceInputId/DIDeviceInputId.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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>
<ClInclude Include="framework.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
103 changes: 103 additions & 0 deletions DeviceLister/DIDeviceInputId/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

#include "pch.h"
#include <initguid.h>
#include <string>
#include <dinput.h>
#include <Devpkey.h>
#include <Cfgmgr32.h>

std::wstring getDeviceInstanceIdProperty(const DIPROPGUIDANDPATH &iap)
{
DEVPROPTYPE propertyType;
ULONG propertySize = 0;
CONFIGRET cr = CM_Get_Device_Interface_PropertyW(iap.wszPath, &DEVPKEY_Device_InstanceId, &propertyType, nullptr, &propertySize, 0);

if (cr != CR_BUFFER_SMALL) {
OutputDebugStringA("CM_Get_Device_Interface_PropertyW 1 failed");
return std::wstring();
}

std::wstring deviceId;
deviceId.resize(propertySize);
cr = ::CM_Get_Device_Interface_PropertyW(iap.wszPath, &DEVPKEY_Device_InstanceId, &propertyType, (PBYTE)deviceId.data(), &propertySize, 0);

if (cr != CR_SUCCESS) {
OutputDebugStringA("CM_Get_Device_Interface_PropertyW 2 failed");
return std::wstring();
}

return deviceId;
}

extern "C" {

// Function that takes a string and returns a string
__declspec(dllexport) const wchar_t * GetDeviceInstanceID(const wchar_t *instanceGUIDStr)
{
wchar_t *output = nullptr;
HRESULT hr;
LPDIRECTINPUT8 di;

hr = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&di, nullptr);

if (FAILED(hr)) {
OutputDebugStringA("DirectInput8Create failed");
return nullptr;
}

GUID instanceGUID;

if (UuidFromStringW((RPC_WSTR)instanceGUIDStr, &instanceGUID) != RPC_S_OK) {
OutputDebugStringA("UuidFromStringA failed");
OutputDebugStringW(instanceGUIDStr);
return nullptr;
}

IDirectInputDevice8W* device;
IDirectInput_CreateDevice(di, instanceGUID, &device, NULL);

DIPROPGUIDANDPATH iap = {};
iap.diph.dwSize = sizeof(DIPROPGUIDANDPATH);
iap.diph.dwHeaderSize = sizeof(DIPROPHEADER);
iap.diph.dwHow = DIPH_DEVICE;

std::wstring deviceId;

if (SUCCEEDED(IDirectInputDevice_GetProperty(device, DIPROP_GUIDANDPATH, &iap.diph))) {
deviceId = getDeviceInstanceIdProperty(iap);
} else {
OutputDebugStringA("IDirectInputDevice_GetProperty failed");
}

IDirectInputDevice_Release(device);

if (deviceId.size() == 0) {
return nullptr;
}

size_t outputSize = deviceId.size() + 1;
output = new wchar_t[outputSize];
wcscpy_s(output, outputSize, deviceId.c_str());

return output;
}

}


BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

5 changes: 5 additions & 0 deletions DeviceLister/DIDeviceInputId/framework.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files
#include <windows.h>
5 changes: 5 additions & 0 deletions DeviceLister/DIDeviceInputId/pch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// pch.cpp: source file corresponding to the pre-compiled header

#include "pch.h"

// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
15 changes: 15 additions & 0 deletions DeviceLister/DIDeviceInputId/pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for future builds.
// This also affects IntelliSense performance, including code completion and many code browsing features.
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
// Do not add files here that you will be updating frequently as this negates the performance advantage.

#ifndef PCH_H
#define PCH_H

#define DIRECTINPUT_VERSION 0x0800

// add headers that you want to pre-compile here
#include "framework.h"

#endif //PCH_H
36 changes: 34 additions & 2 deletions DeviceLister/DeviceLister.sln
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.33130.400
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeviceLister", "DeviceLister\DeviceLister.csproj", "{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}"
ProjectSection(ProjectDependencies) = postProject
{98D64F89-718F-4828-A906-C9499B4A8679} = {98D64F89-718F-4828-A906-C9499B4A8679}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DIDeviceInputId", "DIDeviceInputId\DIDeviceInputId.vcxproj", "{98D64F89-718F-4828-A906-C9499B4A8679}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Debug|x64.ActiveCfg = Debug|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Debug|x64.Build.0 = Debug|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Debug|x86.ActiveCfg = Debug|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Debug|x86.Build.0 = Debug|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Release|Any CPU.Build.0 = Release|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Release|x64.ActiveCfg = Release|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Release|x64.Build.0 = Release|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Release|x86.ActiveCfg = Release|Any CPU
{EBE0AF6C-E6DA-4497-843A-A3B2B16D2627}.Release|x86.Build.0 = Release|Any CPU
{98D64F89-718F-4828-A906-C9499B4A8679}.Debug|Any CPU.ActiveCfg = Debug|Win32
{98D64F89-718F-4828-A906-C9499B4A8679}.Debug|Any CPU.Build.0 = Debug|Win32
{98D64F89-718F-4828-A906-C9499B4A8679}.Debug|x64.ActiveCfg = Debug|x64
{98D64F89-718F-4828-A906-C9499B4A8679}.Debug|x64.Build.0 = Debug|x64
{98D64F89-718F-4828-A906-C9499B4A8679}.Debug|x86.ActiveCfg = Debug|Win32
{98D64F89-718F-4828-A906-C9499B4A8679}.Debug|x86.Build.0 = Debug|Win32
{98D64F89-718F-4828-A906-C9499B4A8679}.Release|Any CPU.ActiveCfg = Release|Win32
{98D64F89-718F-4828-A906-C9499B4A8679}.Release|Any CPU.Build.0 = Release|Win32
{98D64F89-718F-4828-A906-C9499B4A8679}.Release|x64.ActiveCfg = Release|x64
{98D64F89-718F-4828-A906-C9499B4A8679}.Release|x64.Build.0 = Release|x64
{98D64F89-718F-4828-A906-C9499B4A8679}.Release|x86.ActiveCfg = Release|Win32
{98D64F89-718F-4828-A906-C9499B4A8679}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D1358047-6840-475B-BFF7-0DF3D8E46C26}
EndGlobalSection
EndGlobal
Loading

0 comments on commit bc7f96d

Please sign in to comment.