Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help with Parameter Passed to Win32 API
Message
From
10/10/2006 16:18:26
 
 
To
10/10/2006 15:52:09
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01160965
Message ID:
01161032
Views:
17
Thanks Herman, your code is great. Portions of it are exactly what I was working on. However, I cannot use the HIDd api because the USB device I am working with is NOT a true human interface device standard and doesn't respond to the HIDd api calls, but can be accessed directly via USB. I started out a month ago using the HIDd api until I found out the device does not list itself as HID device, but only a USB Printer which can be written to and read from (it contains a card reader). I tested your code using either the PRINTER GUID or the HARD DRIVE GUID and neither return any information (except if I pass it a HID Guid then it does return information on my MSFT Keyboard which is indeed a true HID device):
#Define MAX_PATH                260
#Define ERROR_NO_MORE_ITEMS     259
#Define INVALID_HANDLE_VALUE    -1
#Define DIGCF_DEVICEINTERFACE   0x0010
#Define DIGCF_PRESENT           0x0002

#Define GUID_Size  16
#Define SP_DEVICE_INTERFACE_DATA_Size  28
#Define SP_DEVICE_INTERFACE_DETAIL_DATA_Size  5

Local ls_GUID, lh_DevInfo

Declare Long GetLastError in Kernel32
Declare Long CloseHandle in Kernel32 Long hObject
Declare Long CreateFile in Kernel32 ;
   String cFileName, Long dwDesiredAccess, Long dwShareMode, ;
   String @ lpSecurityAttributes, Long dwCreationDisposition, ;
   Long dwFlagsAndAttributes, Long hTemplateFile

Declare HidD_GetHidGuid in HID ;
   String @ O_sHidGuid

Declare Long HidD_GetProductString in HID ;
   Long hHidDeviceObject, String @ O_cBuffer, Long nBufferLength

Declare Long SetupDiDestroyDeviceInfoList in SetupAPI ;
   Long hDeviceInfoSet

Declare Long SetupDiGetClassDevs in SetupAPI ;
   String @ sGUID, String Enumerator, Long nhWnd, Long nFlags

Declare Long SetupDiEnumDeviceInterfaces in SetupAPI ;
   Long hDeviceInfo, String @ sDeviceInfoData, ;
   String @ sInterfaceClassGuid, Long MemberIndex, ;
   String @ O_sDeviceInterfaceData

Declare Long SetupDiGetDeviceInterfaceDetail in SetupAPI ;
   Long hDeviceInfo, String @ sDeviceInterfaceData, ;
   String @ O_sDeviceInterfaceDetailData, Long DeviceInterfaceDetailDataSize, ;
   Long @ O_nRequiredSize, String @ O_sDeviceInfoData


****************************************
** Main

ls_HidGuid = space( GUID_Size )
HidD_GetHidGuid( @ls_HidGuid )
#DEFINE GUID_DEVCLASS_PRINTER ;
	CHR(0x4D) + CHR(0x36) + CHR(0xE9) + CHR(0x79) + ;
	CHR(0xE3) + CHR(0x25) + CHR(0x11) + CHR(0xCE) + ;
	CHR(0xBF) + CHR(0xC1) + CHR(0x08) + CHR(0x00) + ;
	CHR(0x2B) + CHR(0xE1) + CHR(0x03) + CHR(0x18)
#Define GUID_DEVCLASS_DISKDRIVE ;
	Chr(0x67) + Chr(0xE9) + Chr(0x36) + Chr(0x4D) + ;
	Chr(0x25) + Chr(0xE3) + ;
	Chr(0xCE) + Chr(0x11) + ;
	Chr(0xBF) + Chr(0xC1) + Chr(0x08) + Chr(0x00) + Chr(0x2B) + Chr(0xE1) + Chr(0x03) + Chr(0x18)

ls_hidGuid = GUID_DEVCLASS_DISKDRIVE

If !empty( ls_HidGuid )
   lh_DevInfo = SetupDiGetClassDevs( ls_HidGuid, 0, 0, ;
      DIGCF_PRESENT + DIGCF_DEVICEINTERFACE )

   If (lh_DevInfo != INVALID_HANDLE_VALUE)
      Clear
      EnumerateDevices( lh_DevInfo, ls_HidGuid )
      SetupDiDestroyDeviceInfoList( lh_DevInfo )
   endif
endif

* Clear Dlls


****************************************
** Procedures

