can somebody let me know how to create a windows service application in visualbasic 6.0
Printable View
can somebody let me know how to create a windows service application in visualbasic 6.0
It is more than well explained there, providing a usefull OCX and sample
http://msdn.microsoft.com/library/de...n_ntsrvocx.asp
Edit: Correct link : http://msdn.microsoft.com/archive/de...n_ntsrvocx.asp
Thanks Jeff,But i wanted some code where i could start coding in vb 6.0,i want to know if i have to reference any dll to code or what ???Quote:
Originally posted by JeffB
It is more than well explained there, providing a usefull OCX and sample
http://msdn.microsoft.com/library/de...n_ntsrvocx.asp
actually, if you use the OCX I provide you, you will have to add this OCX and use it to code, the code will be much simpler than to use API, if you don't want to use it, you don't need to add any references, except that you will have to use the APIS, StartService, OpenSCManager, StopService, GetServiceDisplayName, OpenService, etc.. Each time you use a API, you need to add the API declarations and some time, a structure used with the API (that case, there is a SERVICE_STATUS structure you may need). There is no object provided with VisualBasic to create a service, the OCX I show you is the one Microsoft Distribute, but you can find other on the market (Desaware NT Service ToolKit), but they cost money.
for more informations on API:
http://www.vbapi.com
http://www.allapi.net
JeffB
Hey Jeff,Quote:
Originally posted by JeffB
actually, if you use the OCX I provide you, you will have to add this OCX and use it to code, the code will be much simpler than to use API, if you don't want to use it, you don't need to add any references, except that you will have to use the APIS, StartService, OpenSCManager, StopService, GetServiceDisplayName, OpenService, etc.. Each time you use a API, you need to add the API declarations and some time, a structure used with the API (that case, there is a SERVICE_STATUS structure you may need). There is no object provided with VisualBasic to create a service, the OCX I show you is the one Microsoft Distribute, but you can find other on the market (Desaware NT Service ToolKit), but they cost money.
for more informations on API:
http://www.vbapi.com
http://www.allapi.net
JeffB
Thanks a lot for the info,Actually i could not download that sample code you were talking about.I could now,Jeff I am not a VC++ guy,so tell me is that ocx created in vc++ so what should i do to add it in vb program
Bye
the OCX is created in Visual C++, but you don't need to do that, you only add the OCX in your project on your form, like the way you add and use a CommonDialog object. in the downlaod is include the compiled OCX, the source C++ code and a sample in VB (with code and properly working). To add a OCX, on your form, do Add Composants, browse and go search for the OCX, an icon will be added to your toolbar, select it and put it on your form, the default name will be NTService1, you will see all his property, and you can use Object Explorer to look property, const and method
JeffB - hope it helps
Hi Jeff,Quote:
Originally posted by JeffB
the OCX is created in Visual C++, but you don't need to do that, you only add the OCX in your project on your form, like the way you add and use a CommonDialog object. in the downlaod is include the compiled OCX, the source C++ code and a sample in VB (with code and properly working). To add a OCX, on your form, do Add Composants, browse and go search for the OCX, an icon will be added to your toolbar, select it and put it on your form, the default name will be NTService1, you will see all his property, and you can use Object Explorer to look property, const and method
JeffB - hope it helps
I am working on it,I will get back when i have some problem,hopefully i wont have any.I have to create my own service which queries database and sends a response when the condition matches.
If you have any suggestion ,do reply.
Thanks.
here is the sample plus OCX:
JeffB
hi jeff,Quote:
Originally posted by JeffB
here is the sample plus OCX:
JeffB
everything fine,but i am not able to see it in services.how to run it.
i think it is not entering this statement "If Command = "-install" Then"
do i have to set anything before i run my exe.
thanks
The first time you run it you must pass -install as a commandline parameter to install it as a service. Your command line should look like
YouServiceName.exe -install
Yeah, before running a service, it must be installed on the computer, Configuration Pannel -> Service, and you need administrator right to install / uninstall it, you can look the service already installed on your computer with regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
You might also want your service to use an NT account and his password, because your service will be running even if no one is logged, so if you use ADO (for example), you absolutely need registry key, and when no user is connected, no registry, error and, because a service, you don't know when it is bugged (except if you use custom log or the NTService logevent that write to NT log)
JeffB
hi dsj,Quote:
Originally posted by DSJ
The first time you run it you must pass -install as a commandline parameter to install it as a service. Your command line should look like
YouServiceName.exe -install
my serive exe is called "sample.exe"
so i have to give 'sample.exe -install' in the command prompt isn't ?
or do i have to give the path where it recides.
I dont get exactly what you are saying,when i type that in my command prompt it gives an error saying "command not recognised"
could you be a little more specific.
Or is there anything else i have to do.
thanks
hi jeff,Quote:
Originally posted by JeffB
Yeah, before running a service, it must be installed on the computer, Configuration Pannel -> Service, and you need administrator right to install / uninstall it, you can look the service already installed on your computer with regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
You might also want your service to use an NT account and his password, because your service will be running even if no one is logged, so if you use ADO (for example), you absolutely need registry key, and when no user is connected, no registry, error and, because a service, you don't know when it is bugged (except if you use custom log or the NTService logevent that write to NT log)
JeffB
hi dsj,
my serive exe is called "sample.exe"
so i have to give 'sample.exe -install' in the command prompt isn't ?
or do i have to give the path where it recides.
I dont get exactly how and what you are saying,when i type that in my command prompt it gives an error saying "command not recognised"
could you be a little more specific.
Or is there anything else i have to do.
thanks
There's three way to do it:
>First, compile your EXE with Visual Basic (create the EXE, not execute the program), after, go to MSDOS, browse your HardDisk by using cd.. and cd /NameFolder (or type C: to go to the root of your C:) Next, when it come to your EXE, type sample -install, it will look like C:\AppName\> sample -install
>Second, go to StartMenu, go to Execute, on the little window, go to browse, select your EXE (sample.exe), add to the text that should be sample.exe " -install" so it will be writed in the textbox: "sample.exe -install"
>Directly from VB, when you compile your code and execute it FROM VB, you can add command property, before running the app, go to -project/property of sample, then on the tab create, search for the textbox parameters of the command line (my version is in french, so the translate might be innaccurate :) ), then type -install
>Because Service are a little special, your project will need a documentation, for any one that must install it on his computer, they must know to type -install, to have administrator rights and to type -uninstall (or whatever you want the parameters to be, I was using -i and -u) :)
JeffB - hope it helps
Quote:
Originally posted by JeffB
There's three way to do it:
>First, compile your EXE with Visual Basic (create the EXE, not execute the program), after, go to MSDOS, browse your HardDisk by using cd.. and cd /NameFolder (or type C: to go to the root of your C:) Next, when it come to your EXE, type sample -install, it will look like C:\AppName\> sample -install
>Second, go to StartMenu, go to Execute, on the little window, go to browse, select your EXE (sample.exe), add to the text that should be sample.exe " -install" so it will be writed in the textbox: "sample.exe -install"
>Directly from VB, when you compile your code and execute it FROM VB, you can add command property, before running the app, go to -project/property of sample, then on the tab create, search for the textbox parameters of the command line (my version is in french, so the translate might be innaccurate :) ), then type -install
>Because Service are a little special, your project will need a documentation, for any one that must install it on his computer, they must know to type -install, to have administrator rights and to type -uninstall (or whatever you want the parameters to be, I was using -i and -u) :)
JeffB - hope it helps
Hey Jeff,
That was of great help.thanks a lot.Actually i have to query a database and perform certain actions...do you think i can do this ?
I will give it a try on monday.
Also if you have any idea about VB.NET:-My question is If i create a EXE in VB.NET and have to install in client systems,what are the minimum requirements for that client machine.
Thanks.
Have a great week end.
Hi, if you use ADO or OLEDB, you will need to have your service run with an NT account, you can set the account and password with the OCX, or by the configuration pannel -> services, select your service, and instead of using system account, use user account with the full form: if your user account is balajigr1214, use the form SERDIV/balajigr1214
For VB.Net, I don't know really about, there is a vb.net forum here, and I think that this is not much requirement, it is more the developper computer that will need to be better than the one that run the compiled exe.
JeffB - have a nice day (night...)
.NET Framewrork will be needed on the client PCs. It better to have the service run on the server and the client query that server for data ...
Reading this thread in its totality I think Jeff has some good knowledge about exe. I want to know if there is a way to dettect that a exe is not running and if it is not running then to activate it ... Activating is not a problem actually .. The only thing is how to detect that a exe is activated ...
thanks.
[QUOTE]Originally posted by JeffB
Hi, if you use ADO or OLEDB, you will need to have your service run with an NT account, you can set the account and password with the OCX, or by the configuration pannel -> services, select your service, and instead of using system account, use user account with the full form: if your user account is balajigr1214, use the form SERDIV/balajigr1214
For VB.Net, I don't know really about, there is a vb.net forum here, and I think that this is not much requirement, it is more the developper computer that will need to be better than the one that run the compiled exe.
JeffB - have a nice day (night...) [/QUO
TE]
Hi Jeff,
Thanks,i will be working on the services very soon.All this as helped me a lot.Maybe if any doubt i will get back to you.
Bye
That code will allow you to detect, for example, if the process notepad.exe is running. It will only do a msgbox if it find the process. Remember, their can be a lot of process that have the same name, but different PIDS. Try the program while their is many notepad opened, you'll see. You should rearranged all of the code to make a function where you pass a EXE name and that return an array of PIDS number. Oh well, here it is : :)
sorry for misaligned text ... :(Code:Option Explicit
Private Declare Function EnumProcesses Lib "psapi.dll" _
(lpidProcess As Long, _
ByVal cb As Long, _
cbNeeded As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" _
(ByVal hProcess As Long, _
lphModule As Long, _
ByVal cb As Long, _
lpcbNeeded As Long) As Long
Private Declare Function GetModuleBaseName Lib "psapi.dll" _
Alias "GetModuleBaseNameA" _
(ByVal hProcess As Long, _
ByVal hModule As Long, _
ByVal lpBaseName As String, _
ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Sub Command1_Click()
Dim lngPIDList() As Long ' The list of the Process (PIDs)
Dim lngNumberOfPID As Long ' Number of PIDs
Dim lngReturnValue As Long ' Return value of Win32 function
Dim lngNumberOfBytesReceived As Long ' Number of Bytes received from EnumProcess()
Dim lngProcessHandle As Long ' Process Handle (OpenProcess)
Dim lngModuleHandle As Long ' Module Handle (EnumProcessModule)
Dim strProcessName As String ' Name of the process
Dim strExe As String
strExe = "notepad.exe" ' Name of the researched process
Dim i As Long ' For...Next i
Dim lngNumberOfProcessFound As Long 'How many notepad.exe have we found?
lngNumberOfProcessFound = 0
lngNumberOfPID = 100
ReDim lngPIDList(lngNumberOfPID)
'Return the list of process (PIDS number)
lngReturnValue = EnumProcesses(lngPIDList(0), lngNumberOfPID * 4, lngNumberOfBytesReceived)
If lngReturnValue = 0 Then
MsgBox "RED-ALERT: No PID returned (EnumProcess)"
Exit Sub
End If
'How many process do we receive???
lngNumberOfPID = lngNumberOfBytesReceived / 4
If lngNumberOfPID = 0 Then
MsgBox "No process corresponding to :" & strExe
End If
For i = 0 To lngNumberOfPID - 1
' Process 0, 2, and 28 are special process we don't want to analyse them
If lngPIDList(i) = 0 Or lngPIDList(i) = 2 Or lngPIDList(i) = 28 Then GoTo NextProcess
' Retrieve an handle on the process
lngProcessHandle = OpenProcess(&H400 Or &H10, 0&, lngPIDList(i))
If lngProcessHandle = 0 Then GoTo NextProcess
'Retrieve an handle on the fist module of the process (first module is always
'the EXE)
lngModuleHandle = 0
lngReturnValue = EnumProcessModules(lngProcessHandle, lngModuleHandle, 4&, lngNumberOfBytesReceived)
If lngReturnValue = 0 Then GoTo NextProcess
' Get the name of the module
strProcessName = String$(256, 0)
lngReturnValue = GetModuleBaseName(lngProcessHandle, lngModuleHandle, strProcessName, Len(strProcessName))
If lngReturnValue = 0 Then GoTo NextProcess
strProcessName = Replace(strProcessName, Chr(0), "")
' Is that the name we are searching?
If UCase$(strProcessName) = UCase$(strExe) Then
lngNumberOfProcessFound = lngNumberOfProcessFound + 1
MsgBox strProcessName & " " & lngPIDList(i), vbOKOnly, "Result"
End If
NextProcess:
' Close the handle of the process
If lngProcessHandle > 0 Then CloseHandle (lngProcessHandle)
Next i
If lngNumberOfProcessFound = 0 Then
MsgBox "No process ---" & strExe & "--- founded.", vbOKOnly, "Result"
End If
End Sub
JeffB