1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include <stdio.h>
#include <stdlib.h>
#include <objbase.h> //DWORRD, ...
#include <setupapi.h> // you may have to manually include this library.
#include <initguid.h> //USBIO_GUID
//#include <conio.h> // ? unnecessary for the moment
//displayPeriph libs
#include <windows.h>
//#include <devguid.h>
#include <regstr.h>
#pragma comment (lib,"setupapi.lib")
#define INITGUID
int main()
{
//GUID hidGUID = {0xa5dcbf10, 0x6530, 0x11d2, {0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}};
//GUID hidGUID = {0x53f56307, 0xb6bf, 0x11d0, {0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b}};
///*ROOT_HSB*/GUID hidGUID = {0xf18a0e88, 0xc30c, 0x11d0, {0x88, 0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8}};
//GUID hidGUID = {0x53f56307, 0xb6bf, 0x11d0, {0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b}};
///*DOUCHETTE*/GUID hidGUID = {0x745a17a0L, 0x74d3, 0x11d0, {0xb6, 0xfe, 0x00, 0xa0, 0xc9, 0x0f, 0x57, 0xda}};
/*DOUCHETTE*/GUID hidGUID = {0xA5DCBF10L, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}};
/*
HDEVINFO hardwareDeviceInfoSet;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
ULONG requiredSize;
HANDLE deviceHandle = INVALID_HANDLE_VALUE;
DWORD result;
hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
NULL,
NULL,
(DIGCF_PRESENT |
DIGCF_INTERFACEDEVICE));
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
NULL,
&hidGUID,
0,
&deviceInterfaceData);
if (result == FALSE)
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
}
SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
NULL, //interfaceDetail,
0, //interfaceDetailSize,
&requiredSize,
0); //infoData))
deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize);
deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
deviceDetail,
requiredSize,
&requiredSize,
NULL))
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
}
deviceHandle = CreateFile (deviceDetail->DevicePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0,
NULL); // No template file
DWORD res = GetLastError();
if(deviceHandle==INVALID_HANDLE_VALUE)
printf("INVALID HANDLE\n");
else
printf("LE CREATEFILE FONCTIONNE\n");
printf("devicename : %s\n",deviceDetail->DevicePath);*/
/*
char* buffer;
DWORD nb_caractere;
printf("lecture\n");
COMMTIMEOUTS Timeout;
Timeout.ReadTotalTimeoutConstant=10000;
Timeout.WriteTotalTimeoutConstant=0;
Timeout.WriteTotalTimeoutMultiplier=0;
SetCommTimeouts(deviceHandle,&Timeout);
int testread;
testread=ReadFile(deviceHandle,buffer,2048,&nb_caractere,NULL);
if(testread==0)
printf("erreur read %d\n",testread);
else
printf("contenu : %s\n",buffer);
if(GetLastError()==ERROR_HANDLE_EOF)
printf("ERROR_HANDLE_EOF");
if(GetLastError()==ERROR_IO_PENDING)
printf("ERROR_IO_PENDING");
*/
/*DWORD z;
DWORD nb;
if((nb=WriteFile(deviceHandle,"coucou",6,&z,NULL))==0)
printf("ERREUR WRITEFILE");
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
*/
HDEVINFO hDevInfo;
SP_INTERFACE_DEVICE_DATA InterfaceDeviceInfoData;
hDevInfo = SetupDiGetClassDevs(&hidGUID, 0, 0, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("Erreur SetUpDiGetClassDevs\n");
return 0;
}
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
int nb = SetupDiEnumDeviceInfo(hDevInfo,0,&DeviceInfoData);
for (int i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
{
InterfaceDeviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
DWORD needed;
if (!SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&hidGUID,i,&InterfaceDeviceInfoData))
{
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
//une première fois afin de connaître la taille de la zone mémoire à réserver (needed)
SetupDiGetDeviceInterfaceDetail(hDevInfo,&InterfaceDeviceInfoData,NULL,0,&needed,NULL);
//on réserve la taille mémoire
PSP_DEVICE_INTERFACE_DETAIL_DATA detail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) new char[needed];
if (!detail)
{
printf("Problème mémoire PSP_INTERFACE_DEVICE_DETAIL_DATA");
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
//on cherche le "nom" du device
detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,&InterfaceDeviceInfoData,detail,needed,NULL,NULL))
{
delete detail;
printf("Aucune information de détail");
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
printf("%s\n",detail->DevicePath);
HANDLE hCom;
hCom = CreateFile(detail->DevicePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
DWORD test = GetLastError();
if (hCom == INVALID_HANDLE_VALUE)
{
LPSTR Message;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,test,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPSTR)&Message,0,NULL);
printf("%s\n",Message);
}
else
{
//printf("HANDLE Correct %d\n",hCom);
}
/*
char* CB=(char*)malloc(50);
DWORD nb;
if(!ReadFile(hCom,CB,100,&nb,NULL))
printf("erreur readfile\n");
test = GetLastError();
CB[nb]='\0';
if (hCom != INVALID_HANDLE_VALUE)
{
CloseHandle(hCom);
printf("la c sur ca bug\n");
}
if (detail)
delete detail;
}
*/}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
} |
Partager