Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:56:14+00:00 2026-05-31T23:56:14+00:00

I’m writing a C application which creates a Windows service. I’d like to check

  • 0

I’m writing a C application which creates a Windows service. I’d like to check if the service is installed before trying to call the installation function, but I can’t manage to find how to check it.

I’ve written the code above to try :

DWORD InstallMyService()
{
    char strDir[1024 + 1];
    SC_HANDLE schSCManager;
    SC_HANDLE schService;
    LPCTSTR lpszBinaryPathName;

    if (GetCurrentDirectory(1024, strDir) == 0)
    {
        aff_error("GetCurrentDirectory");
        return FALSE;
    }
    strcat(strDir, "\\"MY_SERVICE_BIN_NAME);
    if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
    {
        printf("Error OpenSCManager : %d\n", GetLastError());
        return FALSE;
    }
    lpszBinaryPathName = strDir;
    schService = CreateService(schSCManager, MY_SERVICE_NAME, MY_SERVICE_DESCRIPTOR,
                             SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
                             lpszBinaryPathName, NULL, NULL, NULL, NULL, NULL);
    if (schService == NULL)
    {
        printf("Error CreateService : %d\n", GetLastError());
        return FALSE;
    }
    CloseServiceHandle(schService);
    return TRUE;
}

But this code does not detect if the service is still existing or not. Do someone have an idea of which function to call ? I found many posts talking about this, but not in C, only in C# or VB.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:56:16+00:00Added an answer on May 31, 2026 at 11:56 pm

    A possibility would be to always attempt to CreateService() and if failure query GetLastError() and check if it is equal to ERROR_SERVICE_EXISTS:

    SC_HANDLE service_handle = CreateService(...);
    if (0 == service_handle)
    {
        if (ERROR_SERVICE_EXISTS == GetLastError())
        {
            /* Handle service already exists. */
        }
        else
        {
            /* Handle failure. */
        }
    }
    

    This would require a slight change to your code from having two functions InstallService() and CheckService() to having one function (for example) EnsureServiceInstalled().

    Or, you could use the OpenService() function, which will fail with GetLastError() code of ERROR_SERVICE_DOES_NOT_EXIST:

    SC_HANDLE scm_handle = OpenSCManager(0, 0, GENERIC_READ);
    
    if (scm_handle)
    {
        SC_HANDLE service_handle = OpenService(scm_handle,
                                               "the-name-of-your-service",
                                               GENERIC_READ);
        if (!service_handle)
        {
            if (ERROR_SERVICE_DOES_NOT_EXIST != GetLastError())
            {
                fprintf(stderr,
                        "Failed to OpenService(): %d\n",
                        GetLastError());
            }
            else
            {
                /* Service does not exist. */
                fprintf(stderr, "Service does not exist.\n");
            }
        }
        else
        {
            fprintf(stderr, "Opened service.\n");
            CloseServiceHandle(service_handle);
        }
    
        CloseServiceHandle(scm_handle);
    }
    else
    {
        fprintf(stderr,
                "Failed to OpenSCManager(): %d\n",
                GetLastError());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.