Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help with Parameter Passed to Win32 API
Message
From
10/10/2006 15:52:09
 
 
To
10/10/2006 15:07:36
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:
01161029
Views:
20
>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
Herman
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform