Inno调用dll --- 实现背景播放音乐

  Inno 播放音乐可以调用系统api函数mciSendString来实现,而mciSendString函数存在C:WindowsSystem32winmm.dll中,因此最终只要成功从该dll文件中导出mciSendString函数,并在安装界面显示的同时正确的调用该函数,即可实现背景播放音乐的效果。

  1、添加背景音乐资源

  在[Files]段添加下面语句:

  Source: "bg.mp3"; flags: dontcopy ;

将音乐文件改名为bg.mp3,放到工程文件同目录下。dontcopy 标志表示安装时此文件不拷贝,默认存在于{tmp}目录下,在软件安装完成后会被删除。

  2、从winmm.dll导出mciSendString函数

  在[code]段添加下面语句:

  function mciSendString(lpszCommand: String; lpszReturnString: Integer; cchReturnLength: Integer; hwndCallback: Integer): Integer;

  external 'mciSendStringA@winmm.dll stdcall';

第一句可以理解为函数定义,第二句是该函数的补充,表示定义的函数是外部函数,原型是从winmm.dll中导出的mciSendStringA函数,采用stdcall函数调用约定。此外,如果Inno编译器版本是unicode版本,则导出的函数对应的原型应该是mciSendStringW,第二句则应修改为:external 'mciSendStringW@winmm.dll stdcall';

  3、使用导出函数播放背景音乐

  背景音乐可以在安装界面初始化的时候开启播放,因此可以选择在[code]段中实现回调函数InitializeSetup来调用mciSendString函数过程。以下是调用过程:

  mciSendString(ExpandConstant('open {tmp}g.mp3 alias bg'),0,0,0);

  mciSendString(ExpandConstant('play bg repeat'),0,0,0);

  4、完整代码

 1 ; 脚本由 Inno Setup 脚本向导 生成!
 2 ; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!
 3 
 4 #define MyAppName "我的程序"
 5 #define MyAppVersion "1.5"
 6 #define MyAppPublisher "我的公司"
 7 #define MyAppURL "http://www.example.com/"
 8 #define MyAppExeName "MyProg.exe"
 9 
10 [Setup]
11 ; 注: AppId的值为单独标识该应用程序。
12 ; 不要为其他安装程序使用相同的AppId值。
13 ; (生成新的GUID,点击 工具|在IDE中生成GUID。)
14 AppId={{230CCD19-DFE0-4D57-A14D-A3AE3AED90EE}
15 AppName={#MyAppName}
16 AppVersion={#MyAppVersion}
17 ;AppVerName={#MyAppName} {#MyAppVersion}
18 AppPublisher={#MyAppPublisher}
19 AppPublisherURL={#MyAppURL}
20 AppSupportURL={#MyAppURL}
21 AppUpdatesURL={#MyAppURL}
22 DefaultDirName={pf}{#MyAppName}
23 DisableProgramGroupPage=yes
24 OutputBaseFilename=setup
25 Compression=lzma
26 SolidCompression=yes
27 
28 [Languages]
29 Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"
30 
31 [Files]
32 ;Source: "C:Program Files (x86)Inno Setup 5ExamplesMyProg.exe"; DestDir: "{app}"; Flags: ignoreversion
33 Source: "bg.mp3"; flags: dontcopy ;
34 ; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”
35 
36 [code]
37 function mciSendString(lpszCommand: String; lpszReturnString: Integer; cchReturnLength: Integer; hwndCallback: Integer): Integer;
38 external 'mciSendStringA@winmm.dll stdcall';//unicode版本用 mciSendStringW ansi版本用mciSendStringA
39 
40 function InitializeSetup (): Boolean;
41 begin
42     ExtractTemporaryFile(ExtractFileName(ExpandConstant('{tmp}g.mp3')));  //提前抽取该文件
43     mciSendString(ExpandConstant('open {tmp}g.mp3 alias bg'),0,0,0);  //打开媒体文件,alias别名为bg
44     mciSendString(ExpandConstant('play bg repeat'),0,0,0);  //重复播放(别)名为bg.mp3的媒体文件
45     Result := True;  //返回true才会继续安装
46 end;
原文地址:https://www.cnblogs.com/hbcppcdr/p/6248396.html