Using SetupDiGetDeviceRegistryProperty() from SetupAPI to get SPDRP_FRIENDLYNAME of mouse

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Using SetupDiGetDeviceRegistryProperty() from SetupAPI to get SPDRP_FRIENDLYNAME of mouse

15 Jun 2015, 16:13

Hey all,
I'm trying to use SetupDiGetDeviceRegistryProperty to extract the friendly device name of mouse devices but I can't figure it out...

https://msdn.microsoft.com/en-us/librar ... 85%29.aspx

Using a slight modification of the code from here:
http://www.autohotkey.com/board/topic/9 ... inning-up/

I'm able to get a msgbox for a bunch of devices, but not the mice...

Code: Select all


;Adapted from:
;http://stackoverflow.com/questions/4097000/how-do-i-disable-a-system-device-programatically

hModule := DllCall("LoadLibrary", "str", "setupapi.dll")
OnExit, FreeLib
GUID_DEVCLASS_MOUSE = {4d36e96f-e325-11ce-bfc1-08002be10318}
info := DllCall("setupapi.dll\SetupDiGetClassDevsW", "ptr", 0, "ptr", 0, "ptr", 0, "uint", DIGCF_ALLCLASSES := 4)

if (info = -1)
{
  Msgbox % "SetupDiGetClassDevs failed: " A_LastError
  Exitapp
}

DevInfo_Data(di)
Loop
{
  if !DllCall("setupapi.dll\SetupDiEnumDeviceInfo", "ptr", info, "uint", A_Index-1, "ptr", &di)
  {
    If (A_LastError = 259) ;ERROR_NO_MORE_ITEMS
    {
      Msgbox % "Could not find the device."
      Exitapp
    }
    else
    {
      Msgbox % "SetupDiEnumDeviceInfo failed: " A_LastError
      Exitapp
    }
  }
___ := GetDeviceString(info, di, 12)
 If (___ <> "")
  MsgBox, %___%
  if (___ = DeviceName)
    break
}
return

F1::
  Traytip, Dev State Change, Enabling %DeviceName%
  MakeChange(info, di, 1)
return

F2::
  Traytip, Dev State Change, Disabling %DeviceName%
  MakeChange(info, di, 2)
return

Esc::ExitApp

FreeLib:
  DllCall("setupapi.dll\SetupDiDestroyDeviceInfoList", "ptr", info)
  DllCall("FreeLibrary", "Ptr", hModule)
  ExitApp
return 

PropChange_Params(byref pcp, InstallFunction, state)
{
  /*
  SP_CLASSINSTALL_HEADER
  sizeof: 8
  0 - cbSize
  4 - InstallFunction

  SP_PROPCHANGE_PARAMS
  sizeof: 20
  0 - Install header
  8 - StateChange
  12 - Scope
  16 - HwProfile
  */
  VarSetCapacity(pcp, 20)
  Numput(8, pcp, 0, "uint"), Numput(InstallFunction, pcp, 4, "uint")
  Numput(1, pcp, 12, "uint"), Numput(0, pcp, 16, "uint")

  ;DICS_ENABLE = 1
  ;DICS_DISABLE = 2
  Numput(state, pcp, 8, "uint")
}

GetDeviceString(info, byref di, propID = 1)
{
  ;http://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx
  VarSetCapacity(buf, 512), VarSetCapacity(strsz, 4)
  r := DllCall("setupapi.dll\SetupDiGetDeviceRegistryPropertyW", "ptr", info, "ptr", &di
    , "uint", propID, "ptr", 0, "ptr", &buf, "uint", 512, "ptr", &strsz)
  if !r 
  {
    if (A_LastError = 13) ;ERROR_INVALID_DATA
      return ""
    Msgbox % "SetupDiGetDeviceRegistryProperty failed: " A_LastError
    ExitApp
  }
  return StrGet(&buf, NumGet(strsz, "uint"))  
}

MakeChange(info, byref di, newstate)
{
  PropChange_Params(pcp, 0x12, newstate)
  if !DllCall("setupapi.dll\SetupDiSetClassInstallParams", "ptr", info, "ptr", &di, "ptr", &pcp, "uint", 20)
  {
    Msgbox % "SetupDiSetClassInstallParams failed: " A_LastError
    Exitapp
  }

  if !DllCall("setupapi.dll\SetupDiChangeState", "ptr", info, "ptr", &di)
  {
    Msgbox % "SetupDiChangeState failed: " A_LastError
    Exitapp
  }
}

DevInfo_Data(byref di)
{
  /*
  SP_DEVINFO_DATA struct
  sizeof: 28
   0 - cbSize
   4 - ClassGuid
   20 - DevInst
   24 - reserved
  */
  VarSetCapacity(di, 24+A_PtrSize,0), Numput(24+A_PtrSize, di, 0, "uint")
}
so i think I need to use the mouse device GUID somehow like

Code: Select all

str := "{4d36e96f-e325-11ce-bfc1-08002be10318}" ; GUID_DEVCLASS_MOUSE
StringUpper, str, str
COM_Init()
hDIC := COM_GUID4String(DIC, str)
COM_Term()
from:
http://www.autohotkey.com/board/topic/4 ... -for-guid/

but I can't seem to make sense of it, any help greatly appreciated!
thanks
- joel
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Using SetupDiGetDeviceRegistryProperty() from SetupAPI to get SPDRP_FRIENDLYNAME of mouse

15 Jun 2015, 17:21

So maybe this information isn't saved the way it used to be...

The example here:
http://www.autohotkey.com/board/topic/7 ... b-devices/

Edit: so the way i figure is XP used SPDRP_LOCATION_INFORMATION for this string, and was later fixed to use SPDRP_DEVICEDESC and SPDRP_MFG , and LocationInformation was correctly set to store hub#/port# info...

XP:
SPDRP_FRIENDLYNAME: Not Found, Ret 0 ErrorLevel 0 13 The data is invalid.
SPDRP_LOCATION_INFORMATION: Microsoft Basic Optical Mouse v2.0

Win7:
SPDRP_FRIENDLYNAME: HID-compliant Mouse
SPDRP_DEVICEDESC: Basic Optical Mouse v2.0
SPDRP_MFG: Microsoft
SPDRP_LOCATION_INFORMATION: Port _ # 0002.Hub _ # 0006





Where I was hoping to get info such as:
Primax Electronics, Ltd - HP Optical Mouse
Primax Electronics, Ltd - Dell Optical Mouse
Logitech - Unifying Receiver

(based on the Vid Pid, back in XP this was stored under device Enum LocationInformation in the registry...

If anyone has any ideas/solution I'd be happy to hear it, but I guess I'll try to gather this info from the net at run time:
http://www.linux-usb.org/usb.ids
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750 and 400 guests