windows service

This is a discussion on windows service within the Windows Programming forums, part of the Platform Specific Boards category; I'm testing out methods of making services in windows. I'm using the method that microsoft suggests but I'm the binary ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    windows service

    I'm testing out methods of making services in windows. I'm using the method that microsoft suggests but I'm the binary that gets compiled does not create a service. Any ideas?

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <winsock.h>
    #include <winsvc.h>
    #include <windows.h>
    
    int main()
    {  
        SC_HANDLE  schService;
        SC_HANDLE schSCManager;
    
        LPCTSTR lpszBinaryPathName = "c:\\notepad.exe"; 
        LPCTSTR lpszDisplayName = "NewService"; 
     
        schService = CreateService(
            schSCManager, 
            "Sample_Srv",
            lpszDisplayName, 
            SERVICE_ALL_ACCESS,
            SERVICE_WIN32_OWN_PROCESS, 
            SERVICE_DEMAND_START,
            SERVICE_ERROR_NORMAL,
            lpszBinaryPathName,
            NULL, 
            NULL, 
            NULL, 
            NULL,
            NULL); 
        CloseServiceHandle(schService); 
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Any ideas?
    Yeah, post it on the windows board next time
    Thread moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    from MSDN: CreateService()

    Parameters
    hSCManager
    [in] Handle to the service control manager database. This handle is returned by the OpenSCManager() function and must have the SC_MANAGER_CREATE_SERVICE access right.
    You have a little more work to do to give schSCManager a value.

    gg

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    I added the below code which should have fixed it..also I added an error system to tell me if the service installed or not but it still won't work...its giving me "Failed" every time and the service doesn't install.

    Code:
    schSCManager = OpenSCManager(
                            NULL,                   
                            NULL,                   
                            SC_MANAGER_ALL_ACCESS   
                            );
    Code:
     if (schService){
                printf("Installed.\n");
        CloseServiceHandle(schService); }
        else{
                printf("Failed\n");
                CloseServiceHandle(schService);

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    Use this instead: printf("Failed, ec=%d", GetLastError());

    Does the user that your process is running under have Admin rights?
    Read the remarks section carefully in the OpenSCManager() reference.

    gg

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Originally posted by Codeplug
    Use this instead: printf("Failed, ec=%d", GetLastError());

    Does the user that your process is running under have Admin rights?
    Read the remarks section carefully in the OpenSCManager() reference.

    gg
    Heh..it turned out to be something really stupid, and the error code that you suggested helped figure it out. Turns out that earlier the program did create the service..I just didn't add a startservice function. The error that it was giving me was 1073...according to MSDN Service name already exists.

    Anyway gonna add a start function and I'll go from there. Thx for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows Messenger service.
    By myk_raniu in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-25-2006, 04:00 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. writing a windows service
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-01-2004, 06:32 AM
  4. windows service + hooks + dll
    By rotis23 in forum Windows Programming
    Replies: 5
    Last Post: 09-04-2003, 07:06 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21