delphi enumeration device usage code
Now delphi (2010, xe) already comes with directx related units (...sourcertlwin).
-------------------------------------------------- ----------------------------------
//Enumeration function
function directsoundenumerate(
lpdsenumcallback: tdsenumcallback; //Callback function
lpcontext: pointer //User pointer
): hresult; stdcall; //Return error code, if successful, return s_ok(0)
//The prototype of the callback function required by directsoundenumerate:
tdsenumcallback = function(
lpguid: pguid; //Guid of the device
lpcstrdescription: pchar; //Device description
lpcstrmodule: pchar; //Module identification
lpcontext: pointer //User pointer provided by directsoundenumerate
): bool; stdcall; //Returning true means that we want to continue enumeration. If we no longer want to continue searching, return false
-------------------------------------------------- ----------------------------------
This is the common code:
-------------------------------------------------- ----------------------------------
unit unit1;
interface
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls;
type
tform1 = class(tform)
Listbox1: tlistbox; //Only one list box is placed on the form
Procedure formcreate(sender: tobject);
end;
var
form1: tform1;
implementation
{$r *.dfm}
uses directsound; //!
function enumcallback(lpguid: pguid; lpcstrdescription, lpcstrmodule: pchar;
lpcontext: pointer): bool; stdcall;
begin
form1.listbox1.items.add(lpcstrdescription);
result := true;
end;
procedure tform1.formcreate(sender: tobject);
begin
directsoundenumerate(enumcallback, nil);
end;
end.
-------------------------------------------------- ----------------------------------
It is not good to use the form control directly in the callback function. Modify it as follows:
-------------------------------------------------- ----------------------------------
uses directsound;
function enumcallback(lpguid: pguid; lpcstrdescription, lpcstrmodule: pchar;
lpcontext: pointer): bool; stdcall;
begin
tstrings(lpcontext).add(lpcstrdescription);
result := true;
end;
procedure tform1.formcreate(sender: tobject);
begin
directsoundenumerate(enumcallback, listbox1.items);
end;
-------------------------------------------------- ----------------------------------
Get more information:
-------------------------------------------------- ----------------------------------
uses directsound;
function enumcallback(lpguid: pguid; lpcstrdescription, lpcstrmodule: pchar;
lpcontext: pointer): bool; stdcall;
begin
if lpguid <> nil then tstrings(lpcontext).add(guidtostring(lpguid^));
tstrings(lpcontext).add(lpcstrdescription);
if lpcstrmodule <> nil then tstrings(lpcontext).add(lpcstrmodule);
tstrings(lpcontext).add(emptystr);
result := true;
end;
procedure tform1.formcreate(sender: tobject);
begin
directsoundenumerate(enumcallback, listbox1.items);
end;