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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a "service" Can I do it? 1

Status
Not open for further replies.

SerialCoder

Programmer
Oct 18, 2002
95
US
I wrote a routine today that goes through the network and does some of the cleanup work that i have to do from time to time. does VB allow you to create a service out of a routinelike this and have it run on a machine transparently? if so what is this called?
 
There is nothing that can't be done...

You should register the app as a service for it to be transparent, and not visible in the ctrl+alt+del menu.

let me pls do a little research b4 i answer it completely

All the best Praveen Menon
 
Thanks, I figured it was a matter of how and not if. would this method create a dll that would need to be registered?
 
Check out these articles on MSDN Q137890, Q170883, and Q175948. Microsoft has an NtSvc.ocx that allows you to run an app as a service. The above articles will give you a good idea as to what you have to do. Let me know if this helps you out. If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

[cheers]
 
the complete source to mke an app a service. I have packed it as a class module for reusability. The sample code for using the calss is also provided.


+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!Option Explicit

'====================================================
'File Name : clsService.cls
'Class : clsService
'Written by : Praveen Menon
'Created on : 7th November 2002
'Last Modified : -
'====================================================

Private Declare Function GetCurrentProcessId Lib "kernel32"
() As Long
Private Declare Function RegisterServiceProcess
Lib "kernel32" (ByVal dwProcessID As Long, ByVal dwType As
Long) As Long

Private Const RSP_SIMPLE_SERVICE = 1
Private Const RSP_UNREGISTER_SERVICE = 0

Public Sub Hide_In_CTRL_ALT_Delete()
Dim processID As Long
Dim regService As Long
processID = GetCurrentProcessId()
regService = RegisterServiceProcess(processID,
RSP_SIMPLE_SERVICE)
End Sub

Public Sub Show_In_CTRL_ALT_DELETE()
Dim processID As Long
Dim regService As Long
processID = GetCurrentProcessId()
regService = RegisterServiceProcess(processID,
RSP_UNREGISTER_SERVICE)
End Sub

'End Class
'**************************
'==========================
'File Name : frmServiceTest.frm
'Form : frmServiceTest
'Written by : Praveen Menon
'Created on : 7th November 2002
'Last Modified : -
'====================================================
Option Explicit

Dim CAD As New clsService

Private Sub Form_Load()
CAD.Hide_In_CTRL_ALT_Delete
End Sub

'End Form
'***********************************************************

'!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!


All the best

Praveen Menon
pcmin@rediffmail.com
 
You get a helpful vote and I don't even want to do what this post talks about..

You get it because of this line "let me pls do a little research b4 i answer it completely"

Talk about awesome! Did I help?
Vote!
 
Sadly, I am forced to say that you shouldn't even begin to believe thatPraveen's code is sufficient.
 
On MSDN, there used to be the C++ source code for an NT Service control that you could drop on a form and make your application an NT service. It would not be transparant to a Task Manager, but it would be listed as a service that you could set to be started on bootup and always running on the machine. Be careful not to display error messages when creating a service . . . they may be no one there to click OK. Log errors to the Event log. I'll see if I can find the code to create an NT service in VB using this ActiveX control.
 
You are referring to ntsvc.ocx, which foada mentioned above.
 
Yep, I've used ntsvc.ocx - and finding all of it (samples, docs) and getting it to work are a pain, but it CAN be done.

It is a long topic to try and cover here though, and as I recall even though MS provided the OCX, this approach is still not recommended by them. They just don't want people making NT services in VB because there is a lot of bad VB code out there.

They seemed worried about uncontrolled memory leaks and similar "crud build-up" due to poor programming practices. The problem is an NT service may run, and run, and run... much longer and through more internal cycles than a typical simple desktop or client program. The funny part about this argument is that I'd have to say I've seen perhaps more cruddy C code out there than can otherwise be believed. And C encourages some very NASTY kinds of dangerously bad programming.

So... you can do it, I have done it, it isn't THAT hard but it is far from trivial even using ntsvc.ocx, and MS advises against it. I'd start with some searches on "ntsvc.ocx" though to find samples and tutorials on the web. I'd offer you mine as a sample, but I'm constrained by work-rules.

