eDosStation/testdevcon/testdevcon.cpp
Luc Vandenbroucke 110dc53de7 log
2025-01-09 12:47:42 +01:00

218 lines
9.9 KiB
C++

// testdevcon.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <Windows.h>
#include <devguid.h> // for GUID_DEVCLASS_CDROM etc
#include <setupapi.h>
#include <cfgmgr32.h> // for MAX_DEVICE_ID_LEN, CM_Get_Parent and CM_Get_Device_ID
#include <Devpkey.h>
#define INITGUID
#pragma comment (lib, "Setupapi.lib")
#include <iostream>
#include <tchar.h>
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#define CLS "USB\\VID_0EBB&PID_0340&MI_00"
#define CLS2 0
#define devstr "USB\\VID_0EBB&PID_0340&MI_00\\7&8520AE7&5&0000"
#define portGuid "4d36e978-e325-11ce-bfc1-08002be10318"
#define USBGuid "88BAE032-5A81-49f0-BC3D-A4FF138216D"
HDEVINFO hi;
// include DEVPKEY_Device_BusReportedDeviceDesc from WinDDK\7600.16385.1\inc\api\devpropdef.h
#ifdef DEFINE_DEVPROPKEY
#undef DEFINE_DEVPROPKEY
#endif
#ifdef INITGUID
#define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY DECLSPEC_SELECTANY name = { { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }, pid }
#else
#define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY name
#endif // INITGUID
// include DEVPKEY_Device_BusReportedDeviceDesc from WinDDK\7600.16385.1\inc\api\devpkey.h
DEFINE_DEVPROPKEY(DEVPKEY_Device_BusReportedDeviceDesc, 0x540b947e, 0x8b40, 0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2, 4); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_ContainerId, 0x8c7ed206, 0x3f8a, 0x4827, 0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c, 2); // DEVPROP_TYPE_GUID
DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_DeviceDisplay_Category, 0x78c34fc8, 0x104a, 0x4aca, 0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57, 0x5a); // DEVPROP_TYPE_STRING_LIST
DEFINE_DEVPROPKEY(DEVPKEY_Device_LocationInfo, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 15); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_Manufacturer, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 13); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_SecuritySDS, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 26); // DEVPROP_TYPE_SECURITY_DESCRIPTOR_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_UINumberDescFormat, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 31); // DEVPROP_TYPE_STRING
typedef BOOL(WINAPI* fn_SetupDiGetDevicePropertyW)(
__in HDEVINFO DeviceInfoSet,
__in PSP_DEVINFO_DATA DeviceInfoData,
__in const DEVPROPKEY* PropertyKey,
__out DEVPROPTYPE* PropertyType,
__out_opt PBYTE PropertyBuffer,
__in DWORD PropertyBufferSize,
__out_opt PDWORD RequiredSize,
__in DWORD Flags
);
CONFIGRET eject(DEVINST dnDevInst)
{
CONFIGRET r;
DEVINST d;
TCHAR szVetoName[512];
//r= CM_Get_Child(&d, dnDevInst,0);
// CR_NO_SUCH_DEVNODE (13)
//r= CM_Get_Sibling(&d, dnDevInst, 0);
PNP_VETO_TYPE vt;
CONFIGRET status =
CM_Request_Device_EjectW(
dnDevInst,
&vt, szVetoName, 512, 0);
return 0;
}
static bool Reset(HDEVINFO hDevInfo, SP_DEVINFO_DATA *pDeviceInfoData)
{
DWORD err = 0;
bool rv = true;
//https://forums.codeguru.com/showthread.php?418183-Reset-USB-connection
SP_CLASSINSTALL_HEADER classInstallParams;
SP_PROPCHANGE_PARAMS propChangeParams;
classInstallParams.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
classInstallParams.InstallFunction = DIF_PROPERTYCHANGE;
propChangeParams.ClassInstallHeader = classInstallParams;
propChangeParams.StateChange = DICS_DISABLE; //DICS_PROPCHANGE
propChangeParams.Scope = DICS_FLAG_GLOBAL;
propChangeParams.HwProfile = 0;
if (SetupDiSetClassInstallParams(hDevInfo,pDeviceInfoData,(PSP_CLASSINSTALL_HEADER)&propChangeParams,sizeof(SP_PROPCHANGE_PARAMS)))
{
rv = SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, pDeviceInfoData);
if (!rv) err = GetLastError();
}
return rv;
}
int test()
{
CONFIGRET status;
DWORD Error;
DWORD dwSize, dwPropertyRegDataType;
DEVPROPTYPE ulPropertyType;
int DeviceIndex;
class __declspec(uuid(portGuid)) cRoot;
const GUID root = __uuidof(cRoot);
SP_DEVINFO_DATA DeviceInfoData;
const static LPCTSTR arPrefix[3] = { TEXT("VID_"), TEXT("PID_"), TEXT("MI_") };
TCHAR szDeviceInstanceID[MAX_DEVICE_ID_LEN];
TCHAR szDesc[1024], szHardwareIDs[4096];
WCHAR szBuffer[4096];
//GUID_DEVCLASS_PORTS
HDEVINFO DeviceInfoSet = SetupDiGetClassDevsExA(&GUID_DEVCLASS_PORTS, CLS, nullptr, DIGCF_PRESENT , NULL, NULL, NULL);
ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DeviceIndex = 0;
while (SetupDiEnumDeviceInfo(
DeviceInfoSet,
DeviceIndex,
&DeviceInfoData)) {
DeviceIndex++;
status = CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID, MAX_PATH, 0);
if (status != CR_SUCCESS)
continue;
// Display device instance ID
_tprintf(TEXT("%s\n"), szDeviceInstanceID);
if (SetupDiGetDeviceRegistryProperty(DeviceInfoSet
, &DeviceInfoData, SPDRP_DEVICEDESC,
&dwPropertyRegDataType, (BYTE*)szDesc,
sizeof(szDesc), // The size, in bytes
&dwSize)) {
_tprintf(_T(" Device Description: \"%s\"\n"), szDesc);
}
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
_tprintf(TEXT(" Bus Reported Device Description: \"%ls\"\n"), szBuffer);
//if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
// &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0))
// _tprintf(TEXT(" Bus Reported Device Description: \"%ls\"\n"), szBuffer);
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_Manufacturer,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
_tprintf(TEXT(" Device Manufacturer: \"%ls\"\n"), szBuffer);
}
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_FriendlyName,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
_tprintf(TEXT(" Device Friendly Name: \"%ls\"\n"), szBuffer);
}
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_LocationInfo,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
_tprintf(TEXT(" Device Location Info: \"%ls\"\n"), szBuffer);
}
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_SecuritySDS,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
// See Security Descriptor Definition Language on MSDN
// (http://msdn.microsoft.com/en-us/library/windows/desktop/aa379567(v=vs.85).aspx)
_tprintf(TEXT(" Device Security Descriptor String: \"%ls\"\n"), szBuffer);
}
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_ContainerId,
&ulPropertyType, (BYTE*)szDesc, sizeof(szDesc), &dwSize, 0)) {
StringFromGUID2((REFGUID)szDesc, szBuffer, ARRAY_SIZE(szBuffer));
_tprintf(TEXT(" ContainerId: \"%ls\"\n"), szBuffer);
}
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_DeviceDisplay_Category,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0))
_tprintf(TEXT(" Device Display Category: \"%ls\"\n"), szBuffer);
if (SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize))
_tprintf(TEXT(" Hardware ID : \"%ls\"\n"), szBuffer);
HKEY k = SetupDiOpenDevRegKey(DeviceInfoSet,&DeviceInfoData,DICS_FLAG_GLOBAL,0,DIREG_DEV, KEY_QUERY_VALUE| KEY_READ| KEY_ENUMERATE_SUB_KEYS);
TCHAR value[100];
DWORD l = sizeof(value);
LSTATUS s = 0;
long lo = sizeof(value);
l = sizeof(value);
s= RegGetValueW(k,0, L"PortName", RRF_RT_REG_SZ, nullptr,(PVOID)value, &l);
if (s == 0) {
_tprintf(TEXT(" PORT : %s\n"), value);
RegCloseKey(k);
}
Reset(DeviceInfoSet ,&DeviceInfoData);
//eject(DeviceInfoData.DevInst);
}
}
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return 0;
}
int main()
{
std::cout << "Hello World!\n";
test();
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file