Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing forms from Unloading !

Status
Not open for further replies.

ShaneBrennan

Programmer
May 19, 1999
198
GB

I'm trying to stop a form from unloading, except via code. I found the following code from VBExplorer.com and it works fine, except it still allows Task Manager to close down the application:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
'UnloadMode possibilities:
'0 The user has chosen the Close command from the Control-menu box on the form.
'1 The Unload method has been invoked from code.
'2 The current Windows-environment session is ending.
'3 The Microsoft Windows Task Manager is closing the application.
'4 An MDI child form is closing because the MDI form is closing.

If UnloadMode <> 1 Then
Cancel = True
' free to continue with program
End If

End Sub

Task Manager takes about 5 seconds then returns it's infamous message &quot;The application is not responding, shall I give it another 5 seconds or shall I close it down now!&quot;

Has anyone got any idea how to prevent this, and keep the program runing?

Thanks for any help.
Shane Brennan





Shane Brennan
Shane.Brennan@tcat.ac.uk

 
Try [tt]App.TaskVisible = False[/tt].

You can always hide your app from the Task Manager by registering it as a &quot;Service Process&quot;:
[tt]
Declare Function RegisterServiceProcess Lib _
[tab] &quot;kernel32&quot; (ByVal dwProcessID As Long, _
[tab] ByVal dwType As Long) As Long
Declare Function GetCurrentProcessId Lib _
[tab] &quot;kernel32&quot; () As Long

Sub Main()
Dim Gcpi As Long
Dim RetL As Long
Gcpi = GetCurrentProcessId()
RetL = RegisterServiceProcess(Gcpi, 1)
End Sub
[/tt]

Just make sure you don't call this function until you have fully debugged your app (it makes it a little hard to shut down).

Hope this helps.

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
I can't get it to accept the RegisterServiceProcess line in the main()

I've created a Form_Load() function, to execute the code, with:

Private Sub Form_load()
Call main()
end sub

Main() stops on the line:

RetL = RegisterServiceProcess(Gcpi, 1)

with the error message:

Can't find DLL entry point RegisterServiceProcess in kernal32

Do you know why this is happending?



Shane Brennan
Shane.Brennan@tcat.ac.uk

 
You shouldn't have a problem if the declarations were pasted (verbatum) into a module. Sub Main() should be your startup object, though. You would call the function prior to loading Form1.

What OS are you testing on?
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
I'm using NT4. I think it's Service release 6.


Shane Brennan
Shane.Brennan@tcat.ac.uk

 
I've pasted the function in a module called Module1 (formGoodies.bas). Will this make a difference?



Shane Brennan
Shane.Brennan@tcat.ac.uk

 
It really shouldn't matter where you call it from.

The function is known to work well on NT 4. Just check the declarations and make sure everything is in order.

Oh, BTW, when you get this working, make sure you &quot;Unregister&quot; your app before you terminate it (to free system resources). You can do that by substituting 0 for the 1 listed as the last parameter in RegisterServiceProcess.
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
How can I get a list of the command/procedures/functions that are available in Dlls such as

Kernel32, user32, etc.?

I'm having great fun with VB - I tell ya I've had a real steep learning curve with this lovely language.

Shane Brennan

PS. Still trying!

PPS. It is possible that the RegisterServiceProcess option has been disabled? I'm just registered as a normal user not an administrator.


Shane Brennan
Shane.Brennan@tcat.ac.uk

 
Does your admin use system policies? It may be easier to &quot;create&quot; an NT service if you are part of the Administrative group. :)

The answer to your first question is You can find numerous good examples for using the API.

I don't think you will find a reference to RegisterServiceProcess, though. You might try:


Good luck. Did you try [tt]App.TaskVisible = False[/tt] first? It's usually preferable to use VB's built-in features.

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
I'm not sure about this, but wouldn't the app still appear in the Task Manager when registered as a service. It won't appear under the &quot;Applications&quot; tab, but wouldn't it still appear under the &quot;Processes&quot; tab?
 
Yes it will appear in the Process tab, but it minimises the risk of my students deliberately closing down a piece of software that I want to keep running.

The routine that Alt255 works! except I can still close down the process, and I get the Task Manager &quot;Shall I wait for 5 second then zap the sucker message&quot;.

Question: Is the RegisterServiceProcess in kernal32.dll only a Administrator function?

Shane

Shane Brennan
Shane.Brennan@tcat.ac.uk

 
How are you closing the app? You are intercepting attempts to close the main form through the QueryClose sub, aren't you?

The API doesn't know anything about user groups... but I've seen enough slick tricks performed my company's net admin to know there are ways to control almost everything a user does on a computer.

There is at least one more way to prevent a user from closing your app (it poses a few complications and involves a great deal more code, so save it for last). Try to get the preceding solutions to work well, first.

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top