Your best bet is to use Delphi, modern versions of which can make NT services very easily. Problem is: Delphi is not cheap, and you can't get by with the low-end editions. It also means learning a new language, a new IDE, and a new component model.

If your VB program is very well-behaved, and quite simple... you might also get by with a sort of "wrapper" called srvany.exe that was part of the old NT 4 Resource Kit (I think). I had trouble locating srvany.exe myself, and it also had a number of dire warnings attached.

srvany.exe itself was the service you installed, with a control file of some sort that told a given installed instance of it to fire off your VB EXE when it started up.
 
Strong,

you mean to say it wont make the application transparent to the ctrl+alt+del menu? May be by making it transparent, the thread is not completely answered, but i just wanted to give SerialCoder a starting point. Also i didnt test it in NT. in my system(Win98), it is making the app transparent. Cant we do it from basic VB? Cant we make an app run in a network? can't we make a program run in another system? is the use of srvany.exe or ntsvc.ocx or Delphi, which is painful, is the only way out? Praveen Menon
pcmin@rediffmail.com
 
By "goes through the network" I was assuming SerialCoder meant that his program cleans up files on network shares or something.

You can always "do it the 'right' way" and write your service in C/C++ but I would have to think that would be the most painful way of all if you're a VB programmer.

There is such a thing as a Win9x service as well, but the process is a bit different from making an NT service. As I recall, a "Win9x service" is really just an executeable that is made a startup program via a registry setting, and it appears as a killable program via CTRL-ALT-DEL. You could combine this with one of the hack's that makes a Win9x program not show up in the list after CTRL-ALT-DEL.

If SerialCoder wants to do this (in Win9x) it should be easy. Look at and then use a "hide it" hack like PraveenMenon's above. I doubt this will work under NT/2000/XP the way it does under 9x, but it might be good enough there.

I don't see that SerialCoder asked to hide his program from being killed anyway. Maybe all he wants is auto-start?
 
Praveen,

You won't be able to test your code under NT/W2000/XP, since RegisterServerProcess is a W95/98/Me function. It does not exist in the NT family.

Even in the W9x family, the function call is also a bit of a misnomer, because it doesn't make your process into a service in the sense that an NT user would understand. It merely means that a) you get invisibility from the task list and b) the program won't shut down when you select &quot;Close all programs and log on as a different user&quot; from the Shutdown menu, or &quot;logout <username>&quot;.
 
Hi all,

In a previous thread (thread222-339517) somebody suggested Desaware NT service toolkit. Does any one know how good this is? All I know is it is expensive..


Matt
 
I wanted to play with this (Desaware) but the price wasn't right.

This is always the headache with things like 3rd-party controls. Price them too low and nobody takes them seriously, price 'em too high and you seriously limit your market.

But I did get the impression that the Desaware approach was easier and perhaps slicker and more complete than just using that ntsrv.ocx

For a non-trivial service you usually need some sort of &quot;console&quot; to it at times, to do things beyond start/stop/pause. Hopefully a package like Desaware includes support for a simple named-pipe server function in your service - this is one of the standard ways for a console applet to talk to an active service. Ideally they'd offer a way to easily make an MMC snap-in to use for configuring and monitoring your service.

But we've ventured pretty deep into &quot;making a real service&quot; now.
 
And how you would argue that svrany is not a 3rd party solution?
 
It depends on how technical you want to be about it. It is a tool published by microsoft. We are talking about running a VB(microsoft) app in an NT/2000(microsoft) enviroment. By that rational, would not any MS component be a 3rd party tool?
 
It may be a philosophical point. My suspicion is that anyone saying &quot;How do I do a service in VB without a 3rd party add-on&quot; is actually saying &quot;How do I do a service natively in VB?&quot;. From that point of view, srvrany is a 3rd party helper, as is ntsvc.ocx.

If I'm being ungenerous, however, the answer to:

>By that rational, would not any MS component be a 3rd party tool?

would be yes, if it is not really part of VB.

Ask yourself this: if I use Delphi and I want to parse XML files then there are several solutions, the easiest of of which is to use one of the Microsoft XML libraries. Now, you'd agree that this would be a considered a 3rd party solution, I suspect.

Now, I get similar choices in VB. But, suddenly, the same library (not a part of VB) is not a 3rd party solution simply because both products have the word &quot;Microsoft&quot; in front of them?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top