你再试试这个:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ImgList, ExtCtrls;
const
DIGCF_PRESENT = $00000002;
DIGCF_ALLCLASSES = $00000004;
SPDRP_DEVICEDESC = $00000000;
SPDRP_FRIENDLYNAME = $0000000C;
LINE_LEN = 256;
type
PSP_DEVINFO_DATA = ^SP_DEVINFO_DATA;
SP_DEVINFO_DATA = record
cbSize: DWORD;
ClassGuid: TGUID;
DevInst: DWORD;
Reserved: LongInt;
end;
type
TForm1 = class(TForm)
btn1: TButton;
Memo1: TMemo;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function SetupDiGetClassDevs(const ClassGuid: PGUID; Enumerator: PChar;
hwndParent: HWND; Flags: DWORD): Cardinal; stdcall;
external 'Setupapi.dll' name 'SetupDiGetClassDevsA';
function SetupDiEnumDeviceInfo(DeviceInfoSet: Cardinal; MemberIndex: DWORD;
DeviceInfoData: PSP_DEVINFO_DATA): BOOL; stdcall;
external 'Setupapi.dll' name 'SetupDiEnumDeviceInfo';
function SetupDiGetDeviceRegistryProperty(DeviceInfoSet: Cardinal;
DeviceInfoData: PSP_DEVINFO_DATA; Propertys: DWORD;
PropertyRegDataType: PWORD; PropertyBuffer: PByte;
PropertyBufferSize: DWORD; RequiredSize: PDWORD): BOOL; stdcall;
external 'Setupapi.dll' name 'SetupDiGetDeviceRegistryPropertyA';
function SetupDiDestroyDeviceInfoList(DeviceInfoSet: Cardinal): BOOL; stdcall;
external 'Setupapi.dll' name 'SetupDiDestroyDeviceInfoList';
function SetupDiClassNameFromGuid(ClassGuid: PGUID; ClassName: PChar;
ClassNameSize: DWORD; RequiredSize: PDWORD): BOOL; stdcall;
external 'Setupapi.dll' name 'SetupDiClassNameFromGuidA';
function DeviceClassName(Guid: TGUID): string;
var
ClassName: PChar;
ClassNameSzie: Cardinal;
begin
ClassNameSzie := 0;
GetMem(ClassName, ClassNameSzie);
while not SetupDiClassNameFromGuid(@Guid, ClassName,
ClassNameSzie, @ClassNameSzie) do
begin
if (GetLastError() = ERROR_INSUFFICIENT_BUFFER) then
begin
if ClassName <> nil then FreeMem(ClassName);
GetMem(ClassName, ClassNameSzie);
end else
Break;
end;
Result := StrPas(ClassName);
if ClassName <> nil then FreeMem(ClassName);
end;
function DeviceFirendName(DeviceInfoSet: Cardinal;
DeviceInfoData: PSP_DEVINFO_DATA; var Name: string): Boolean;
var
DataT, buffersize: DWORD;
buffer: PChar;
begin
buffersize := 256;
buffer := AllocMem(buffersize);
if not SetupDiGetDeviceRegistryProperty(DeviceInfoSet, DeviceInfoData,
SPDRP_FRIENDLYNAME, @DataT, PByte(buffer), buffersize, @buffersize) then
begin
Result := SetupDiGetDeviceRegistryProperty(DeviceInfoSet, DeviceInfoData,
SPDRP_DEVICEDESC, @DataT, PByte(buffer), buffersize, @buffersize)
end
else
Result := True;
if Result then
Name := StrPas(buffer)
else
Name := 'Unknow';
FreeMem(buffer);
end;
procedure TForm1.btn1Click(Sender: TObject);
var
hDevInfo: Cardinal;
DeviceInfoData: SP_DEVINFO_DATA;
i : DWORD;
Name: string;
begin
Memo1.Lines.Clear;
hDevInfo := SetupDiGetClassDevs(nil, 0, 0, DIGCF_PRESENT or DIGCF_ALLCLASSES);
if (hDevInfo = INVALID_HANDLE_VALUE) then Exit;
DeviceInfoData.cbSize := SizeOf(SP_DEVINFO_DATA);
i := 0;
while SetupDiEnumDeviceInfo(hDevInfo, i, @DeviceInfoData) do
begin
if DeviceClassName(DeviceInfoData.ClassGuid)='Display' then begin
DeviceFirendName(hDevInfo, @DeviceInfoData, Name);
Memo1.Lines.Add(Name);
end ;
Inc(i);
end;
SetupDiDestroyDeviceInfoList(hDevInfo);
end;
end.