{Ursprünglich programmiert von BeniBela (Benito van der Zander, BeniBela@aol.com)
 Teilweise verbessert von Assarbad (Oliver , Assarbad@gmx.net)

 Diese Datei steht unter der Mozilla Public  License (http://www.mozilla.org/MPL/MPL-1.1.html)

Exhibit A - Mozilla Public License.

"The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.

The Original Code is Virus2.pas and VirusNV.pas.

The Initial Developer of the Original Code is Benito van der Zander.
All Rights Reserved.

Alternatively, the contents of this file may be used under the terms
of the Gnu Public License Version 2 or higher license (the  "GPL 2 License"), in which case the
provisions of GPL 2 License are applicable instead of those
above. If you wish to allow use of your version of this file only
under the terms of the GPL 2 License and not to allow others to use
your version of this file under the MPL, indicate your decision by
deleting the provisions above and replace them with the notice and
other provisions required by the GPL 2 License. If you do not delete
the provisions above, a recipient may use your version of this file
under either the MPL or the GPL 2 License."}

unit VirusNV;

interface
uses
  Windows, nvScreenCanvas,nvHashClass,mmsystem;

type
  TNVNotifyEvent=procedure of object;
  TNVirus = class
  private
    { Private-Deklarationen}
  protected
    { Protected-Deklarationen}
    Handle:HWND;

    F_Canvas: TNVScreenCanvas;
    Hidden:Boolean; // ist der zugehörige Prozeß versteckt?

    //Timerfelder
    Timer: UINT;
    MousePos: TNVNotifyEvent;
    x, y: integer;
    TimerInterval: cardinal;

    //Timermethoden
    procedure setTimerInterval(value: cardinal);

{    //Windowskommunikation
    function RegisterServiceProcess(const dwProcessId, dwType: Dword): DWord;}
  public
    { Public-Deklarationen}
    procedure SetKeyBoardLEDs(const numlock, caps, scroll: Boolean);

    //Windowskommunikation
    function GetShellWindow:HWND;
    function GetStartButton:HWND;
    function GetTNA:HWND;
    function GetTNAClock:HWND;
    function GetReBar:HWND;
    function GetTaskList:HWND;
    function GetIconDesktop:HWND;

    procedure openCDTray;
    procedure closeCDTray;

    constructor Create;
    destructor Destroy(); override;
    procedure Hide(handle: THandle);
    procedure HideFromTaskManager;
    procedure ShowToTaskManager;

    property Canvas: TNVScreenCanvas read F_Canvas;
    property IsHidden:Boolean read Hidden;
    property ShellWindow:HWND read GetShellWindow;
    property StartButton:HWND read GetStartButton;
    property TNAHandle:HWND read GetTNA;
    property TNAClock:HWND read GetTNAClock;
    property ReBar:HWND read GetReBar;
    property TaskList:HWND read GetTaskList;
    property IconDesktop:HWND read GetIconDesktop;
  published
    {Published-Deklarationen }
    property XMouseTest: integer read x write x default 0;
    property YMouseTest: integer read y write y default 0;
    property MouseTestIntervall: cardinal read TimerInterval write setTimerInterval default 0;
    property OnMousePos: TNVNotifyEvent read MousePos write MousePos default nil;
  end;

const
  RSPSIMPLESERVICE = 1;
  RSPUNREGISTERSERVICE = 0;

function RegisterServiceProcess(const dwProcessId, dwType: DWORD): DWORD;
implementation
type
  TFNRegisterServiceProcess = function(dwProcessId, dwType: Dword): DWord; stdcall;
var
  _RegisterServiceProcess: TFNRegisterServiceProcess = nil;
  lib: THandle = 0;
  VirenTimerList:TNVHashClass;

function RegisterServiceProcess(const dwProcessId, dwType: DWORD): DWORD;
begin
  if Assigned(@_RegisterServiceProcess) then
    result := _RegisterServiceProcess(dwProcessId, dwType)
  else
    result := 0;
end;



(******************************************************************************
  START TNVirus
 ******************************************************************************)
procedure OnTimerEvent(wnd:HWND; uMsg:UINT; idEvent:UINT;dwTime:DWORD);stdcall;
var Pos: TPoint;
    self:TNVirus;
    hash:PNVHashRec;
begin
  hash:=VirenTimerList.GetHashRec(integer(idEvent));
  if hash<>nil then begin
    self:=TNVirus(hash.wert);
    GetCursorPos(Pos);
    if (Pos.x = self.x) and (Pos.y = self.y)and (assigned(self.MousePos)) then
      self.OnMousePos();
  end;
end;

constructor TNVirus.Create;
begin
  F_Canvas := TNVScreenCanvas.Create;

  TimerInterval:=0;
  Timer:=0;
  Hidden:=false;
end;


procedure TNVirus.HideFromTaskManager;
begin
  if not Hidden then
    Hidden:=RegisterServiceProcess(GetCurrentprocessID, RSPSIMPLESERVICE)=1;
end;

procedure TNVirus.ShowToTaskManager;
begin
  if Hidden then
    Hidden:=RegisterServiceProcess(GetCurrentprocessID, RSPUNREGISTERSERVICE)=0;
end;

procedure TNVirus.Hide(handle: THandle);
begin
  ShowWindow(GetWindow(handle, GW_OWNER), SW_HIDE);
  ShowWindow(handle, SW_HIDE);
end;

procedure TNVirus.SetKeyBoardLEDs(const numlock, caps, scroll: Boolean);
var
  KeyBoardState: TKeyboardState;
begin
  ZeroMemory(@KeyBoardState, sizeof(KeyBoardState));
  KeyBoardState[VK_NUMLOCK] := ord(numlock);
  KeyBoardState[VK_CAPITAL] := ord(caps);
  KeyBoardState[VK_SCROLL] := ord(scroll);
  SetKeyboardState(KeyBoardState);
end;

function TNVirus.GetShellWindow:HWND;
begin
  result := FindWindow('Shell_TrayWnd', nil);
end;
function TNVirus.GetStartButton:HWND;
begin
  Result:=FindWindowEx( GetShellWindow,0,'Button',nil);
end;
function TNVirus.GetReBar:HWND;
begin
  Result:=FindWindowEx( GetShellWindow,0,'ReBarWindow32',nil);
end;
function TNVirus.GetTaskList:HWND;
begin
  Result:=FindWindowEx( GetReBar,0,'MSTaskSwWClass',nil);
end;
function TNVirus.GetTNA:HWND;
begin
  Result:=FindWindowEx( GetShellWindow,0,'TrayNotifyWnd',nil);
end;
function TNVirus.GetTNAClock:HWND;
begin
  Result:=FindWindowEx( GetTNA,0,'TrayClockWCLass',nil);
end;
function TNVirus.GetIconDesktop:HWND;
begin
  Result:=FindWindowEx(FindWindowEx(findWindow('Progman',nil),0,'SHELLDLL_DefView',nil),0,'SysListView32',nil);
end;

procedure TNVirus.openCDTray;
begin
   mciSendString('Set cdaudio door open wait', nil, 0, 0);
end;
procedure TNVirus.closeCDTray;
begin
   mciSendString('Set cdaudio door closed wait', nil, 0, 0);
end;


procedure TNVirus.setTimerInterval(value: cardinal);
begin
  if TimerInterval <> 0 then begin
    VirenTimerList.DeleteHashRec(timer);
    KillTimer(0,Timer);
    timer:=0;
  end;
  if value <> 0 then begin
    timer:=setTimer(0,0,value,@OnTimerEvent);
    VirenTimerList.InsertHashRec(timer,integer(self));
  end;
  TimerInterval:=value;
end;

destructor TNVirus.Destroy();
begin
  MouseTestIntervall:=0; //Timer freigeben
  F_Canvas.free;
  inherited Destroy;
end;

(******************************************************************************
  END TNVirus
 ******************************************************************************)

initialization
  lib := LoadLibrary(kernel32);
  if lib <> 0 then
    @_RegisterServiceProcess := GetProcAddress(lib, 'RegisterServiceProcess');
  VirenTimerList:=TNVHashClass.create;
finalization
  VirenTimerList.free;
end.
