delphi 枚举设备使用代码
现在的 delphi(2010、xe) 已经自带了 directx 的相关单元(...sourcertlwin).
--------------------------------------------------------------------------------
//枚举函数
function directsoundenumerate(
lpdsenumcallback: tdsenumcallback; //回调函数
lpcontext: pointer //用户指针
): hresult; stdcall; //返回错误代码, 成功则返回 s_ok(0)
//directsoundenumerate 需要的回调函数的原形:
tdsenumcallback = function(
lpguid: pguid; //设备的 guid
lpcstrdescription: pchar; //设备描述
lpcstrmodule: pchar; //模块标识
lpcontext: pointer //由 directsoundenumerate 提供的用户指针
): bool; stdcall; //返回 true 表示要继续枚举, 不在继续找了就返回 false
--------------------------------------------------------------------------------
这是常见的代码:
--------------------------------------------------------------------------------
unit unit1;
interface
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls;
type
tform1 = class(tform)
listbox1: tlistbox; //只在窗体上放了一个列表框
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.
--------------------------------------------------------------------------------
在回调函数中直接使用窗体控件不好, 修改如下:
--------------------------------------------------------------------------------
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;
--------------------------------------------------------------------------------
获取更多信息:
--------------------------------------------------------------------------------
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;