ACTIVATE screen CLEAR cguid = [{36FC9E60-C465-11CF-8056-444553540000}] ? cguid *!* DIGCF_ALLCLASSES *!* Return a list of installed devices for all device setup classes or all device interface classes. *!* DIGCF_DEVICEINTERFACE *!* Return devices that support device interfaces for the specified device interface classes. *!* DIGCF_DEFAULT *!* Return only the device that is associated with the system default device interface, if one is set, for the specified device interface classes. *!* DIGCF_PRESENT *!* Return only devices that are currently present in a system. *!* DIGCF_PROFILE *!* Return only devices that are a part of the current hardware profile. #DEFINE ERROR_NO_MORE_ITEMS 259 #DEFINE ERROR_INVALID_PARAMETER 87 #DEFINE ERROR_INVALID_FLAGS 1004 #DEFINE ERROR_INSUFFICIENT_BUFFER 122 #DEFINE DIGCF_INTERFACEDEVICE 16 && 0x00000010 #DEFINE DIGCF_PRESENT 2 && 0x00000002 #define DIGCF_ALLCLASSES 4 *!* #define DIGCF_DEFAULT 0x00000001 // only valid with DIGCF_DEVICEINTERFACE *!* #define DIGCF_PRESENT 0x00000002 *!* #define DIGCF_ALLCLASSES 0x00000004 *!* #define DIGCF_PROFILE 0x00000008 *!* #define DIGCF_DEVICEINTERFACE 0x00000010 DECLARE SHORT StrToIntEx IN Shlwapi; STRING pszString, INTEGER dwFlags, INTEGER @pllRet DECLARE INTEGER GetLastError IN kernel32 DECLARE INTEGER SetupDiGetClassDevs IN setupapi.dll ; STRING @cGuid, ; STRING Enumerator, ; INTEGER hwndParent, ; INTEGER nFlags nflags = 0 Enumerator = [] hwndParent = 0 DECLARE INTEGER SetupDiEnumDeviceInterfaces IN setupapi; INTEGER DeviceInfoSet,; STRING @ DeviceInfoData,; STRING @ ByRefterfaceClassGuid,; INTEGER MemberIndex,; STRING @ DeviceInterfaceData sizeof = REPLICATE(' ',2048)+CHR(0) i = 1 nInfo = REPLICATE(' ',2048)+CHR(0) PSP_DEVINFO_DATA = [] lnpointer = 0 lldestroy = .F. DO WHILE .T. lnpointer = SetupDiGetClassDevs(@cGuid,null,0,DIGCF_INTERFACEDEVICE) IF TYPE('lnpointer') = "N" .and. lnpointer > 0 lldestroy = .t. ? lnpointer DeviceInfoSet = lnpointer i = 1 *!!! Bombs on the next line IF !SetupDiEnumDeviceInterfaces(DeviceInfoSet, @nInfo , @cGuid, 0, @sizeof) IF GetLastError() = ERROR_NO_MORE_ITEMS EXIT ELSE EXIT ENDIF ELSE EXIT ENDIF ELSE EXIT ENDIF ENDDO IF lldestroy *--Cleanup ?'Cleaning Up...' DECLARE INTEGER SetupDiDestroyDeviceInfoList IN setupapi; INTEGER DeviceInfoSet llokay = SetupDiDestroyDeviceInfoList(lnpointer) ?llokay ?GetLastError() ENDIF RETURNI am obviously overlooking something I did yesterday but I cannot recall what. Any ideas?
// {B5157D69-75F8-11d3-8CE0-00207815E611} DEFINE_GUID(USBIODS_GUID, 0xb5157d69, 0x75f8, 0x11d3, 0x8c, 0xe0, 0x0, 0x20, 0x78, 0x15, 0xe6, 0x11) HDEVINFO hinfo = SetupDiGetClassDevs(&USBIODS_GUID, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); //Poll the device manager till there are no matching devices left. int i; Cstring Devices[10]; // an array of cstrings for (DWORD i=0; ; ++i) { SP_INTERFACE_DEVICE_DATA Interface_Info; Interface_Info.cbSize = sizeof(Interface_Info); // Enumerate device if (!SetupDiEnumInterfaceDevice(hInfo, NULL, (LPGUID) &USBIODS_GUID,i, &Interface_Info)) { SetupDiDestroyDeviceInfoList(hInfo); return(i); } DWORD needed; // get the required lenght SetupDiGetInterfaceDeviceDetail(hInfo, &Interface_Info, NULL, 0, &needed, NULL); PSP_INTERFACE_DEVICE_DETAIL_DATA detail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc(needed); if (!detail) { SetupDiDestroyDeviceInfoList(hInfo); return(i); } // fill the device details detail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if (!SetupDiGetInterfaceDeviceDetail(hInfo, &Interface_Info,detail, needed,NULL, NULL)) { free((PVOID) detail); SetupDiDestroyDeviceInfoList(hInfo); return(i); } char name[MAX_PATH]; strncpy(name, detail->DevicePath, sizeof(name)); free((PVOID) detail); Devices[i] = name; // keep a copy of each device name } // end of for loopA full example in C which prints by accessing the USB printer device via the setupapi:
#include "stdafx.h" #include <windows.h> #include <setupapi.h> #include <objbase.h> #include "guid829.h" /////guid829 in which i define the interface for USB_PRINT GUID for usbprint.sys that is i am sending and used DEFINE_GUID(USB_PRINT, 0xa5dcbf10,0x6530,0x11d2,0x90,0x1f,0x00,0xc0,0x4f,0xb9,0x51,0xed); ////////////////////////////////////// int main(int argc, char* argv[]) { int MemberIndex = 0; LONG Result=0; DWORD Length = 0; HANDLE hDevInfo; ULONG Required; LPSTR pPort; pPort="USB001"; HANDLE m_hComm; PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = NULL; SP_DEVICE_INTERFACE_DATA devInfoData; hDevInfo = SetupDiGetClassDevs((LPGUID)&(USB_PRINT), NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); if ( hDevInfo == INVALID_HANDLE_VALUE ) { if (pPort) { delete pPort; } printf("No hardware device"); return 0; } devInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); //Step through the available devices looking for the one we want. do { //[1] Result = SetupDiEnumDeviceInterfaces(hDevInfo,0,(LPGUID)&(USB_PRINT), MemberIndex, &devInfoData); if (Result != 0) { SetupDiGetDeviceInterfaceDetail( hDevInfo, &devInfoData, NULL, 0, &Length, NULL); //Allocate memory for the hDevInfo structure, using the returned Length. detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)new BYTE[Length * 4]; //Set cbSize in the detailData structure. detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //Call the function again, this time passing it the returned buffer size. if(SetupDiGetDeviceInterfaceDetail( hDevInfo, &devInfoData, detailData, Length, &Required, NULL)==TRUE) { m_hComm = CreateFile(detailData->DevicePath, GENERIC_READ|GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, 0, NULL); if(m_hComm!=INVALID_HANDLE_VALUE) { Result=0; printf("USB port Available"); } } delete(detailData); } MemberIndex = MemberIndex + 1; }while(Result!=0); SetupDiDestroyDeviceInfoList(hDevInfo); LPWSTR pBuffer; pBuffer=(LPWSTR)HeapAlloc(GetProcessHeap (), HEAP_ZERO_MEMORY,50); pBuffer=L"\n12345678912345678912345678912345678912345/n"; DWORD pcbWritten=0; DWORD cbBuf=49; long ret=0; ret=WriteFile(m_hComm,pBuffer,cbBuf,&pcbWritten,0); if(ret) printf("\n Value Printed"); if(CloseHandle(m_hComm)) printf("\nPort Closed"); return 0; }More examples using C: