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;
http://www.bkjia.com/PHPjc/632336.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632336.htmlTechArticledelphi 枚舉裝置使用代碼 現在的 delphi(2010、xe) 已經內建了 directx 的相關單元(...sourcertlwin). --------------------------------------------------------------...