kelvin00929您好: 請試試這個範例:
Unit1.H
//--------------------------------------------------------------------------- #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TListBox *ListBox1;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif Unit1.cpp //--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#pragma argsused
#define MAX_DEV_LEN 1000
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- int EnumerateDevices(ULONG DeviceIndex, char *ClassName, char *DeviceName)
{
DWORD RequiredSize = 0;
GUID *guids;
HDEVINFO NewDeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
DWORD rs;
DWORD typeP;
bool res=SetupDiClassGuidsFromNameA(ClassName,
guids,
RequiredSize,
&RequiredSize);
if(RequiredSize==0){
//incorrect class name:
*DeviceName=0;
return -2;
}
if(!res)
{
guids=(GUID *)malloc(RequiredSize*sizeof(GUID));
res=SetupDiClassGuidsFromNameA(ClassName,
guids,
RequiredSize,
&RequiredSize);
if(!res || RequiredSize==0)
{
//incorrect class name:
realloc(guids,0);
*DeviceName=0;
return -2;
}
}
NewDeviceInfoSet=SetupDiGetClassDevs(guids,
NULL,
NULL,
DIGCF_PRESENT);
if( NewDeviceInfoSet == INVALID_HANDLE_VALUE )
if(!res) {
//device information is unavailable:
realloc(guids,0);
*DeviceName=0;
return -3;
} DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
res=SetupDiEnumDeviceInfo(NewDeviceInfoSet,DeviceIndex,&DeviceInfoData);
if(!res) {
//no such device:
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
realloc(guids,0);
*DeviceName=0;
return -1;
} if(!SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,&DeviceInfoData,
SPDRP_FRIENDLYNAME,&typeP,DeviceName,MAX_DEV_LEN,&rs) )
{
res = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&typeP,
DeviceName,
MAX_DEV_LEN,
&rs);
if(!res){
//incorrect device name:
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
realloc(guids,0);
*DeviceName=0;
return -4;
}
}
realloc(guids,0);
return 0;
} void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *devices=0;
char *devicesOEM=0;
int Index=0;
devices=(char*)malloc(MAX_DEV_LEN);
devicesOEM=(char*)malloc(MAX_DEV_LEN);
while(true)
{
int result=EnumerateDevices(Index, "USB", devices);
Index++;
if(result == -2)
{
ShowMessage("Incorrect name of Class GPIB");
break;
}
if(result == -1)break;
if(result == 0)
{
CharToOem(devices,devicesOEM);
ListBox1->Items->Add(devicesOEM);
}
}
realloc(devices,0);
realloc(devicesOEM,0);
// return 0;
}
//---------------------------------------------------------------------------
/*生活是一種藝術,用心生活享受生活*/
發表人 - axsoft 於 2004/05/27 14:58:47