Page 1 of 3 123 LastLast
Results 1 to 40 of 98

Thread: Check if Application is Running Without Timer

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Check if Application is Running Without Timer

    How do I check if an application is running without calling the timer every 10 seconds to check current processes. Can anyone offer a solution?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check if Application is Running Without Timer

    There is no other way, because in order to check it has to have orders to check something..Timer is the only way buddy (unless you only want to fire the thing once..)

    ---
    Actually..you could use a do loop and add some doevents inside and such..but it will use 50% computer usage

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Check if Application is Running Without Timer

    Lol, nah, I'm trying to avoid CPU time, I guess I'll stick to the timer. I guess that makes sense though. Thanks Remix.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check if Application is Running Without Timer

    yeah timers are very low cpu..stick with that

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Check if Application is Running Without Timer

    If it's your app, then it could write a file, kind of like a flag, and then delete the file when the app closes. If it's another program, maybe you could use a batch file that does the same thing.

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    Or a WH_SHELL hook might tell you, but then this is a VB forum isn't it

  7. #7
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Check if Application is Running Without Timer


  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by moeur
    Or a WH_SHELL hook might tell you, but then this is a VB forum isn't it
    Your point?

  9. #9
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by Jacob Roman
    what would you use instead?

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by Jacob Roman
    You said that timers are inconsistent, slow, horrible, but you haven't said why. And also I hardly think that any of those reasons matter in a situation like this. But if it is possible to do it through a hook then I would not touch any form of loop or timer.

  11. #11
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by dglienna
    then it could write a file
    You'd still need to check for the presence of that file.

  12. #12
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    Your point?
    Sometimes I wonder myself what my point is.

    By installing a system-wide WHSHELL hook you can get HSHELL_WINDOWCREATED notifications whenever a new window is created. Unfortunately this has to be done in C.

    Oh wait! there is that fancy method of yours that allows us to create win32 dlls in VB. Why don't you whip up a little demo for this monkey?

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by moeur
    Oh wait! there is that fancy method of yours that allows us to create win32 dlls in VB. Why don't you whip up a little demo for this monkey?
    OK, I will, but to run it, you will need to mess with your VB files a little, and to make it, I will need to know how on earth to install a WHSHELL hook, and what on earth to do with it I will search for information but since I have no idea how one works I can't promise anything

  14. #14
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    I'll give you the code, then you compile it into a win32 dll.

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    OK, here is the code.
    Make sure that the ShellProce function is in a standard module and that
    you export the other two subs.
    If this works it will open a world of programming, good luck.
    VB Code:
    1. Option Explicit
    2. 'This code is meant to be compiled into a win32 dll
    3. Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" ( _
    4.     ByVal idHook As Long, _
    5.     ByVal lpfn As Long, _
    6.     ByVal hmod As Long, _
    7.     ByVal dwThreadId As Long _
    8. ) As Long
    9.  
    10. Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
    11.     ByVal hHook As Long _
    12. ) As Long
    13.  
    14. Private Declare Function CallNextHookEx Lib "user32" ( _
    15.     ByVal hHook As Long, _
    16.     ByVal ncode As Long, _
    17.     ByVal wParam As Long, _
    18.     lParam As Any _
    19. ) As Long
    20.  
    21. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    22.     Destination As Any, _
    23.     Source As Any, _
    24.     ByVal Length As Long _
    25. )
    26.                                                            
    27. Public Const WH_SHELL = 10
    28. Public Const HSHELL_WINDOWCREATED = 1
    29. Public Const HSHELL_WINDOWDESTROYED = 2
    30. Public Const HSHELL_ACTIVATESHELLWINDOW = 3
    31. Public Const HSHELL_APPCOMMAND = 12   '???
    32.  
    33. ' #if(WINVER >= 0x0400) NT 4.0 and above
    34. Public Const HSHELL_WINDOWACTIVATED = 4
    35. Public Const HSHELL_GETMINRECT = 5
    36. Public Const HSHELL_REDRAW = 6
    37. Public Const HSHELL_TASKMAN = 7
    38. Public Const HSHELL_LANGUAGE = 8
    39.  
    40. ' #if(_WIN32_WINNT >= 0x0500)
    41. Public Const HSHELL_ACCESSIBILITYSTATE = 11
    42. Public Const ACCESS_STICKYKEYS = &H1
    43. Public Const ACCESS_FILTERKEYS = &H2
    44. Public Const ACCESS_MOUSEKEYS = &H3
    45.                                                            
    46. Public Type RECT
    47.         Left As Long
    48.         Top As Long
    49.         Right As Long
    50.         Bottom As Long
    51. End Type
    52.  
    53.  
    54. Private hHook As Long
    55.  
    56. Public IsHooked As Boolean
    57.  
    58. Public Sub SetShellHook()
    59.     If IsHooked Then
    60.         MsgBox "Don't hook SHELL twice or you will be unable to unhook it."
    61.     Else
    62.         'the following line installs a system-wide hook
    63.         hHook = SetWindowsHookEx(WH_SHELL, AddressOf ShellProc, App.hInstance, 0)
    64.         IsHooked = True
    65.     End If
    66. End Sub
    67.  
    68. Public Sub RemoveShellHook()
    69.     Dim temp As Long
    70.     temp = UnhookWindowsHookEx(hHook)
    71.     IsHooked = False
    72. End Sub
    73.  
    74. '*** This routine must be placed in a standard module so we can use the AddressOf operator ***
    75. Public Function ShellProc(ByVal uCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    76.     Dim structWindowSize As RECT
    77.    
    78.     Select Case uCode
    79.         Case HSHELL_ACTIVATESHELLWINDOW
    80.  
    81.         '*** This one tells us that a new window has been created ***
    82.         Case HSHELL_WINDOWCREATED
    83.             'wParam == Handle to the created window
    84.  
    85.         Case HSHELL_WINDOWDESTROYED
    86.             'wParam == Handle to the destroyed window
    87.  
    88.         Case HSHELL_WINDOWACTIVATED
    89.             'wParam == Handle to the activated window
    90.             'lParam == The value is TRUE if the window is in full-screen mode, or FALSE otherwise
    91.  
    92.         Case HSHELL_GETMINRECT
    93.             'wParam == Handle to the minimized or maximized window
    94.             'lParam == Pointer to a RECT structure
    95.             CopyMemory structWindowSize, lParam, Len(structWindowSize)
    96.  
    97.         Case HSHELL_REDRAW
    98.             'wParam == Handle to the redrawn window
    99.             'lParam == The value is TRUE if the window is flashing, or FALSE otherwise
    100.  
    101.         Case HSHELL_TASKMAN
    102.  
    103.         Case HSHELL_LANGUAGE
    104.             'wParam == Handle to the window
    105.             'lParam == Handle to a keyboard layout
    106.  
    107.         Case HSHELL_ACCESSIBILITYSTATE
    108.             'wParam == ACCESS_FILTERKEYS, ACCESS_MOUSEKEYS, or ACCESS_STICKYKEYS
    109.  
    110.             If wParam = ACCESS_FILTERKEYS Then
    111.  
    112.             ElseIf wParam = ACCESS_MOUSEKEYS Then
    113.  
    114.             ElseIf wParam = ACCESS_STICKYKEYS Then
    115.  
    116.             End If
    117.         Case HSHELL_APPCOMMAND
    118.             'wParam == Windows 2000: Where the WM_APPCOMMAND message was originally sent
    119.             'lParam == application command corresponding to the input event
    120.  
    121.            
    122.     End Select
    123.    
    124.     ShellProc = CallNextHookEx(hHook, uCode, wParam, lParam)
    125. End Function

  16. #16
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by penagate
    You said that timers are inconsistent, slow, horrible, but you haven't said why. And also I hardly think that any of those reasons matter in a situation like this. But if it is possible to do it through a hook then I would not touch any form of loop or timer.
    I have yet to find out the real reasons why. They are probably slow because they are objects, simulating a loop and executing the code within it ever so many intervals. How it maintains low CPU usage, I can't tell ya. I have no idea what code MS put in there to do this. But if I knew, then I would know why they are so slow, inconsistant, inaccurate, and such.

    When I get home, I'm gonna do an experiment with multiple timers to see why they make it even worse. When I am done, Im gonna upload it.

  17. #17
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Check if Application is Running Without Timer

    It's possible to make your own timer functions with GetTickcount and the Sleep API and a Date variable.
    Just calling Sleep with a value of one millsec, from inside the delay loop, reduces the CPU use to almost nill.

    The Problem is that it's damned hard to kill your program if the user exits while in one of those loops.
    A VB Timer doesn't have that problem.

  18. #18
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Check if Application is Running Without Timer

    That's because you aren't suppose to be using the Sleep API to slow down loops. It halts your program and all of it's events for a number of milliseconds, even after using DoEvents. It's no wonder why you have a hard time closing your app. This project I uploaded is a managed loop locked at 60 FPS.

    The GetTickCount API is only 10 ms accurate, while the QueryPerformanceCounter and QueryPerformanceFrequency API's are 1 ms accurate.
    Attached Files Attached Files

  19. #19
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Check if Application is Running Without Timer

    Ah ha. That's why Timers get worse and worse the more you use. Let's say you have a Listbox and 4 timers, with the first 3 set at an interval of 1, and the 4th one set at an interval of 1000. Using this code:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Timer1_Timer()
    4.  
    5.     List1.AddItem "1"
    6.     List1.AddItem "1"
    7.     List1.AddItem "1"
    8.  
    9. End Sub
    10.  
    11. Private Sub Timer2_Timer()
    12.  
    13.     List1.AddItem "2"
    14.     List1.AddItem "2"
    15.     List1.AddItem "2"
    16.  
    17. End Sub
    18.  
    19. Private Sub Timer3_Timer()
    20.    
    21.     List1.AddItem "3"
    22.     List1.AddItem "3"
    23.     List1.AddItem "3"
    24.  
    25. End Sub
    26.  
    27. Private Sub Timer4_Timer()
    28.    
    29.     Timer1.Enabled = False
    30.     Timer2.Enabled = False
    31.     Timer3.Enabled = False
    32.     Timer4.Enabled = False
    33.    
    34. End Sub

    You will see that Timer1's code will fire off first, then Timer2, then Timer3, then back to Timer1, 2, 3, etc. But sometimes it'll end up skipping every now and then, so sometimes you end up with 1, 3, 1, 2, 3, 1, 2, 1, 2, 3, etc. I had it add 3 items each into the listbox because I wanted to see if the timers actually ran at the same time to interupt each other. Turns out that it has to execute ALL of the code one timer at a time before executing code in the next timer. So the other timers have to wait. It was an interesting learning experience there.

  20. #20
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by Jacob Roman
    Ah ha. That's why Timers get worse and worse the more you use.
    That's why I mentioned the Sleep API

    And I just have Sleep set to 1 ms, not many things will hit it in that time.
    The problem with killing the app doesn't come from the Sleep API.
    It happens when the user tries to exit the program and triggers an event.
    The forms unload event will run, but then the code will return to the loop and then to the sub that called the loop.
    That normally prevents the app from closing.

    I'll have to check into QueryPerformanceCounter and QueryPerformanceFrequency, sounds good.

    BTW, I looked at another of your posts with your Managed Loop.zip
    You mentioned that it's not setup to handle DoEvents.
    So what do you do when you DO need to handle user events during a delay?

  21. #21
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Check if Application is Running Without Timer

    It does use DoEvents, although I recommend an alternative to it since it's one of the worse functions ever written for VB. And read post #18 which stated this:

    Quote Originally Posted by Jacob Roman
    That's because you aren't suppose to be using the Sleep API to slow down loops. It halts your program and all of it's events for a number of milliseconds, even after using DoEvents. It's no wonder why you have a hard time closing your app.
    So don't use the Sleep API at all

    And if you noticed in my code, I had my loop setup like this:

    VB Code:
    1. Running = True
    2.  
    3. Do While Running
    4.  
    5. ...
    6.  
    7. Loop

    And the Form_Unload statement having this:

    VB Code:
    1. Private Sub Form_Unload(Cancel As integer)
    2.  
    3.      Running = False 'Will end the loop
    4.  
    5.      Unload Me
    6.  
    7.      End
    8.  
    9. End Sub

  22. #22
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Check if Application is Running Without Timer

    Well, judging by your title of 'DirectX God', I'm guessing your delay loops are for games where time/respnce is critical.

    But I'm more likely to use them to halt a program and wait for a user to answer a question or hit a 'continue' button.
    In which case I see nothing wrong with using Sleep.

    And the 'Stops your app' aspect is exactly what lowers the CPU use.
    Last edited by longwolf; Jul 13th, 2005 at 10:36 PM.

  23. #23
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Check if Application is Running Without Timer

    Let's see if you are right. You're using XP I assume, is that correct? I'm using ME so I can't do this. Make a sample app. Put the Sleep API in the General declarations and in the Form_Activate event, make it Sleep 100000. While the app is frozen, check how much CPU is being used. Is it low or high?

  24. #24
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by Jacob Roman
    Let's see if you are right. You're using XP I assume, is that correct? I'm using ME so I can't do this. Make a sample app. Put the Sleep API in the General declarations and in the Form_Activate event, make it Sleep 100000. While the app is frozen, check how much CPU is being used. Is it low or high?
    I run everything from win95/win98 - WinNt4-WinXp.
    (Several comps with several partions)
    My main comp is XP.

    Your post raises a lot of Q's.
    Why can't you do it on ME?
    Why would you want a delay loop in the Form_Activate event?

    For grins and giggles I tried it.
    The CPU useage was between 0-4%
    Most of that was going to IE and taskmanager.

    So what was the point of the exercise?

  25. #25
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Check if Application is Running Without Timer

    I just downloaded and tried your Managed Loop app.
    CPU usage 100%

    I also noticed the 'End' statement in the unload event.

    If you change your code in frmMain like this:
    VB Code:
    1. Private Sub Form_Activate()
    2.  
    3.     Main
    4.     txtTest.Text = "Gotcha!"
    5. End Sub
    6.  
    7. Private Sub Form_Unload(Cancel As Integer)
    8.  
    9.     Running = False
    10.    
    11.     Unload Me
    12.    
    13. '    End
    14.  
    15. End Sub
    You'll get the same problem I mentioned.

    The End statement isn't recommended because it can cause memory leaks depending on what else is in your code.

    I sometimes have to use the End statement if I use this type of delay.
    Just make sure that ALL objects from all modules get unloaded first

    Next I tried adding Sleep to your Public Sub Game_Loop()

    I did have to set Sleep to 10 ms to get an effect.
    It lowered the CPU usage to 42% with no noticeable effect on the frame rate.

  26. #26
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Check if Application is Running Without Timer

    BTW, Hiya eyeRmonkey
    Didn't mean to take over your post

  27. #27
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    And the 'Stops your app' aspect is exactly what lowers the CPU use
    Don't worry about what Taskmanager is telling you the CPU usage is.

    If you have a loop with a DoEvents statement in it you'll see 100% CPU usage. All that means is that the CPU is spending time in your loop rather than the idle process. Your DoEvents allows other threads to grab CPU time when they need it.

    Putting a sleep statement in your loop only puts it back into the idle process for the sleep time. Besides, putting Sleep 10 in your loop limits your loop times to > 10ms increments.

  28. #28
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by moeur
    OK, here is the code.
    Make sure that the ShellProce function is in a standard module and that
    you export the other two subs.
    If this works it will open a world of programming, good luck.
    What do I do once I've compiled it? Don't I have to register the hook or something?
    Last edited by penagate; Jul 15th, 2005 at 09:38 AM.

  29. #29
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    So, you should now have two subs you can call from VB in the dll. so from your VB app
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub SetShellHook Lib "MyDll.dll" ()
    4. Private Declare Sub RemoveShellHook Lib "MyDll.dll" ()
    5.  
    6. Private Sub Form_Load()
    7.     SetShellHook
    8. End Sub
    9.  
    10. Private Sub Form_Unload(Cancel As Integer)
    11.     RemoveShellHook
    12. End Sub
    will set the hook

    First you have to decided what to do when the message is received. I doubt if you could do something as simple as popup a MsgBox, but you could use sendmessage to send mouse clicks or keypresses back to your VB app.

  30. #30
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    What's the point of making it a standard DLL anyway? If the code is that simple then it could just be run from an EXE like a regular callback. Obviously I have missed something.

  31. #31
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    What's the point of making it a standard DLL anyway?
    It's because we are setting up a system-wide hook (as opposed to a thread-specific hook).
    System-wide hooks can intercept messages dispatched to all threads in the system, and the code for their filter functions must (in most cases) be placed inside a Win32 dll. This is because when a thread needs to call the filter function it needs to map the function into its own address space and this can only be done using a Win32 dll.

    So we are really injecting our code into every running thread that can receive shell messages.

  32. #32
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    moeur I changed SetShellHook into a function that returns the hHook value. But when I call it (It is supposed to display a messagebox with the result) the MsgBox doesn't show up

  33. #33
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    When you previously used this method to create dlls, did you try to invoke a messagebox from inside the dll?

    Anyway, make SetShellHook a function that returns a Long and retrun the value that way.

  34. #34
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by moeur
    When you previously used this method to create dlls, did you try to invoke a messagebox from inside the dll?
    No

    Quote Originally Posted by moeur
    Anyway, make SetShellHook a function that returns a Long and retrun the value that way.
    That's what I did. The MsgBox call was in the test EXE project. That's why I'm so confused why it didn't work. I also tried stripping all the code except the CallNextHookEx call from ShellProc. Still no luck.

    Here's the code in the exe:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetShellHook _
    4. Lib "C:\Yudster\Development\Visual Basic 6\My Code Tests\WH_SHELL hook\ShellHook.dll" ( _
    5.     ByVal lpfnCallback As Long _
    6. ) As Long
    7.  
    8. Private Declare Function RemoveShellHook _
    9. Lib "C:\Yudster\Development\Visual Basic 6\My Code Tests\WH_SHELL hook\ShellHook.dll" ( _
    10. ) As Long
    11. '
    12.  
    13. Private Sub cmdHook_Click()
    14.     MsgBox "Result: " & SetShellHook(AddressOf HookNotify)
    15. End Sub
    16.  
    17. Private Sub cmdUnhook_Click()
    18.     MsgBox "Result: " & RemoveShellHook
    19. End Sub

    The RemoveShellHook call returns zero and the MsgBox displays. The other one doesn't. I also tried removing the parameter from SetShellHook (like you had it). That didn't produce a MsgBox either.

  35. #35
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    What is the code inside the dll SetHookFunction, any changes?

    Also, you will not be able to make a Notify callback from the dll, so don't worry about passing AddressOf HookNotify. The reason is that when the filter function is called by the message system, it is called in another process which has another address space and so pointers to functions in the VB address space won't make sense.

    There are other reasons too as pointed out in this thread
    http://www.vbforums.com/showthread.php?t=327589.
    Last edited by moeur; Jul 15th, 2005 at 12:29 PM.

  36. #36
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    Quote Originally Posted by moeur
    What is the code inside the dll SetHookFunction, any changes?
    VB Code:
    1. Public Function SetShellHook() As Long
    2.     If (IsHooked) Then
    3.         MsgBox "Don't hook SHELL twice or you will be unable to unhook it."
    4.        
    5.       Else
    6.         'mlpfnCallback = plpfnCallback
    7.        
    8.         'the following line installs a system-wide hook
    9.         hHook = SetWindowsHookExA(WH_SHELL, AddressOf ShellProc, App.hInstance, 0)
    10.         IsHooked = True
    11.         SetShellHook = hHook
    12.        
    13.     End If
    14. End Function

    Also, you will not be able to make a Notify callback from the dll, so don't worry about passing AddressOf HookNotify. The reason is that when the filter function is called by the message system, it is called in another process which has another address space and so pointers to functions in the VB address space won't make sense.
    Bugger didn't think of that. What are we supposed to do with the hook then once it is set up anyway?
    Last edited by penagate; Jul 15th, 2005 at 12:42 PM.

  37. #37
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    BTW was I supposed to export ShellProc as well?
    Last edited by penagate; Jul 15th, 2005 at 12:43 PM. Reason: Just realised I contradicted myself

  38. #38
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    try this function instead.
    VB Code:
    1. Public Function SetShellHook() As Long
    2.     If (IsHooked) Then
    3.         SetShellHook = hHook
    4.     Else
    5.        'the following line installs a system-wide hook
    6.         hHook = SetWindowsHookExA(WH_SHELL, AddressOf ShellProc, App.hInstance, 0)
    7.         IsHooked = True
    8.         SetShellHook = hHook
    9.     End If
    10. End Function

    BTW was I supposed to export ShellProc as well?
    No that is only useful inside the dll

    What are we supposed to do with the hook then once it is set up anyway?
    We can do one of two things:
    1. handle the message inside the dll.
    2. pass data back to the VB App using a WM_COPYDATA message.

  39. #39
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check if Application is Running Without Timer

    I found the issue, it was App.hInstance. Changing that to zero made the function work and the Msgbox was displayed correctly (But of course the hook wasn't set).

    Because App is part of the VB runtime we can't use that. So we need another way of finding the instance handle, unless we can somehow link to the VB runtime through code.

  40. #40
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Check if Application is Running Without Timer

    I just solved this problem for someone else. Let me find it...

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width