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;