Register when a program is runned - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Register when a program is runned

QuesadaRQuesadaR Posts: 3Member
I need to register when internet explorer is loaded and when it is closed in a .txt file from Visual basic.

Comments

  • PavlinIIPavlinII Posts: 404Member
    : I need to register when internet explorer is loaded and when it is closed in a .txt file from Visual basic.
    :
    Hi, there exist at least two ways..
    1)Your app will catch every executed exe file and if it recognized IE (or anything), will make log entry:
    Add your app to registry:
    HKEY_CLASSES_ROOTexefileShellOpenCommand
    There exist value [blue]"%1" %*[/blue] and you have to replace it with [blue]"C:myPathmyApp.exe" "%1" %*[/blue]
    But you should more replace subkeys lnkfile; (comfile;) batfile and piffile if enyone run it from these files..
    And in your App will be enoug module with one Main() sub and storing sub
    [code]Public Sub Main()
    Dim iFile As Integer
    [green]'Command gives you arguments - represented by "%1" %* in reg
    'If executed program is IE (or any)[/gray]
    If Instr(Command, "Internet ExplorerIExplore.exe")=0 Then
    iFile = FreeFile()
    Open "C:myPathIElog.txt" For Append as #iFile
    Print #iFile, CStr(Now) & vbTab & Command & vbTab & "IE Started"
    Close #iFile
    Shell Command, vbNormalFocus 'Start app
    WaitForIEClose 'I'll describe this in part 2
    Else
    Shell Command, vbNormalFocus 'Start app
    End 'I do not care about other apps
    End If[/code]
    You should prevent running your app several times (on severals IE windows) by finding your app in task - I'll show in part2
    2) Your app will strat at windows startup and run still... and run and don't do anything till IE will be found...
    [code]Private Const ieNAME = "IdoNOTuseIT-iDOnotKNOW"

    Public Sub Main()
    Dim bRunning As Boolean
    Do
    If FindWindow(vbNullString, ieNAME) <> 0 Then
    'IE is running
    If bRunning = False Then
    bRunning = True
    Open "C:myPathIElog.txt" For Append as #iFile
    Print #iFile, CStr(Now) & vbTab & "IE Started"
    Close #iFile
    End If
    Else
    If bRunning = True Then
    bRunning = False
    Open "C:myPathIElog.txt" For Append as #iFile
    Print #iFile, CStr(Now) & vbTab & "IE Closed"
    Close #iFile
    End If
    End If
    DoEvents
    Loop
    End Sub[/code]
    In this case, you'll not know about more running windows of IE but FindWindow return hwnd of windows and if you make list of them... you will.

    Hope this will help you

    PavlinII

  • QuesadaRQuesadaR Posts: 3Member
    : : I need to register when internet explorer is loaded and when it is closed in a .txt file from Visual basic.
    : :
    : Hi, there exist at least two ways..
    : 1)Your app will catch every executed exe file and if it recognized IE (or anything), will make log entry:
    : Add your app to registry:
    : HKEY_CLASSES_ROOTexefileShellOpenCommand
    : There exist value [blue]"%1" %*[/blue] and you have to replace it with [blue]"C:myPathmyApp.exe" "%1" %*[/blue]
    : But you should more replace subkeys lnkfile; (comfile;) batfile and piffile if enyone run it from these files..
    : And in your App will be enoug module with one Main() sub and storing sub
    : [code]Public Sub Main()
    : Dim iFile As Integer
    : [green]'Command gives you arguments - represented by "%1" %* in reg
    : 'If executed program is IE (or any)[/gray]
    : If Instr(Command, "Internet ExplorerIExplore.exe")=0 Then
    : iFile = FreeFile()
    : Open "C:myPathIElog.txt" For Append as #iFile
    : Print #iFile, CStr(Now) & vbTab & Command & vbTab & "IE Started"
    : Close #iFile
    : Shell Command, vbNormalFocus 'Start app
    : WaitForIEClose 'I'll describe this in part 2
    : Else
    : Shell Command, vbNormalFocus 'Start app
    : End 'I do not care about other apps
    : End If[/code]
    : You should prevent running your app several times (on severals IE windows) by finding your app in task - I'll show in part2
    : 2) Your app will strat at windows startup and run still... and run and don't do anything till IE will be found...
    : [code]Private Const ieNAME = "IdoNOTuseIT-iDOnotKNOW"
    :
    : Public Sub Main()
    : Dim bRunning As Boolean
    : Do
    : If FindWindow(vbNullString, ieNAME) <> 0 Then
    : 'IE is running
    : If bRunning = False Then
    : bRunning = True
    : Open "C:myPathIElog.txt" For Append as #iFile
    : Print #iFile, CStr(Now) & vbTab & "IE Started"
    : Close #iFile
    : End If
    : Else
    : If bRunning = True Then
    : bRunning = False
    : Open "C:myPathIElog.txt" For Append as #iFile
    : Print #iFile, CStr(Now) & vbTab & "IE Closed"
    : Close #iFile
    : End If
    : End If
    : DoEvents
    : Loop
    : End Sub[/code]
    : In this case, you'll not know about more running windows of IE but FindWindow return hwnd of windows and if you make list of them... you will.
    :
    : Hope this will help you
    :
    : PavlinII
    :
    :
    Thanks a bunch for your help. I have used your code but i'm having several problems. First of all you wrote 2 Mains sub or 2nd one is other sub???.

    ==>> 1st Main sub doubts
    On the first one on line [ If InStr(Command, .... ] command is returning nothing, due to this [ Shell Command ... ] makes fall the system.
    Regarding [ WaitForIEClose... ] there is nothing on 2nd part.

    ==>> 2nd Main sub doubts
    Is ieName the variable for the program to search for???
    FindWindow is a function you wrote? because It doesn't work for me


    Thanks again for all your help... I'll be waiting your comments.

  • PavlinIIPavlinII Posts: 404Member
    : : : I need to register when internet explorer is loaded and when it is closed in a .txt file from Visual basic.
    : : :
    : : Hi, there exist at least two ways..
    : : 1)Your app will catch every executed exe file and if it recognized IE (or anything), will make log entry:
    : : Add your app to registry:
    : : HKEY_CLASSES_ROOTexefileShellOpenCommand
    : : There exist value [blue]"%1" %*[/blue] and you have to replace it with [blue]"C:myPathmyApp.exe" "%1" %*[/blue]
    : : But you should more replace subkeys lnkfile; (comfile;) batfile and piffile if enyone run it from these files..
    : : And in your App will be enoug module with one Main() sub and storing sub
    : : [code]Public Sub Main()
    : : Dim iFile As Integer
    : : [green]'Command gives you arguments - represented by "%1" %* in reg
    : : 'If executed program is IE (or any)[/gray]
    : : If Instr(Command, "Internet ExplorerIExplore.exe")=0 Then
    : : iFile = FreeFile()
    : : Open "C:myPathIElog.txt" For Append as #iFile
    : : Print #iFile, CStr(Now) & vbTab & Command & vbTab & "IE Started"
    : : Close #iFile
    : : Shell Command, vbNormalFocus 'Start app
    : : WaitForIEClose 'I'll describe this in part 2
    : : Else
    : : Shell Command, vbNormalFocus 'Start app
    : : End 'I do not care about other apps
    : : End If[/code]
    : : You should prevent running your app several times (on severals IE windows) by finding your app in task - I'll show in part2
    : : 2) Your app will strat at windows startup and run still... and run and don't do anything till IE will be found...
    : : [code]Private Const ieNAME = "IdoNOTuseIT-iDOnotKNOW"
    : :
    : : Public Sub Main()
    : : Dim bRunning As Boolean
    : : Do
    : : If FindWindow(vbNullString, ieNAME) <> 0 Then
    : : 'IE is running
    : : If bRunning = False Then
    : : bRunning = True
    : : Open "C:myPathIElog.txt" For Append as #iFile
    : : Print #iFile, CStr(Now) & vbTab & "IE Started"
    : : Close #iFile
    : : End If
    : : Else
    : : If bRunning = True Then
    : : bRunning = False
    : : Open "C:myPathIElog.txt" For Append as #iFile
    : : Print #iFile, CStr(Now) & vbTab & "IE Closed"
    : : Close #iFile
    : : End If
    : : End If
    : : DoEvents
    : : Loop
    : : End Sub[/code]
    : : In this case, you'll not know about more running windows of IE but FindWindow return hwnd of windows and if you make list of them... you will.
    : :
    : : Hope this will help you
    : :
    : : PavlinII
    : :
    : :
    : Thanks a bunch for your help. I have used your code but i'm having several problems. First of all you wrote 2 Mains sub or 2nd one is other sub???.
    :
    : ==>> 1st Main sub doubts
    : On the first one on line [ If InStr(Command, .... ] command is returning nothing, due to this [ Shell Command ... ] makes fall the system.
    : Regarding [ WaitForIEClose... ] there is nothing on 2nd part.
    :
    : ==>> 2nd Main sub doubts
    : Is ieName the variable for the program to search for???
    : FindWindow is a function you wrote? because It doesn't work for me
    :
    :
    : Thanks again for all your help... I'll be waiting your comments.
    :
    :
    OK, step by step...
    Yes, I wrote 2 Main... 2 different each other independent (two projects).. It were 2 different ways how to get simillar result..

    If InStr(Command, "Internet ExplorerIExplore.exe")[blue]<>0[/blue] Then
    (Sorry, my mistake - I exchanged returns from InStr and StrComp in my mind :-) )

    Shell: Inside variable Command are given parameters. (%1, %* passes parameters to your app and you can find them in Command)... Make sure, that there is correct information... Try to run "myApp.exe pArAm1 param2 -testParam3" and look if [code]MsgBox Command[/code]in the beginning of your app will show you "pArAm1 param2 -testParam3"... It should do so... And then try to run any app (ie) and check if you'll see something like "C:Program Filesieiexplorer.exe".. If you'll not, there is mistake somewhere in registry...

    Now about WaitForIEClose:
    It's nearly the same like second Main... Just let out some unneccesary lines... (I described the way how to manage it.. :-) never mind)
    [code]Private Const ieNAME = "IdoNOTuseIT-iDOnotKNOW"
    Public Sub WaitForIEClose()
    Do
    If [blue]Not[/blue] FindWindow(vbNullString, ieNAME) <> 0 Then
    [green]'You can omit this
    ' 'IE is running
    ' If bRunning = False Then
    ' bRunning = True
    ' Open "C:myPathIElog.txt" For Append as #iFile
    ' Print #iFile, CStr(Now) & vbTab & "IE Started"
    ' Close #iFile
    ' End If
    ' Else
    ' If bRunning = True Then
    ' bRunning = False[/green]
    Open "C:myPathIElog.txt" For Append as #iFile
    Print #iFile, CStr(Now) & vbTab & "IE Closed"
    Close #iFile
    [blue]Exit Do[/blue] [green]'You don't need to wait anymore[/green]
    End If
    End If
    DoEvents [green]'Let the system do what it needs[/green]
    Loop
    End Sub[/code]

    Yes, you're finding window by title.. And here it's ieName... My Windows calculator has Kalkulačka... I don't know what has ie...

    FindWindow is a standard API function...
    Check Menu/Add-Ins/Add-In Manager.../VB 6 API Viewer (and Load On StartUp)
    Run Menu/Add-Ins/ApiViewer and there are declarations of ALL API function, constants and datatypes.. (I don't remeber if that list is loaded on first run but I suggest you to do this: File/Load text file... and choose ..MS Visual StudioCommonToolsWinApi32.txt... and then File/Convert Text to database... (It'll start much faster)... And at last check View/Load Last File)
    If there exists any reason why thiw will not work to you, write this somewhere at the beginning of module...
    [code]Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long[/code]

    If you have more questions, ask..

    PavlinII
  • QuesadaRQuesadaR Posts: 3Member
    Thanks a lot !!!

    I adjust your code and it is doing exactly what i was trying.

    Thanks again.

    Roy Q>
Sign In or Register to comment.