forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How to know with program started your program.

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
Bart
Valued Newbie


Joined: 24 Jun 2003
Posts: 41
Location: Netherlands

PostPosted: Fri Mar 30, 2007 10:58 am    Post subject: How to know with program started your program. Reply with quote

Is there a way to know what program started your program.

Let me explain, i have a executable that is run by different programs.

Prog_1.exe , Prog_2.exe and Prog_3.exe, they all runs Prog_me.exe from time to time.

Prog 1 to 3 are not written by me and have no option of sending commandline data, all i need to know is what program started my program.
Back to top
View user's profile Send private message
vdsalchemist
Admin Team


Joined: 23 Oct 2001
Posts: 1448
Location: Florida, USA

PostPosted: Fri Mar 30, 2007 4:51 pm    Post subject: Reply with quote

Bart,
Ok I have updated the code and converted it into a Function called @GetParentProcess(). Please read the code before use. Also this is not compatible with Windows NT 3 & 4 as the API functions are not available.

Code:

#-----------------------------------------------------------------------------#
#                                                                             #
# A function that demostrates how to get the parent name or parent process ID #
# of either the current process or another process running                    #
#                                                                             #
# Author:  Johnny Kinsey                                                      #
#                                                                             #
# Copyright: Copyright 2007 DragonSphere Software                           #
#                                                                             #
#-----------------------------------------------------------------------------#
#DEFINE FUNCTION,GETPARENTPROCESS
%A = @GetParentProcess(,NI)
If %A
  Parse "%%ParentFile;%%ParentID",%A
  Info The parent for the current process is %%ParentFile.@CR()It's Process ID is %%ParentID
End
Exit

:GetParentProcess
# The GetParentProcess will return the parent process name and/or ID
#   Parameters:
#      Process ID: This is an optional parameter if not specified the current process ID is used
#      return options: This is an optional paramter if not specified the name of the parent process is returned
#                      If more than 1 option is specified the current field seperator is used to seperator
#                      the return values.
#                      The return options are:
#                       N = name of the parent process for the given or current process ID
#                       I = the process ID of the parent process for the given or current process ID
#                       
# Syntax: %A = @GetParentProcess(<Process ID>,<return options>)
  %R =
  # Load the kernel32.dll
  LoadLib kernel32.dll
  # Create a list to hold all the current running processes
  List Create,9
  # Constants for the CreateToolhelp32Snapshot API function
  %%TH32CS_INHERIT = $80000000
  %%TH32CS_SNAPHEAPLIST = $00000001
  %%TH32CS_SNAPMODULE = $00000008
  %%TH32CS_SNAPMODULE32 = $00000010
  %%TH32CS_SNAPPROCESS = $00000002
  %%TH32CS_SNAPTHREAD = $00000004
  %%TH32CS_SNAPALL = @SUM(%%TH32CS_SNAPHEAPLIST,%%TH32CS_SNAPMODULE,%%TH32CS_SNAPPROCESS,%%TH32CS_SNAPTHREAD)
 
  # Test for the first parameter if NULL use the current Process ID
  If %1
    %%ProcID = %1
  Else
    %%ProcID = @lib(kernel32,GetCurrentProcessId,INT:)
  End
 
  # Get a snapshot of all the running processes
  %%SnapShotHandle = @lib(kernel32,CreateToolhelp32Snapshot,INT:,DWORD:%%TH32CS_SNAPALL,DWORD:0)
  If %%SnapShotHandle
    #typedef struct tagPROCESSENTRY32 {  DWORD dwSize;
    #  DWORD cntUsage;
    #  DWORD th32ProcessID;
    #  ULONG_PTR th32DefaultHeapID;
    #  DWORD th32ModuleID;
    #  DWORD cntThreads;
    #  DWORD th32ParentProcessID;
    #  LONG pcPriClassBase;
    #  DWORD dwFlags; 
    #  TCHAR szExeFile[MAX_PATH];
    #} PROCESSENTRY32,  *PPROCESSENTRY32;

    # Create the PROCESSENTRY32 structure above and assign the size of the structure
    # to the first dword variable while filling the rest with @chr(0)
    %Z = @BINARY(DWORD,548)@Fill(544,,Z)
    # Get the first running process
    %B = @lib(kernel32,Process32First,BOOL:,DWORD:%%SnapShotHandle,DWORD:@ADDR("%Z"))
    If %B
      # The loop below walks the rest of the process tree getting each processes information
      Repeat
        If %B
          # Parse the PROCESSENTRY32 structure returned
          %%dwSize = @Val(@SubStr(%Z,1,4))
          %%cntUsage = @Val(@SubStr(%Z,5,8))
          %%th32ProcessID = @Val(@SubStr(%Z,9,12))
          %%th32DefaultHeapID = @Val(@SubStr(%Z,13,16))
          %%th32ModuleID = @Val(@SubStr(%Z,17,20))
          %%cntThreads = @Val(@SubStr(%Z,21,24))
          %%th32ParentProcessID = @Val(@SubStr(%Z,25,28))
          %%pcPriClassBase = @Val(@SubStr(%Z,29,32))
          %%dwFlags = @Val(@SubStr(%Z,33,36))
          %%szExeFile = @SubStr(%Z,37,@diff(%%dwSize,36))
          # The next line gets rid of the extra Null characters @chr(0).         
          %%szExeFile = @SubStr(%%szExeFile,1,@pred(@Pos(@chr(0),%%szExeFile)))
          # Add the process and exe file name to list 9
          List Add,9,%%th32ProcessID@fsep()%%szExeFile
        End
        # Reinitialize the PROCESSENTRY32 structure
        %Z = @BINARY(DWORD,548)@Fill(544,,Z)
        # Get the next Process
        %B = @lib(kernel32,Process32Next,BOOL:,DWORD:%%SnapShotHandle,DWORD:@ADDR("%Z"))
      Until @Equal(%%th32ProcessID,%%ProcID)@Null(%B)
    End
   
    If @Equal(%%th32ProcessID,%%ProcID)
      # Look up the Parent process in List 9
      List Seek,9,0
      %U = @Match(9,%%th32ParentProcessID@fsep())
      Parse "%%ParentProcessID;%%ParentExeFile",@Item(9)
      If %2
        # Process the second parameter placing the return values according to the order that the user gave.
        %P = %2
        %%cnt = 1
        %%count = @len(%P)
        Repeat
          %C = @Trim(@SubStr(%P,%%cnt,%%cnt))
          If @Equal(I,%C)
            If %R
              %R = %R@fsep()%%th32ParentProcessID
            Else
              %R = %%th32ParentProcessID
            End
          End
          If @Equal(N,%C)
            If %R
            %R = %R@fsep()%%ParentExeFile
            Else
              %R = %%ParentExeFile
            End
          End
          %%cnt = @succ(%%cnt)
        Until @Greater(%%cnt,%%count)
      Else
        # No second parameter so just return the parent module ID
        %R = %%ParentExeFile
      End
    End
    # Close the SnapShot Handle
    %B = @lib(kernel32,CloseHandle,BOOL:,DWORD:%%SnapShotHandle)
  End
  # Clean up everything else.
  List clear,9
  List close,9
  FreeLib kernel32
  # We are done.
