Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Return handle to device set from SetupDiGetClassDevs
Message
De
27/09/2006 14:46:04
 
 
À
Tous
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Titre:
Return handle to device set from SetupDiGetClassDevs
Versions des environnements
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01157607
Message ID:
01157607
Vues:
1836
SetupDiGetClassDevs returns 37845736 (for example) and that is a handle to a device set. I guess that is a pointer? Passing that value to SetupDiEnumDeviceInterfaces I receive a datatype mismatch error which suggest the type of values I pass are incorrect. How do I access a handle to a device set (what would be hdevinfo) correctly? Is the numeric result (just like in a file handle) correct? Is it simply that I am not setting up the parameters correctly (other than the number returned from SetupDiGetClassDevs) before passing them to SetupDiEnumDeviceInterfaces?

The below code works down to the portion which passes the value returned from SetupDiGetClassDevs to SetupDiEnumDeviceInterfaces:
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
RETURN
I am obviously overlooking something I did yesterday but I cannot recall what. Any ideas?

It would be something like the below in C:
// {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 loop
A 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:
http://www.codeproject.com/system/DevMgr.asp
http://www.codeproject.com/useritems/SimpleSetup.asp
.·*´¨)
.·`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"
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform