Results 1 to 22 of 22

Thread: windows service using VB

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    india
    Posts
    273

    windows service using VB

    How to create windows service using Visual basic 6.0?

    I know it using vb.Net but if anybody worked on it using vb 6.0 then please let me know.

    help is appreciated

    thanks & regards

    PPCC

  2. #2
    Addicted Member
    Join Date
    Feb 2004
    Location
    Texas
    Posts
    144
    How do you create windows services? Are you talking about Start & Stop Windows Services from Control Panel?

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    To create a NT Service from VB...
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const SERVICE_WIN32_OWN_PROCESS = &H10&
    4. Private Const SERVICE_WIN32_SHARE_PROCESS = &H20&
    5. Private Const SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS + _
    6.                               SERVICE_WIN32_SHARE_PROCESS
    7.  
    8. Private Const SERVICE_ACCEPT_STOP = &H1
    9. Private Const SERVICE_ACCEPT_PAUSE_CONTINUE = &H2
    10. Private Const SERVICE_ACCEPT_SHUTDOWN = &H4
    11.  
    12. Private Const SC_MANAGER_CONNECT = &H1
    13. Private Const SC_MANAGER_CREATE_SERVICE = &H2
    14. Private Const SC_MANAGER_ENUMERATE_SERVICE = &H4
    15. Private Const SC_MANAGER_LOCK = &H8
    16. Private Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
    17. Private Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
    18.  
    19. Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    20. Private Const SERVICE_QUERY_CONFIG = &H1
    21. Private Const SERVICE_CHANGE_CONFIG = &H2
    22. Private Const SERVICE_QUERY_STATUS = &H4
    23. Private Const SERVICE_ENUMERATE_DEPENDENTS = &H8
    24. Private Const SERVICE_START = &H10
    25. Private Const SERVICE_STOP = &H20
    26. Private Const SERVICE_PAUSE_CONTINUE = &H40
    27. Private Const SERVICE_INTERROGATE = &H80
    28. Private Const SERVICE_USER_DEFINED_CONTROL = &H100
    29. Private Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
    30.                                     SERVICE_QUERY_CONFIG Or _
    31.                                     SERVICE_CHANGE_CONFIG Or _
    32.                                     SERVICE_QUERY_STATUS Or _
    33.                                     SERVICE_ENUMERATE_DEPENDENTS Or _
    34.                                     SERVICE_START Or _
    35.                                     SERVICE_STOP Or _
    36.                                     SERVICE_PAUSE_CONTINUE Or _
    37.                                     SERVICE_INTERROGATE Or _
    38.                                      SERVICE_USER_DEFINED_CONTROL)
    39.  
    40. Private Const SERVICE_DEMAND_START As Long = &H3
    41. Private Const SERVICE_ERROR_NORMAL As Long = &H1
    42.  
    43. ' Private Enum SERVICE_CONTROL
    44. Private Const SERVICE_CONTROL_STOP = &H1
    45. Private Const SERVICE_CONTROL_PAUSE = &H2
    46. Private Const SERVICE_CONTROL_CONTINUE = &H3
    47. Private Const SERVICE_CONTROL_INTERROGATE = &H4
    48. Private Const SERVICE_CONTROL_SHUTDOWN = &H5
    49. ' End Enum
    50.  
    51. ' Private Enum SERVICE_STATE
    52. Private Const SERVICE_STOPPED = &H1
    53. Private Const SERVICE_START_PENDING = &H2
    54. Private Const SERVICE_STOP_PENDING = &H3
    55. Private Const SERVICE_RUNNING = &H4
    56. Private Const SERVICE_CONTINUE_PENDING = &H5
    57. Private Const SERVICE_PAUSE_PENDING = &H6
    58. Private Const SERVICE_PAUSED = &H7
    59. ' End Enum
    60.  
    61. Private Type SERVICE_TABLE_ENTRY
    62.    lpServiceName     As String
    63.    lpServiceProc     As Long
    64.    lpServiceNameNull As Long
    65.    lpServiceProcNull As Long
    66. End Type
    67.  
    68. Private Type SERVICE_STATUS
    69.    dwServiceType             As Long
    70.    dwCurrentState            As Long
    71.    dwControlsAccepted        As Long
    72.    dwWin32ExitCode           As Long
    73.    dwServiceSpecificExitCode As Long
    74.    dwCheckPoint              As Long
    75.    dwWaitHint                As Long
    76. End Type
    77.  
    78. Private Declare Function StartServiceCtrlDispatcher Lib "advapi32.dll" Alias "StartServiceCtrlDispatcherA" (lpServiceStartTable As SERVICE_TABLE_ENTRY) As Long
    79. Private Declare Function RegisterServiceCtrlHandler Lib "advapi32.dll" Alias "RegisterServiceCtrlHandlerA" (ByVal lpServiceName As String, ByVal lpHandlerProc As Long) As Long
    80. Private Declare Function SetServiceStatus Lib "advapi32.dll" (ByVal hServiceStatus As Long, lpServiceStatus As SERVICE_STATUS) As Long
    81. Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
    82. Private Declare Function CreateService Lib "advapi32.dll" Alias "CreateServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal lpDisplayName As String, ByVal dwDesiredAccess As Long, ByVal dwServiceType As Long, ByVal dwStartType As Long, ByVal dwErrorControl As Long, ByVal lpBinaryPathName As String, ByVal lpLoadOrderGroup As String, ByVal lpdwTagId As String, ByVal lpDependencies As String, ByVal lp As String, ByVal lpPassword As String) As Long
    83. Private Declare Function DeleteService Lib "advapi32.dll" (ByVal hService As Long) As Long
    84. Declare Function CloseServiceHandle _
    85.     Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
    86. Declare Function OpenService _
    87.     Lib "advapi32.dll" Alias "OpenServiceA" _
    88.     (ByVal hSCManager As Long, ByVal lpServiceName As String, _
    89.     ByVal dwDesiredAccess As Long) As Long
    90.  
    91. '** Change SERVICE_NAME as needed
    92. Private Const SERVICE_NAME As String = "MyService"
    93.  
    94. Private hServiceStatus As Long
    95. Private ServiceStatus  As SERVICE_STATUS
    96. Public Sub Main()
    97.      
    98.       Dim hSCManager As Long
    99.       Dim hService As Long
    100.       Dim ServiceTableEntry As SERVICE_TABLE_ENTRY
    101.       Dim b As Boolean
    102.       Dim cmd As String
    103.  
    104.       cmd = Trim(LCase(Command()))
    105.       Select Case cmd
    106.          Case "install"                      'Install service on machine
    107.             hSCManager = OpenSCManager(vbNullString, vbNullString, _
    108.                          SC_MANAGER_CREATE_SERVICE)
    109.             hService = CreateService(hSCManager, SERVICE_NAME, _
    110.                        SERVICE_NAME, SERVICE_ALL_ACCESS, _
    111.                        SERVICE_WIN32_OWN_PROCESS, _
    112.                        SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, _
    113.                        App.Path & "\" & App.EXEName, vbNullString, _
    114.                        vbNullString, vbNullString, vbNullString, _
    115.                        vbNullString)
    116.             CloseServiceHandle hService
    117.             CloseServiceHandle hSCManager
    118.          Case "uninstall"                   'Remove service from machine
    119.             hSCManager = OpenSCManager(vbNullString, vbNullString, _
    120.                          SC_MANAGER_CREATE_SERVICE)
    121.             hService = OpenService(hSCManager, SERVICE_NAME, _
    122.                        SERVICE_ALL_ACCESS)
    123.             DeleteService hService
    124.             CloseServiceHandle hService
    125.             CloseServiceHandle hSCManager
    126.          Case Else                                    'Start the service
    127.             ServiceTableEntry.lpServiceName = SERVICE_NAME
    128.             ServiceTableEntry.lpServiceProc = FncPtr(AddressOf ServiceMain)
    129.             b = StartServiceCtrlDispatcher(ServiceTableEntry)
    130.       End Select
    131.    
    132.    End Sub
    133. Public Sub ServiceMain(ByVal dwArgc As Long, ByVal lpszArgv As Long)
    134.      
    135.       Dim b As Boolean
    136.  
    137.       'Set initial state
    138.       ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS
    139.       ServiceStatus.dwCurrentState = SERVICE_START_PENDING
    140.       ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP _
    141.                                       Or SERVICE_ACCEPT_PAUSE_CONTINUE _
    142.                                       Or SERVICE_ACCEPT_SHUTDOWN
    143.       ServiceStatus.dwWin32ExitCode = 0
    144.       ServiceStatus.dwServiceSpecificExitCode = 0
    145.       ServiceStatus.dwCheckPoint = 0
    146.       ServiceStatus.dwWaitHint = 0
    147.  
    148.       hServiceStatus = RegisterServiceCtrlHandler(SERVICE_NAME, AddressOf Handler)
    149.       ServiceStatus.dwCurrentState = SERVICE_START_PENDING
    150.       b = SetServiceStatus(hServiceStatus, ServiceStatus)
    151.  
    152.       '** Do Initialization Here
    153.  
    154.       ServiceStatus.dwCurrentState = SERVICE_RUNNING
    155.       b = SetServiceStatus(hServiceStatus, ServiceStatus)
    156.  
    157.       '** Perform tasks -- if none exit
    158.  
    159.       ''** If an error occurs, the following should be used for shutting
    160.       ''** down:
    161.       ''   SetServerStatus SERVICE_STOP_PENDING
    162.       ''   Clean up
    163.       ''   SetServerStatus SERVICE_STOPPED
    164.  
    165. End Sub
    166. Public Sub Handler(ByVal fdwControl As Long)
    167.      
    168.       Dim b As Boolean
    169.  
    170.       Select Case fdwControl
    171.          Case SERVICE_CONTROL_PAUSE
    172.             '** Do whatever it takes to pause here.
    173.             ServiceStatus.dwCurrentState = SERVICE_PAUSED
    174.          Case SERVICE_CONTROL_CONTINUE
    175.             '** Do whatever it takes to continue here.
    176.             ServiceStatus.dwCurrentState = SERVICE_RUNNING
    177.          Case SERVICE_CONTROL_STOP
    178.             ServiceStatus.dwWin32ExitCode = 0
    179.             ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING
    180.             ServiceStatus.dwCheckPoint = 0
    181.             ServiceStatus.dwWaitHint = 0     'Might want a time estimate
    182.             b = SetServiceStatus(hServiceStatus, ServiceStatus)
    183.             '** Do whatever it takes to stop here.
    184.             ServiceStatus.dwCurrentState = SERVICE_STOPPED
    185.          Case SERVICE_CONTROL_INTERROGATE
    186.             'Fall through to send current status.
    187.          Case Else
    188.       End Select
    189.       'Send current status.
    190.       b = SetServiceStatus(hServiceStatus, ServiceStatus)
    191. End Sub
    192. Public Function FncPtr(ByVal fnp As Long) As Long
    193.    FncPtr = fnp
    194. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    india
    Posts
    273

    sounds great

    Ok tell me, this code is need to write in class module, if yes then what to do next , i mean how to add this service in windows service list so that i can start and stop this service.

    tx

    PPCC

  5. #5
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: windows service using VB

    hi.
    i had this code in my sources. but this code have a problem.
    i compile this code and put the exe file in address of my service to run.
    when i click on start of my service to run this exe file , program terminate.
    see the error.jpg file

    program terminate on this line.
    b = SetServiceStatus(hServiceStatus, ServiceStatus)
    and show me an error.
    see the error service.jpg

    Please help me.
    Attached Images Attached Images   
    Last edited by Payman; Jul 9th, 2006 at 08:08 PM.

  6. #6
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: windows service using VB

    CS

  7. #7
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: windows service using VB

    Or... here's an OCX (from MS) that's easy to use too.
    Attached Files Attached Files
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  8. #8
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: windows service using VB

    yeah that worked cssriraman

    attached sample project ..
    BTW you will need to copy the exe's from that link to the projects folder to test or use as we cant include them in attachments (?) ..

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: windows service using VB

    Yes, that control is easy to use and I also have code on the forums but remember that its unsupported by MS.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: windows service using VB

    Quote Originally Posted by RobDog888
    Yes, that control is easy to use and I also have code on the forums but remember that its unsupported by MS.
    Yeah, sad but true. But to be fair to MS, I've been using that control in a production environment for years now and have never had a problem with it.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: windows service using VB

    I like the APIs better as there is more control and they are not really to hard to work with. Plus, no ocx to distribute.
    Also, if you wanted to add a dependancy service you cant with the ocx.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: windows service using VB

    This is an internal app so there's no issue about distributing the OCX, but I agree with you about API's especially if you can be bothered to take the time to wrap them up in a nice, friendly to use class etc.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  13. #13
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: windows service using VB

    hi.
    i dont want to use ocx.

  14. #14
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: windows service using VB

    anyone have source of that ocx?

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: windows service using VB

    It uses code similar to the APIs I posted but since Microsoft wrote it you wont be able to get its source and would not be legal to do anyways
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: windows service using VB

    can i run my program as service without any component or refrence in vb6. only with code.

  17. #17
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: windows service using VB

    Only using RobDog's code. That doesn't require any external components or references other than the APIs.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  18. #18
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: windows service using VB

    Quote Originally Posted by pnish
    Only using RobDog's code. That doesn't require any external components or references other than the APIs.
    what is RobDog's code?

  19. #19
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: windows service using VB

    In post #3 in this thread...
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  20. #20
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: windows service using VB

    Quote Originally Posted by pnish
    In post #3 in this thread...
    i used that code. but that code have a problem. see my post(Post 5).

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: windows service using VB

    But you need to tell us if you are running 2000 or above and if you have Admin permissions?

    also, the error message shows that "Nima3" contains an error to the APIs cant start/stop it correctly. What does Nima3 contain and is it error free?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: windows service using VB

    my os is:
    Windows XP professional
    version 2002
    service pack2
    and i have a admin user.

    "Nima3" is my service name.
    can you run a program as service with RobDog's code?

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