Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 997931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:06:50+00:00 2026-05-16T07:06:50+00:00

I need to recognize and fire an event when a file is going to

  • 0

I need to recognize and fire an event when a file is going to be executed or run by an application. I know I can do it by hooking windows procedures, but I don’t know what procedure or event of windows fires.
For example, when an autorun file going to execute, my application should recognize it, Like an antivirus application.

I’m not sure that hooking is useful for my purpose, if solution isn’t hooking, please give me a true solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:06:51+00:00Added an answer on May 16, 2026 at 7:06 am

    try using the PsSetCreateProcessNotifyRoutine, this function adds a driver-supplied callback routine to, or removes it from, a list of routines to be called whenever a process is created or deleted.

    you can find a very nice sample int this link written in c++

    Detecting Windows NT/2K process execution

    UPDATE

    Another option is use the WMI events, check the Win32_Process class, the ExecNotificationQuery method and the SWbemEventSource.NextEvent function.

    Check this sample tested in delphi 7 and Windows 7, you must run this application from outside of the Delphi IDE or disable the exception notification for the EOleException exception (check this link), to avoid the EOleException wich is intercepted by the IDE.

    program GetWMI_InstanceCreationEvent;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils
      ,Windows
      ,ComObj
      ,ActiveX
      ,Variants;
    
    
    Function KeyPressed:boolean; //detect if an key is pressed
    var
    NumEvents   : DWORD;
    ir          : _INPUT_RECORD;
    bufcount    : DWORD;
    StdIn       : THandle;
    begin
    Result:=false;
    StdIn := GetStdHandle(STD_INPUT_HANDLE);
    NumEvents:=0;
    GetNumberOfConsoleInputEvents(StdIn,NumEvents);
        if NumEvents<> 0 then
        begin
            PeekConsoleInput(StdIn,ir,1,bufcount);
            if bufcount <> 0 then
            begin
                if ir.EventType = KEY_EVENT then
                begin
                  if ir.Event.KeyEvent.bKeyDown then
                  result:=true
                  else
                  FlushConsoleInputBuffer(StdIn);
                end
                else
                FlushConsoleInputBuffer(StdIn);
            end;
        end;
    end;
    
    
    function VarStrNUll(VarStr:OleVariant):string;//dummy function to handle null variants
    begin
      Result:='';
      if not VarIsNull(VarStr) then
      Result:=VarToStr(VarStr);
    end;
    
    function GetWMIObject(const objectName: String): IDispatch; //create a wmi object instance
    var
      chEaten: Integer;
      BindCtx: IBindCtx;
      Moniker: IMoniker;
    begin
      OleCheck(CreateBindCtx(0, bindCtx));
      OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
      OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
    end;
    
    Procedure  GetWin32_InstanceCreationEvent;
    var
      objWMIService              : OLEVariant;
      colMonitoredProcesses      : OLEVariant;
      objLatestProcess           : OLEVariant;
    begin
      objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
      colMonitoredProcesses      := objWMIService.ExecNotificationQuery('Select * From __InstanceCreationEvent Within 1 Where TargetInstance ISA ''Win32_Process'''); //Get the event listener
      while not KeyPressed do
      begin
        try
         objLatestProcess := colMonitoredProcesses.NextEvent(100);//set the max time to wait (ms)
        except
         on E:EOleException do
         if EOleException(E).ErrorCode=HRESULT($80043001) then //Check for the timeout error wbemErrTimedOut 0x80043001
         objLatestProcess:=Null
         else
         raise;
        end;
    
        if not VarIsNull(objLatestProcess) then
        begin
          Writeln('Process Started '+VarStrNUll(objLatestProcess.TargetInstance.Name));
          Writeln('CommandLine     '+VarStrNUll(objLatestProcess.TargetInstance.CommandLine));
          Writeln('PID             '+VarStrNUll(objLatestProcess.TargetInstance.ProcessID));
        end;
      end;
    end;
    
    
    
    begin
     try    
        CoInitialize(nil);
        try
          Writeln('Press Any key to exit');
          GetWin32_InstanceCreationEvent;
        finally
        CoUninitialize;
        end;
    
     except
        on E:Exception do
        Begin
            Writeln(E.Classname, ': ', E.Message);
            Readln;
        End;
      end;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.