Results 1 to 24 of 24

Thread: [2005] Do something when closing program with task manager..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    [2005] Do something when closing program with task manager..

    Dear all
    I know that there are CloseReason.TaskManagerClosing in vb.net handle by formclosing.
    But I try to close it in processes of task manager(not closing in applications of task manager), it seems doesn't work! Do there any other ways to do the same thing of my question?

  2. #2
    Junior Member NeoDement's Avatar
    Join Date
    Apr 2008
    Posts
    20

    Re: [2005] Do something when closing program with task manager..

    Probably impossible because the process is ended, therefore the process can't process anything after that point. You'd need a secondary process to watch that process by my reckoning (which is often wrong)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by NeoDement
    Probably impossible because the process is ended, therefore the process can't process anything after that point. You'd need a secondary process to watch that process by my reckoning (which is often wrong)
    what's exactly meaning of "often wrong"?? You mean I need to write the secondary monitor program?

  4. #4
    Junior Member NeoDement's Avatar
    Join Date
    Apr 2008
    Posts
    20

    Re: [2005] Do something when closing program with task manager..

    I mean I often get things wrong. You'd need to make the first program launch the second one and detect when the second one ends.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by NeoDement
    I mean I often get things wrong. You'd need to make the first program launch the second one and detect when the second one ends.
    Really? How do I search the method in MSDN to do the thing that u mention?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: [2005] Do something when closing program with task manager..

    When you close a window from the Applications tab in the Task Manager it's like calling Process.CloseMainWindow. When you end a process from the Processes tab it's like calling Process.Kill. Anyone who ends processes that way without good reason deserves what they get. It is a last resort for ending processes that cannot, or will not, be closed any other way.

    If your issue is that you don't want the user to be able to end your app that way then you should be restricting their access to the Task Manager, which functionality is built into Windows.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by jmcilhinney
    When you close a window from the Applications tab in the Task Manager it's like calling Process.CloseMainWindow. When you end a process from the Processes tab it's like calling Process.Kill. Anyone who ends processes that way without good reason deserves what they get. It is a last resort for ending processes that cannot, or will not, be closed any other way.

    If your issue is that you don't want the user to be able to end your app that way then you should be restricting their access to the Task Manager, which functionality is built into Windows.
    ha jmcihinney, are you meaning CloseReason.TaskManagerClosing? I think it just work for the reason of closing in applications tab.

  8. #8
    Lively Member
    Join Date
    Jan 2008
    Location
    England
    Posts
    113

    Re: [2005] Do something when closing program with task manager..

    I would assume that TaskManager uses TerminateProcess()

    BOOL WINAPI TerminateProcess(
    __in HANDLE hProcess,
    __in UINT uExitCode
    );

    I would probably recommended trying to hook that API in taskmgr and then stop taskmgr from calling the function from kernel32.

    Here are some methods briefly discussed:

    http://blade.nagaokaut.ac.jp/cgi-bin...by-talk/157256

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: [2005] Do something when closing program with task manager..

    MHO - You want to circumvent Process.Kill? Task Manager is, and should be, my last resort at getting rid of a process that went south. A question, how in your code, are you going to know whether your program is having a problem and needs to be killed or the user is just being a user? It is my computer running your application and if I want it killed why can't I? It is bad enough that under XP you can stop me from shutting down.

    IMHO, I hope there isn't a way to do this.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2005] Do something when closing program with task manager..

    No, there isn't. TerminateProcess() is from what I understand a protected function in Kernel32 and can't be intercepted without a kernel mode driver (i.e. a RootKit). TaskManager is still a program though, and it can be gummed up if something is specifically targeting it (which is why I wrote my own "kill process" application).

    Such is the programming of MalWare though and besides theory, I doubt many programmers on here will divulge any specifics for putting any of it into practice.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  11. #11
    Lively Member
    Join Date
    Jan 2008
    Location
    England
    Posts
    113

    Re: [2005] Do something when closing program with task manager..

    Not going into any specifics, you can Hook it easily using the detours lib in C++

    Other ways to end the process if, for whatever reason, it became unresponsive to terminate process would be:
    SafeTerminateProcess() - kernel32
    NtTerminateProcess() - ntdll
    TerminateProcess() - coredll

    But yeah, thats how you would go about it. The folks at this forum aren't too keen on anything that could potentially be used for anything vaugley malicious. Id recommend reading up in dll injection and API hooking. You should also know that this will most probably be far easier to accomplish in C++

    Edit: And, Jenner, as far as I can see, you can hook it just like any other function.
    newpat - Could you state for what purpose you wish to 'Do something' when closing a program from tskmgr?
    As they have stated above, people only really use End Process as a last resort if your program has frozen. What are you looking to do?
    Last edited by TGOSeraph; Apr 21st, 2008 at 04:06 PM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by TGOSeraph
    Not going into any specifics, you can Hook it easily using the detours lib in C++

    Other ways to end the process if, for whatever reason, it became unresponsive to terminate process would be:
    SafeTerminateProcess() - kernel32
    NtTerminateProcess() - ntdll
    TerminateProcess() - coredll

    But yeah, thats how you would go about it. The folks at this forum aren't too keen on anything that could potentially be used for anything vaugley malicious. Id recommend reading up in dll injection and API hooking. You should also know that this will most probably be far easier to accomplish in C++

    Edit: And, Jenner, as far as I can see, you can hook it just like any other function.
    newpat - Could you state for what purpose you wish to 'Do something' when closing a program from tskmgr?
    As they have stated above, people only really use End Process as a last resort if your program has frozen. What are you looking to do?
    Thx for your concern
    Ok..My purpose is to do when a user logout the system, but he closes it in processes of task manager. My system will do this thing at the following:

    When he login before, the attribute(emptate) is going false in database, when he logout, the empState should turn true, the eState can make sure another user cannot login the system when others are logined to the system.
    But the system is closed in processes of task mamanegr, how should I control the empState in the above way by vb method or something else??

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by dbasnett
    MHO - You want to circumvent Process.Kill? Task Manager is, and should be, my last resort at getting rid of a process that went south. A question, how in your code, are you going to know whether your program is having a problem and needs to be killed or the user is just being a user? It is my computer running your application and if I want it killed why can't I? It is bad enough that under XP you can stop me from shutting down.

    IMHO, I hope there isn't a way to do this.
    I supply detail of question in front of the post of this topic, check it out
    In my last topic, u have given me an unhandledexception in my.application and its work!So I can ignore even my program having a problem. Now I concern that the user is trying to close it in processes of task manager, not applications of task manager. You are right, I cannot stop you to close the program, but I need to do something(mentioned at last post) in order to maintain my database.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by Jenner
    No, there isn't. TerminateProcess() is from what I understand a protected function in Kernel32 and can't be intercepted without a kernel mode driver (i.e. a RootKit). TaskManager is still a program though, and it can be gummed up if something is specifically targeting it (which is why I wrote my own "kill process" application).

    Such is the programming of MalWare though and besides theory, I doubt many programmers on here will divulge any specifics for putting any of it into practice.
    woo, Kernel32 is really professional for me, I never know the API in it. But u suggest that I do not allow user to end it in task manager and use my own method to let user close my program?

  15. #15
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Do something when closing program with task manager..

    You should never do anything that interferes with the task manager and what it does. If a user needs to end your application then that's their choice. The only reason you'd need to do this is in a kiosk application in which case you'd disable the task manager as well as many other Windows components rather than trying to intercept the end process / end application.
    Quote Originally Posted by newpat
    Thx for your concern
    When he login before, the attribute(emptate) is going false in database, when he logout, the empState should turn true, the eState can make sure another user cannot login the system when others are logined to the system.
    But the system is closed in processes of task mamanegr, how should I control the empState in the above way by vb method or something else??
    I think the real question is why are you doing this in the first place?

    When a client application connects to the server, there is no guarantee that the client will remain connected for many reasons (system crash? network crash? network instability? etc) so you won't have a reliable way to setting this flag at all.

    I'd suggest re-thinking your design rather than trying to handle the application being ended. What is empState used for?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by kasracer
    You should never do anything that interferes with the task manager and what it does. If a user needs to end your application then that's their choice. The only reason you'd need to do this is in a kiosk application in which case you'd disable the task manager as well as many other Windows components rather than trying to intercept the end process / end application.

    I think the real question is why are you doing this in the first place?

    When a client application connects to the server, there is no guarantee that the client will remain connected for many reasons (system crash? network crash? network instability? etc) so you won't have a reliable way to setting this flag at all.

    I'd suggest re-thinking your design rather than trying to handle the application being ended. What is empState used for?
    empState point out the user is login the system or not. true=logout, false=user is login. Your suggestion is I may think another way, I think I need to write the trigger instead of user end the application and asked in the forum
    http://www.vbforums.com/showthread.php?t=519410
    But I also want to know how to do it done by my program, so I will keep looking at this topic
    Last edited by newpat; Apr 22nd, 2008 at 07:44 AM.

  17. #17
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2005] Do something when closing program with task manager..

    So, you want a soft-lock to show how many users are logged into your database or are you only allowing one user into your database at a time?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by Jenner
    So, you want a soft-lock to show how many users are logged into your database or are you only allowing one user into your database at a time?
    Only allowing one user to use his own account at a time.

  19. #19
    Member
    Join Date
    Aug 2007
    Posts
    43

    Re: [2005] Do something when closing program with task manager..

    I resolved this problem by starting a new thread on application lauch. It writes to a the database every 10 seconds. On launch, it checks for user activity in the last 10 seconds. If there is activity, that user can't log in again. It's also easy to see who's logged in..

  20. #20
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by xoqon
    I resolved this problem by starting a new thread on application lauch. It writes to a the database every 10 seconds. On launch, it checks for user activity in the last 10 seconds. If there is activity, that user can't log in again. It's also easy to see who's logged in..
    That works but isn't very scalable. What happens if you have 800 people using your application at the same time? Talk about slow-down.

    Is there a specific reason a user can't login with his or her account at multiple locations?

    Another idea may be to have a table that logs all logins. If one is at a different location inform the user.

    Another option is to connect to a server (a custom server), have it write to the database and once the connection disappears, write to the database again.

    Another option is to use SQL Session (or possibly a Web Server session if this is an ASP.Net app) so even if a user disconnects they can easily reconnect to their session and resume.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by xoqon
    I resolved this problem by starting a new thread on application lauch. It writes to a the database every 10 seconds. On launch, it checks for user activity in the last 10 seconds. If there is activity, that user can't log in again. It's also easy to see who's logged in..
    ha, It seems you have same idea with me. I want to ask that I want to write a trigger in database. Please try to answer if you can
    http://www.vbforums.com/showthread.php?t=519410

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by kasracer
    That works but isn't very scalable. What happens if you have 800 people using your application at the same time? Talk about slow-down.

    Is there a specific reason a user can't login with his or her account at multiple locations?

    Another idea may be to have a table that logs all logins. If one is at a different location inform the user.

    Another option is to connect to a server (a custom server), have it write to the database and once the connection disappears, write to the database again.

    Another option is to use SQL Session (or possibly a Web Server session if this is an ASP.Net app) so even if a user disconnects they can easily reconnect to their session and resume.
    For the security reason, I have to write this function.
    1. You mean I have to create an attribute to remember what is the user's ip?
    2. How about the user logout? It cannot always connecting to the database.

  23. #23
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by newpat
    For the security reason, I have to write this function.
    What do you mean? If security is an issue then you may need to change other things as well. Is this all within an intranet for a company? If so are users domain authenticated? Where is the database connectivity information stored? Surely if you're worried about security you're abstracting the database through the use of web services, correct?
    Quote Originally Posted by newpat
    1. You mean I have to create an attribute to remember what is the user's ip?
    No as IPs change sometimes frequently. I would remember the username instead.
    Quote Originally Posted by newpat
    For the security reason, I have to write this function.
    2. How about the user logout? It cannot always connecting to the database.
    I'm not sure what this means, sorry.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] Do something when closing program with task manager..

    Quote Originally Posted by kasracer
    What do you mean? If security is an issue then you may need to change other things as well. Is this all within an intranet for a company? If so are users domain authenticated? Where is the database connectivity information stored? Surely if you're worried about security you're abstracting the database through the use of web services, correct?
    No as IPs change sometimes frequently. I would remember the username instead.
    I'm not sure what this means, sorry.
    Supposed it is intranet, but also can accessed by internet. I think one user can login his own account is perfectly to ensure no others will use his account at same time.

    1. How do u take advantage of username?
    2. Nothing..Just some doubt on it.

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