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!

Visual Basic as a Service 2

Status
Not open for further replies.

ASPVBNerd

Programmer
Nov 23, 2005
83
SE
Is it possible to create an application that can run as a service programmed with Visual Basic 6?
I have read the msdn help and that article was about version 4 and 5 and microsft doesn't recommend this.
But I have copied the code from the help and install my application with this command from dos MyService.exe install
It now appears in the services dialog but the exe file isn't available in the service properties.

Is it possible to create a application coded by vb6 to run as a service?
 
Don't forget that when the service starts, the OnStart event is being run in the Service Control Manager's thread, and not yours. So you don't want to do anything time-consuming in there.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
I've looked at this some more.

The problem is more serious than first apparent!

As I see it, the NTSvc control raises events (Start Stop etc) These are integral to the correct operation of the control. At the moment, the whole project is based around 1 module, with the control loaded as a dll. This means that there are no event sinks available for the control to call back. (the stub code is there complete with implementation). If you set

Code:
      Case "/install"
        '***  set all defaults for the service here
        With ObjService
          
          '***  True if the service needs to interact with the user
          .Interactive = [COLOR=red]True[/color]
(and re-install) then when you try to start the service all hell breaks loose (exception error box, which is supressed because the thread isn't allowed to interact with the desktop).

The control is designed to be used with a form, and while I agree that in many instances OCXs can be used in a formless program, I think this is an exception to the rule!

It is possible to wrap the NTservice control in a class and either handle the events inside the class or just call a module method. However, I think this is just a bodge to dogmatically avoid using a hidden form.

[looks for strongm who will probably have a neat and tidy solution (probably requiring regular expressions and wombats)]

In short, I think it is time to recode the whole thing using a form as well as a module



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
You are right. The code that i have found in vbcity was actually with a form. So I thaught that I would change it as the way i wanted it.

What I have done is chosen to create Standard exe and removed the form and just added a module and from the I would use the api function SetTimer and KillTimer to use a timer for call every 5 seconds. becuase I am not using a form.

Code:
      Case "/install"
        '***  set all defaults for the service here
        With ObjService
          
          '***  True if the service needs to interact with the user
          .Interactive = True
I have chosen .Interactive = False

As I understod it is that a service should not have a form.
Should say that I have learn programming visual basic by own for about 10 months ago, so I am still a newb at this.

It feels like I have tried to learn something that is bigger then I know.

Can I have the same code and use it in class then just call the module or am I thinking wrong?

I would like to use zarkon4 but i felt that that was to diffcult for me.

I am very thankful for all responses that i am getting.

//George
 
As I understod it is that a service should not have a form.
I think you may be a bit confused; It is usual for services not to have a visual component, as they (in general) don't use the same "bit" of the display (look up winstations for further information). Likewise standard things like message boxes must not be used as they will cause the service to wait for user input that will never happen (because the message box is never displayed for the user)

That being said, it is quite possible to have a form in a service, providing that you do not need to interact with it (i.e. no buttons, menus or visible feedback). In fact, I would suggest that you never call the .Show method.

Likewise, it is perfectly possible to "wrap" the NTSVC control in your own class and call methods implemented in the main module.

However, I think this is just a bodge to dogmatically avoid using a hidden form.

I would suggest that this is a time where the "pure" approach of
BobRhodes said:
Well, yes, that's so, but it seems a bit of a workaround kludge to me to have some visual ui as a means of accomplishing some non ui purpose, that's all.
should be, in the interests of pragmatism, be avoided. I personally think that wrapping the control in a class to then call out to functions in a module is a worse Kludge

BobRhodes - I am not having a general disagreement however in this specific case I think using a hidden form is a much better solution

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Okay! Now I know that I could use a form and just load it and never show it. Then the example code that I have will be easy for me to use.

Last question. (just want to learn)
Matt:
If don't want to use a form but I want to use modules and classes insted. could I do it as the way that I did or that is totaly wrong with the code above with some changes?
 
Well, if I were a true purist, I'd be writing in machine language. I'm just saying that if Microsoft really wanted to support services in VB, they should have done it via classes rather than controls. That said, I don't disagree with you either, Matt.

:)

Bob
 
>if Microsoft really wanted to support services in VB

They didn't and don't. As chiph has said, they are strongly aginst it.


>should have done it via classes rather than controls

Having said that, you can load NTSVC.OCX as a class library rather than a control if you really want
 
Perhaps I should have said they WOULD have done it via classes rather than controls.
 
As chiph has said, they are strongly aginst it.
We obviously know it works -- either via the OCX or API calls. I used it in production systems supporting 9 million+ users. strongm, I'm sure you've done similar things with it.

I think their reason for removing it was the support headache. It was never part of the released product, and let's face it -- most VB programmers don't understand the whys and wherefores of NT services. They just want to get their job done, which is not saying bad things about VB programmers, it's just the culture, and since I am/was one, I can understand it. Not every VB programmer is a Dan Appleman.

How many times have visitors here tried to use NT services in inappropriate ways? More than I can count. So, I can understand why MS pulled the OCX from their site. They got tired of saying "It's unsupported, and you shouldn't be using it in that manner anyway."

Chip H.

____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Okay, I have started and works okay, but today I found a problem. I am using ntsvc.ocx
The NTService1 is not running Interactive.

At the end of the day I want to know if a process (dbBackup.exe) is running.
If the process isn't started then I want to start the process (dbBackup.exe).
This works well and the dbBackup.exe is started and is shown in the processes list.

I want the program dbBackup.exe to run interactive with the user.
What I mean by that is that the dbBackup.exe should be shown to the user. Is this possible?
If so could anyone help me this problem.

Code in my module
Code:
Public Const SW_SHOWNORMAL = 1
Public Const SW_SHOW = 5
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Public Const dbBackup = "dbBackup.exe"
Public Const Path = "C:\Temp\"
small part of my code in my timer
Code:
If IsProcessRunning(dbBackup) = False Then
      ShellExecute 0, vbNullString, Path & dbBackup, vbNullString, Path, SW_SHOWNORMAL
End If

George
 
I thought that if I created a dll that should only start the dbBackup.exe program.
I would call dll from the Service application and the main task of the dll would be just to start the program dbBackup.exe.

I did it like this and dbbackup.exe is started but as a process.

Please anyone help me with my problem.

George
 
Any exe will start in its own separate process. The fact that you start it in a dll doesn't change that. If you explain why that's a problem, perhaps someone can help.

HTH

Bob
 
This is starting to look confusing. Is it all just a dodge to avoid using Windows Scheduler or something?

I'd think you'd just start your EXE as a scheduled task, and write it to silently terminate if another copy is already running.
 
I have an application that is up and running all the time.
This application that is up and runing all the time is critical for the user.
Sometimes the program for no reason isn't running, this program has been terminated and not by the user .
This program is just badly programmed.

I can not have a windows Scheduler becuase i dont know when this program is terminated.

BobRodes said:
If you explain why that's a problem, perhaps someone can help.
This program that is being started has to be visible to the user

My code

George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top