Procedure EnumerateDevices( th_DevInfo, ts_Guid )
   Local ls_DiData, ls_DiDetailData, lh_Device
   Local ln_Error, ln_Index, ln_ReqSize
   Local lc_DeviceName, lw_Buffer, lc_Buffer

   Store 0 to ln_Error, ln_Index, ln_ReqSize
   ls_DiData = padr( BinToC( SP_DEVICE_INTERFACE_DATA_Size, '4rs' ), ;
      SP_DEVICE_INTERFACE_DATA_Size, chr(0) )

   Do while (ln_Error != ERROR_NO_MORE_ITEMS)
      If (SetupDiEnumDeviceInterfaces( th_DevInfo, 0, ts_Guid, ln_Index, @ls_DiData ) == 0)
         ln_Error = GetLastError()
         If (ln_Error != ERROR_NO_MORE_ITEMS)
            ShowError( ln_Error, ln_Index, 'SetupDiEnumDeviceInterfaces()' )
         ELSE
         	? [ No More Devices]
         endif
      else

         SetupDiGetDeviceInterfaceDetail( th_DevInfo, ls_DiData, ;
            0, 0, @ln_ReqSize, 0 )
         ls_DiDetailData = padr( BinToC( SP_DEVICE_INTERFACE_DETAIL_DATA_Size, '4rs' ), ;
            ln_ReqSize, chr(0) )
         ln_Result = SetupDiGetDeviceInterfaceDetail( th_DevInfo, ls_DiData, ;
            @ls_DiDetailData, ln_ReqSize, @ln_ReqSize, 0 )

         If (ln_Result == 1)
            lc_DeviceName = substr( ls_DiDetailData, 5 )
            ln_Pos = at( chr(0), lc_DeviceName )
            If (ln_Pos > 0)
               lc_DeviceName = left( lc_DeviceName, ln_Pos - 1 )
            endif
?lc_DeviceName
            lh_Device = CreateFile( lc_DeviceName, 0x80000000, 0x1, 0, 3, 0x20000000, 0 )
            If (lh_Device != INVALID_HANDLE_VALUE)
               lw_Buffer = space( MAX_PATH )
               If (HidD_GetProductString( lh_Device, @lw_Buffer, MAX_PATH ) == 1)
                  lc_Buffer = MakeANSI( lw_Buffer )
                  ? 'Product Name: ' + lc_Buffer
               endif
               CloseHandle( lh_Device )
            else
               ShowError( GetLastError(), ln_Index, 'CreateFile()' )
            endif
         else
            ShowError( GetLastError(), ln_Index, 'SetupDiGetDeviceInterfaceDetail()' )
         endif
         ln_Index = ln_Index + 1
      endif
   enddo
EndProc


Procedure ShowError( tn_Error, tn_Index, tc_Message )
   ? tc_Message + ' ERROR !!'
   If (VarType( tn_Index ) == 'N')
      ? 'Member index:', tn_Index
   endif
   ? 'Error No:', tn_Error
EndProc


Procedure MakeANSI( tw_String )
   Local lc_String, ln_Pos

   lc_String = strconv( strconv( tw_String, 6 ), 2 )
   ln_Pos = at( chr(0), lc_String )
   If (ln_Pos > 0)
      lc_String = left( lc_String, ln_Pos - 1 )
   endif

   Return lc_String
