Writing simplest module (Start WinAMP)

Module SDK, articles and other usefull information.

Writing simplest module (Start WinAMP)

Postby Nikolai Kurkov » 01 Feb 2008, 23:00

Image
Let’s write a module which will start WinAMP when system is idle and stop it when system is in use.

Steps:
1. Use Empty module from SDK.
2. Fill out data for function GetModuleInfo:
Code: Select all

// Module information
function GetModuleInfo(Language: String):TModuleInfo;stdcall;
var
  Buffer:array [byte] of char;
begin
  with Result do begin
    if GetModuleFileName(hInstance,@Buffer,SizeOf(Buffer)-1)>0 then
    begin
    // Путь к модулю
    FileName:=ExtractFileName(StrPas(Buffer));
    Path:=ExtractFilePath(StrPas(Buffer));
      Name:='WinAMP';
      Description:='This module play music while computer idle';
      Data:='02.02.2008';
      Author:='Nikolai Kurkov';
      AuthorEmail:='nikkurkov@gmail.com';
  end;
end;
end;

3. Before launch, information about where WinAMP is installed needs to be collected, let’s do that:
Code: Select all
Uses Registry;
Var WinAMPPath: String; PlayList: String; Process, Thread: Cardinal;
// Initialization
procedure Initialize;stdcall;
var
Reg: TRegistry;
begin
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winamp_is1') then
WinAMPPath:=Reg.ReadString('Inno Setup: App Path')
else
WinAMPPath:='';
end;

4. In order for module to work WinAMP is required, right? Let’s check:
Code: Select all
// Compatible test
function Compatible: Boolean;stdcall;
begin
Result:=FileExists(WinAMPPath+'\winamp.exe');
end;

5. Now, settings window, where playlist is selected, needs to be created:
Code: Select all
// Show module settings
procedure Settings;stdcall;
begin
// Create new form
  SettingsForm := TSettingsForm.Create(Application);
  SettingsForm.OpenDialog1.FileName:=PlayList;
  SettingsForm.Edit1.Text:=PlayList;
// Show form
SettingsForm.ShowModal;
// If "Ok" choosen in settings window
if SettingsForm.ModalResult= mrOk then
begin
PlayList:=SettingsForm.OpenDialog1.FileName;
// Save settings
{Save action}
end;
SettingsForm.Free;
end;

6. After that, write the main function: launch.
a) I create procedure to run executable file like winamp.exe:
Code: Select all
// Run procedure
procedure Startz;
const
  ReadBuffer = 2400;
var
  Security: TSecurityAttributes;
  start: TStartUpInfo;
  ProcessInfo: TProcessInformation;
  ExitCode: Cardinal;
begin
  with Security do
  begin
    nlength := SizeOf(TSecurityAttributes);
    binherithandle := true;
    lpsecuritydescriptor := nil;
  end;
    FillChar(Start, Sizeof(Start), #0);
    start.cb := SizeOf(start);
    start.dwFlags := STARTF_USESTDHANDLES +
      STARTF_USESHOWWINDOW;
    start.wShowWindow := SW_HIDE;
// Create process
     if CreateProcess(nil, PChar(WinAMPPath+'\winamp.exe '+Playlist),
      @Security,
      @Security,
      true,
      NORMAL_PRIORITY_CLASS,
      nil,
      nil,
      start,
      ProcessInfo) then
    begin
    with ProcessInfo do
    begin
    Process:=ProcessInfo.hProcess;
    Thread:=ProcessInfo.hThread;
    WaitForInputIdle(Process, INFINITE); // waiting for close
    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
     GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
     CloseHandle(Thread);
    CloseHandle(Process);
    end;
  end;
end;

b) Then just call this procedure to run Winamp with parameters:
Code: Select all
// Start module
function StartPlugin: Boolean;stdcall;
begin
Startz;
end;

7. Stop and pause functions are also required, they will be identical:
Code: Select all
// Stop plugin
function StopPlugin: Boolean;stdcall;
begin
TerminateProcess(Process, 0);
end;

// Pause plugin
function PausePlugin:Boolean;stdcall;
begin
TerminateProcess(Process, 0);
end;

8. Module is ready! Now add icons (winamp.bmp (16x16) and winamp -icon64.png (64x64). It can be compiled and ready to use.

I also attached original code for Delphi for this example to this article.

See also:
Program for testing modules
Empty module
Attachments
winamp.bop
WinAMP compiled module
(524 KiB) Downloaded 88 times
winamp-sources.zip
WinAMP module sources
(16.41 KiB) Downloaded 89 times
User avatar
Nikolai Kurkov
Администратор
 
Posts: 128
Joined: 08 Jan 2008, 19:09

Return to For developers



cron