PDA

Ver la Versión Completa : Problema al Utilizar SetupDiCreateDeviceInfoList


JoeyJordison
14-01-2007, 01:28:25
Estoy tratando de utilizar SetupDiCreateDeviceInfoList pero me da error en tiempo de compilación mi codigo es el sgte:

procedure TForm1.FormShow(Sender: TObject);
function CrearLista : HDEVINFO;
begin
Result:=SetupDiCreateDeviceInfoList(nil,HWND_DESKTOP);
end;
begin
if (CrearLista = INVALID_HANDLE_VALUE) then
begin
ShowMessage('La creación de la lista falló en: '+inttostr(GetLastError()));
end;
end;

pero me da error de compilación en la sgte línea:
if (CrearLista = INVALID_HANDLE_VALUE) then

me dice:
[Error] Unit1.pas(34): Operator not applicable to this operand type

seoane
14-01-2007, 01:41:36
El problema es que HDEVINFO esta definido de la siguiente manera:

HDEVINFO = Pointer;


Prueba haciendo un typecast:

if CrearLista = Pointer(INVALID_HANDLE_VALUE) then

JoeyJordison
14-01-2007, 01:49:31
Jaja eres un genio.

JoeyJordison
14-01-2007, 01:52:00
ahora tengo otra duda tengo este ejemplo de C:

hDevInfo = SetupDiGetClassDevs(NULL,0, // Enumerator0,DIGCF_PRESENT | DIGCF_ALLCLASSES );


y tengo dudas por ejemplo el caracter | delphi no me lo va a reconocer me dira caracter ilegal, que hago??

seoane
14-01-2007, 01:56:05
:D Me parece que sin saber C se te va a hacer cuesta arriba. Animo!

El simbolo "|" se corresponde con "or" en delphi

JoeyJordison
14-01-2007, 02:07:37
Ese es mi problema es que no si ni papa de C jaja yo lo habia sustituido por and Ñoooo! ! ! ! ! !

Estoy todo confundido con este codigo por ejemplo me dice

return 0;

Jamas he visto eso en delphi. que cosa es??

JoeyJordison
14-01-2007, 02:12:38
tampoco se que poner aca, mira me dice que la sintaxis es la sgte

HDEVINFO SetupDiGetClassDevsEx(
const GUID* ClassGuid,
PCTSTR Enumerator,
HWND hwndParent,
DWORD Flags,
HDEVINFO DeviceInfoSet,
PCTSTR MachineName,
PVOID Reserved
);

todo bien pero que pongo en el handle para que lo necesito?

seoane
14-01-2007, 02:13:24
"return 0" lo podriamos traducir por:

Result:= 0;
Exit;

Es algo que se utiliza mucho en el código en C, pero en delphi, no me gusta mucho eso de salir en la mitad de la función. Pero es lo que hay ...

seoane
14-01-2007, 02:15:44
todo bien pero que pongo en el handle para que lo necesito?


:confused: Eso depende de lo quieras hacer.

JoeyJordison
14-01-2007, 02:16:08
es que en el código de ejemplo jamas me hablan de que es una funcion.

Lo estoy implementando en el evento OnSHow del form

JoeyJordison
14-01-2007, 02:17:44
Solamente quiero hacer una lista de todos los dispositivos de una pc en este caso de la mia.

seoane
14-01-2007, 02:28:03
Bueno, puede que esto te sirva de ayuda:

Coloca un TMemo en el form

uses SetupApi;

procedure TForm4.FormCreate(Sender: TObject);
var
DevInfo: HDEVINFO;
DevInfoData: SP_DEVINFO_DATA;
i: integer;
Data: Pointer;
Size: DWORD;
begin
DevInfo:= SetupDiGetClassDevs(nil,nil,0,DIGCF_ALLCLASSES or DIGCF_PROFILE);
if DevInfo <> HDEVINFO(INVALID_HANDLE_VALUE) then
begin
FillChar(DevInfoData,Sizeof(DevInfoData),0);
DevInfoData.cbSize:= Sizeof(DevInfoData);
for i:= 0 to MAXINT do
begin
if not SetupDiEnumDeviceInfo(DevInfo,i,DevInfoData) then
break;
SetupDiGetDeviceRegistryProperty(DevInfo,DevInfoData,SPDRP_DEVICEDESC,
nil,nil,0,@Size);
if GetLastError = ERROR_INSUFFICIENT_BUFFER then
begin
GetMem(Data,Size);
try
if SetupDiGetDeviceRegistryProperty(DevInfo,DevInfoData,
SPDRP_DEVICEDESC,nil,Data,Size,@Size) then
begin
// Recuerda colcoar el memo
Memo1.Lines.Add(String(PChar(Data)));
end;
finally
FreeMem(Data);
end;
end;
end;
SetupDiDestroyDeviceInfoList(DevInfo);
end;
end;


;) Espero que este código en Delphi te aclare algo las cosas

JoeyJordison
14-01-2007, 02:28:30
Mira ya he creado la función que se supone me recoja la lista de dispositivos del sistema, me quedó así:

var
Form1: TForm1;
DeviceInfoSet : HDEVINFO;
NewDeviceInfoSet : HDEVINFO;

function CrearLista : Pointer;
begin
DeviceInfoSet := SetupDiCreateDeviceInfoList(NiL, HWND_TOP);
if (DeviceInfoSet = Pointer(INVALID_HANDLE_VALUE)) then
begin
ShowMessage('CreateDeviceInfoList failed: '+Inttostr(GetLastError()));
Result:=0;
Exit;
end;
NewDeviceInfoSet := SetupDiGetClassDevsEx(nil,Nil,HWND_DESKTOP,DIGCF_PRESENT or DIGCF_ALLCLASSES,DeviceInfoSet,NiL,NiL);
if (NewDeviceInfoSet = Pointer(Invalid_handle_value)) then
begin
ShowMessage('SetupDiGetClassDevsEx failed: '+inttostr(GetLastError));
Result:=0;
Exit;
end;
Result:=NewDeviceInfoSet;
end;
Para llamarla me dice que debo esperar al evento DBT_DEVICEARRIVAL (http://msdn2.microsoft.com/en-us/library/aa363205.aspx)
algo nuevo para mi, una funcion con eventos?