EndProc
>>I looked at that and was sure that I added the size up correctly. Just goes to show you! Thanks Herman. However, I wonder why it is returning No more Devices (259)? the guid I'm passing is for the hard drive, and it works if I check the registry. Can you run this code and let me know what it returns for you?
>
>It returns the same for me (no more devices). I have a question, where did you get the GUID_DEVCLASS_DISKDRIVE? Also, I tried to run the commented part, all returns ERROR
>
>This code works for me to enumerate the USB Devices, try this:
>
>#Define MAX_PATH                260
>#Define ERROR_NO_MORE_ITEMS     259
>#Define INVALID_HANDLE_VALUE    -1
>#Define DIGCF_DEVICEINTERFACE   0x0010
>#Define DIGCF_PRESENT           0x0002
>
>#Define GUID_Size  16
>#Define SP_DEVICE_INTERFACE_DATA_Size  28
>#Define SP_DEVICE_INTERFACE_DETAIL_DATA_Size  5
>
>Local ls_GUID, lh_DevInfo
>
>Declare Long GetLastError in Kernel32
>Declare Long CloseHandle in Kernel32 Long hObject
>Declare Long CreateFile in Kernel32 ;
>   String cFileName, Long dwDesiredAccess, Long dwShareMode, ;
>   String @ lpSecurityAttributes, Long dwCreationDisposition, ;
>   Long dwFlagsAndAttributes, Long hTemplateFile
>
>Declare HidD_GetHidGuid in HID ;
>   String @ O_sHidGuid
>
>Declare Long HidD_GetProductString in HID ;
>   Long hHidDeviceObject, String @ O_cBuffer, Long nBufferLength
>
>Declare Long SetupDiDestroyDeviceInfoList in SetupAPI ;
>   Long hDeviceInfoSet
>
>Declare Long SetupDiGetClassDevs in SetupAPI ;
>   String @ sGUID, String Enumerator, Long nhWnd, Long nFlags
>
>Declare Long SetupDiEnumDeviceInterfaces in SetupAPI ;
>   Long hDeviceInfo, String @ sDeviceInfoData, ;
>   String @ sInterfaceClassGuid, Long MemberIndex, ;
>   String @ O_sDeviceInterfaceData
>
>Declare Long SetupDiGetDeviceInterfaceDetail in SetupAPI ;
>   Long hDeviceInfo, String @ sDeviceInterfaceData, ;
>   String @ O_sDeviceInterfaceDetailData, Long DeviceInterfaceDetailDataSize, ;
>   Long @ O_nRequiredSize, String @ O_sDeviceInfoData
>
>
>****************************************
>** Main
>
>ls_HidGuid = space( GUID_Size )
>HidD_GetHidGuid( @ls_HidGuid )
>If !empty( ls_HidGuid )
>   lh_DevInfo = SetupDiGetClassDevs( ls_HidGuid, 0, 0, ;
>      DIGCF_PRESENT + DIGCF_DEVICEINTERFACE )
>
>   If (lh_DevInfo != INVALID_HANDLE_VALUE)
>      Clear
>      EnumerateDevices( lh_DevInfo, ls_HidGuid )
>      SetupDiDestroyDeviceInfoList( lh_DevInfo )
>   endif
>endif
>
>* Clear Dlls
>
>
>****************************************
>** Procedures
>
>Procedure EnumerateDevices( th_DevInfo, ts_Guid )
>   Local ls_DiData, ls_DiDetailData, lh_Device
>   Local ln_Error, ln_Index, ln_ReqSize
>   Local lc_DeviceName, lw_Buffer, lc_Buffer
>
>   Store 0 to ln_Error, ln_Index, ln_ReqSize
>   ls_DiData = padr( BinToC( SP_DEVICE_INTERFACE_DATA_Size, '4rs' ), ;
>      SP_DEVICE_INTERFACE_DATA_Size, chr(0) )
>
>   Do while (ln_Error != ERROR_NO_MORE_ITEMS)
>      If (SetupDiEnumDeviceInterfaces( th_DevInfo, 0, ts_Guid, ln_Index, @ls_DiData ) == 0)
>         ln_Error = GetLastError()
>         If (ln_Error != ERROR_NO_MORE_ITEMS)
>            ShowError( ln_Error, ln_Index, 'SetupDiEnumDeviceInterfaces()' )
>         endif
>      else
>
>         SetupDiGetDeviceInterfaceDetail( th_DevInfo, ls_DiData, ;
>            0, 0, @ln_ReqSize, 0 )
>         ls_DiDetailData = padr( BinToC( SP_DEVICE_INTERFACE_DETAIL_DATA_Size, '4rs' ), ;
>            ln_ReqSize, chr(0) )
>         ln_Result = SetupDiGetDeviceInterfaceDetail( th_DevInfo, ls_DiData, ;
>            @ls_DiDetailData, ln_ReqSize, @ln_ReqSize, 0 )
>
>         If (ln_Result == 1)
>            lc_DeviceName = substr( ls_DiDetailData, 5 )
>            ln_Pos = at( chr(0), lc_DeviceName )
>            If (ln_Pos > 0)
>               lc_DeviceName = left( lc_DeviceName, ln_Pos - 1 )
>            endif
>
>            lh_Device = CreateFile( lc_DeviceName, 0x80000000, 0x1, 0, 3, 0x20000000, 0 )
>            If (lh_Device != INVALID_HANDLE_VALUE)
>               lw_Buffer = space( MAX_PATH )
>               If (HidD_GetProductString( lh_Device, @lw_Buffer, MAX_PATH ) == 1)
>                  lc_Buffer = MakeANSI( lw_Buffer )
>                  ? 'Product Name: ' + lc_Buffer
>               endif
>               CloseHandle( lh_Device )
>            else
>               ShowError( GetLastError(), ln_Index, 'CreateFile()' )
>            endif
>         else
>            ShowError( GetLastError(), ln_Index, 'SetupDiGetDeviceInterfaceDetail()' )
>         endif
>         ln_Index = ln_Index + 1
>      endif
>   enddo
>EndProc
>
>
>Procedure ShowError( tn_Error, tn_Index, tc_Message )
>   ? tc_Message + ' ERROR !!'
>   If (VarType( tn_Index ) == 'N')
>      ? 'Member index:', tn_Index
>   endif
>   ? 'Error No:', tn_Error
>EndProc
>
>
>Procedure MakeANSI( tw_String )
>   Local lc_String, ln_Pos
>
>   lc_String = strconv( strconv( tw_String, 6 ), 2 )
>   ln_Pos = at( chr(0), lc_String )
>   If (ln_Pos > 0)
>      lc_String = left( lc_String, ln_Pos - 1 )
>   endif
>
>   Return lc_String
>EndProc
>
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform