Results 1 to 8 of 8

Thread: How do i create a "service"?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769

    How do i create a "service"?

    I want to make an application that runs as a service. The application has no user interface. I want to create an MMC snap-in to set and change variables within this application. I might also want to create other applications that can link to that same instance of the "service" application to set and manipulate variables.

    How could this be done? ActiveX.exe's or normal .exe with .dll?
    How then would i specify in another app, that it must connect to that instance of the service, and not create a new one.

  2. #2
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    You might want to think about using another language.

    You can write a service in VB, but it is not ideal.

    Services do not always have access to the display, and since VB defaults to the display for an untrapped (or untrappable) error, you can have a problem.

    You basically have to trap every possible error and write to a log rather than to the display.

    We started trying to write a service in VB last year, but quickly gave up and did it in C instead.

    If you try it, here's a good place to start:

    http://vbwire.com/advanced/howto/service.asp
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    Ok. Is it possible though to have other applications link dirrectly to a running instance of my app or object. If so, how do i go about this?

  4. #4
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    Yes, I know it is, but I'm not sure exactly how.

    I guess you need to find the handle of the application and then send it a message?

    Perhaps somebody smarter than me can help? Otherwise I'll give it a try over the weekend and see how it goes.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  5. #5
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Edinburgh, Scotland
    Posts
    272
    I don't know if this is of any use, but I use this code to register a VB app as a service to hide it from CTRL-ALT-DEL. The only problem is that the API is unsupported under WinNT. Call RegisterAsService to hide it, and UnRegisterAsService to show it again.

    VB Code:
    1. ' Declarations and constants to hide from CTRL-ALT-DEL list
    2. Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
    3. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    4. Private Declare Function RegisterServiceProcess Lib "kernel32" (ByVal dwProcessID As Long, ByVal dwType As Long) As Long
    5. Private Const RSP_UNREGISTER_SERVICE = 0
    6. Private Const RSP_SIMPLE_SERVICE = 1
    7.  
    8.  
    9. Private Sub RegisterAsService()
    10. ' Hide program from CTRL-ALT-DEL list by registering it as a service
    11.  
    12. Dim lProcId As Long
    13. Dim lReturn As Long
    14.  
    15.     lProcId = GetCurrentProcessId()
    16.     lReturn = RegisterServiceProcess(lProcId, RSP_SIMPLE_SERVICE)
    17.  
    18. End Sub
    19.  
    20.  
    21. Private Sub UnRegisterAsService()
    22. ' Restore program to CTRL-ALT-DEL list by unregistering it as a service
    23.  
    24. Dim lProcId As Long
    25. Dim lReturn As Long
    26.  
    27.     lProcId = GetCurrentProcessId()
    28.     lReturn = RegisterServiceProcess(lProcId, RSP_UNREGISTER_SERVICE)
    29.  
    30. End Sub

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    thanx rudgej.

    Unfortunately, the standard is NT. In actual fact it will be running under NT Server.

    I don't think it is all too possible to accomplish a "service" application in VB, like BrianHawley says. I will work around it, and definately start learning C!

    How does a program like Excel achieve the linking though. I have been able to use the GetObject function to get the already running instance of Ecel. Does anyone know a VB way of doing the same thing?

  7. #7
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Yes.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes it is possible under VB to create and register a vb program as
    a NT Service. Look here for the code to create the service exe and
    here to register and create the service out of your exe.

    Its a two step process. You need to create the exe that will
    interact with the Service Manager and another step to actually
    create the NT Service and register the exe to be associated with
    the service.

    HTH
    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

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