随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


现在的 Delphi(2010、XE) 已经自带了 DirectX 的相关单元(...\source\rtl\win\).
//枚举函数
function DirectSoundEnumerate(
  lpDSEnumCallback: TDSEnumCallback; //回调函数
  lpContext: Pointer                 //用户指针
): HResult; stdcall; //返回错误代码, 成功则返回 DS_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;

posted on   万一  阅读(4421)  评论(11编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2009-01-12 Delphi 与 DirectX 之 DelphiX(6): 让 TDXImageList 和常规 VCL 交互使用
2009-01-12 Delphi 与 DirectX 之 DelphiX(5): 初识 TDXImageList
2009-01-12 Delphi 与 DirectX 之 DelphiX(4): 初识 TDXTimer
2009-01-12 Delphi 与 DirectX 之 DelphiX(3): 初识 TDXDraw


点击右上角即可分享
微信分享提示