Exit %R


Enjoy...

_________________
Home of

Give VDS a new purpose!
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Bart
Valued Newbie


Joined: 24 Jun 2003
Posts: 41
Location: Netherlands

PostPosted: Mon Apr 02, 2007 11:29 am    Post subject: Reply with quote

That does the job, thanks
Back to top
View user's profile Send private message
vdsalchemist
Admin Team


Joined: 23 Oct 2001
Posts: 1448
Location: Florida, USA

PostPosted: Mon Apr 02, 2007 12:47 pm    Post subject: Reply with quote

Bart,
After messing with this for a little bit I have to post a warning. If the parent process does not wait for the child process to finish then the child process ID will get assigned to the system. The system will become the parent process.

_________________
Home of

Give VDS a new purpose!
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Bart
Valued Newbie


Joined: 24 Jun 2003
Posts: 41
Location: Netherlands

PostPosted: Mon Apr 02, 2007 1:08 pm    Post subject: Reply with quote

I discovered that when testing it with the run command and not using ,wait

But the programs that runs my program are waiting for output from my program so it works fine.
Back to top
View user's profile Send private message
marty
Professional Member
Professional Member


Joined: 10 May 2001
Posts: 789

PostPosted: Thu Jun 09, 2011 5:51 pm    Post subject: Reply with quote

I like this function Smile

Great if it is a EXE starting the VDS EXE, but in my case I am starting the VDS EXE within a .bat file, and would like to get the name of the BAT file starting the VDS EXE

With the above function I always get CMD.EXE

You think its possible to get the name?

Thanks

Martin Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Mon Jul 04, 2011 7:52 pm    Post subject: Reply with quote

@marty:

Within your *.bat just append the name of the batch-file to the calling command and within your exe read variable %1.


Bach-file: your.exe test.bat

your.exe: info %1

Hope this helps.

bornSoft
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum




Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group