home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / ctenari / Hadraba / Source / autorun.dpr < prev    next >
Text File  |  2002-08-12  |  3KB  |  116 lines

  1. program autorun;
  2.  
  3. uses
  4.   Forms,
  5.   SysUtils, Windows, ShellAPI,
  6.   MainForm in 'MainForm.pas' {MainForm1},
  7.   AboutForm in 'AboutForm.pas' {AboutForm1},
  8.   HelpForm in 'HelpForm.pas' {HelpForm1},
  9.   ShortCutForm in 'ShortCutForm.pas' {ShortCutForm1},
  10.   ErrorForm in 'ErrorForm.pas' {ErrorForm1},
  11.   EventForm in 'EventForm.pas' {EventForm1};
  12.  
  13. {$R *.RES}
  14. {$R icons.res}
  15.  
  16. const
  17.   UniqueAppString : PChar   = 'AutoRun 1.0 by Hadraba-Soft';
  18.   MutexHandle     : THandle = 0;
  19.  
  20. var
  21.   S               : String;
  22.   OrgExitProc     : Pointer;
  23.   MessageID       : Integer;
  24.   WProc           : TFNWndProc = nil;
  25.   BSMRecipients   : dWord;
  26.   i               : LongInt;
  27.  
  28. function BroadcastSystemMessage(Flags: DWORD; Recipients: PDWORD;
  29.   uiMessage: UINT; wParam: WPARAM; lParam: LPARAM): Longint; stdcall;
  30.   external user32 name 'BroadcastSystemMessage';
  31.  
  32. function NewWndProc(Handle : hWnd; Msg : Integer; wParam,
  33.   lParam : LongInt) : LongInt; stdcall;
  34. begin
  35.   Result := 0;
  36.   If Msg = MessageID then
  37.     begin
  38.     If IsIconic(Application.Handle) then
  39.       begin
  40.       Application.MainForm.WindowState := wsNormal;
  41.       Application.Restore;
  42.       end;
  43.     SetForegroundWindow(Application.MainForm.Handle);
  44.     end
  45.   else
  46.     Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
  47. end;
  48.  
  49. procedure HaltApplication; far;
  50. begin
  51.   ExitProc := OrgExitProc;
  52.   ReleaseMutex(MutexHandle);
  53.   Halt(ExitCode);
  54. end;
  55.  
  56. begin
  57. OrgExitProc := ExitProc;
  58. ExitProc    := @HaltApplication;
  59. MessageID   := RegisterWindowMessage(UniqueAppString);
  60. If ParamCount > 0 then
  61.   begin
  62.   For i := 1 to ParamCount do
  63.     begin
  64.     If ParamStr(i) = ':' then
  65.       ShellExecute(0, 'open', PChar(ExtractFileDrive(ParamStr(0))), '', '',
  66.         SW_Normal)
  67.     else
  68.       begin
  69.       If ShellExecute(0, 'open', PChar(ParamStr(i)), '', '',
  70.         SW_Normal) <= 32 then
  71.         MessageBox(0, PChar('Can''t open ' + ParamStr(i) +
  72.           '. Operation aborted.'), 'AutoRun Version 1.0',
  73.           mb_IconExclamation or mb_Ok);
  74.       end;
  75.     end;
  76.     Halt(0);
  77.   end;
  78. If OpenMutex(Mutex_All_Access, False, UniqueAppString) = 0 then
  79.   begin
  80.   MutexHandle       := CreateMutex(nil, False, UniqueAppString);
  81.   WProc             := TFNWndProc(SetWindowLong(Application.Handle,
  82.     gwl_WndProc, LongInt(@NewWndProc)));
  83.   Application.Initialize;
  84.   Application.Title := 'AutoRun Version 1.0';
  85.   CDDrive           := ExtractFileDrive(ParamStr(0));
  86.   IniFileName       := ExtractFilePath(ParamStr(0)) +
  87.     ExtractFileName(ParamStr(0));
  88.   S                 := ExtractFileExt(ParamStr(0));
  89.   SetLength(IniFileName, Length(IniFileName) - Length(S));
  90.   IniFileName       := IniFileName + '.ini';
  91.   FillInfo(CDVName, CDVSerial);
  92.   If FileSearch(IniFileName, '') = '' then
  93.     begin
  94.     Application.CreateForm(THelpForm1, HelpForm1);
  95.     Application.CreateForm(TAboutForm1, AboutForm1);
  96.     end
  97.   else
  98.     begin
  99.     Application.CreateForm(TMainForm1, MainForm1);
  100.     Application.CreateForm(TAboutForm1, AboutForm1);
  101.     Application.CreateForm(THelpForm1, HelpForm1);
  102.     Application.CreateForm(TShortCutForm1, ShortCutForm1);
  103.     Application.CreateForm(TErrorForm1, ErrorForm1);
  104.     Application.CreateForm(TEventForm1, EventForm1);
  105.     end;
  106.   Application.Run;
  107.   end
  108. else
  109.   begin
  110.   BSMRecipients := bsm_Applications;
  111.   BroadCastSystemMessage(bsf_IgnoreCurrentTask or bsf_PostMessage,
  112.     @BSMRecipients, MessageID, 0, 0);
  113.   end;
  114. end.
  115.  
  116.