The C and C++ code snippets monologue forum

The C, C++, Code, Coder, Coding Snippets Zone for Tenouk.com

 

 
It is currently Thu Sep 09, 2010 12:43 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Getting a Windows service information
PostPosted: Mon Jan 01, 2007 11:13 am 
Offline

Joined: Wed Nov 08, 2006 11:47 pm
Posts: 91
Code:
// Don't forget to add .h to the header files...
#include <windows>
#include <stdio>

BOOL GetSampleServiceConfig();

int main()
{
   GetSampleServiceConfig();

   return 0;
}

BOOL GetSampleServiceConfig()
{
   SC_HANDLE schService, schSCManager;
    LPQUERY_SERVICE_CONFIG lpqscBuf;
   // Service name, service to query
   LPCTSTR lpszServiceName = L"NetLogon";
    LPSERVICE_DESCRIPTION lpqscBuf2;
    DWORD dwBytesNeeded;
    BOOL bSuccess=TRUE;

   // Open a handle to the SC Manager database...
       schSCManager = OpenSCManager(
       NULL,                    // local machine
       NULL,                    // SERVICES_ACTIVE_DATABASE database is opened by default
       SC_MANAGER_ALL_ACCESS);  // full access rights, not a good choice

   if (NULL == schSCManager)
      printf("OpenSCManager(), Open a handle to the SC Manager database failed, error: %d.\n", GetLastError());
   else
      printf("OpenSCManager(), Open a handle to the SC Manager database looks OK.\n");

    // Open a handle to the service.
    schService = OpenService(
        schSCManager,         // SCManager database
        lpszServiceName,      // name of service
        SERVICE_QUERY_CONFIG);// need QUERY access
    if (schService == NULL)
    {
      printf("OpenService() failed, error: %d", GetLastError());
        return FALSE;
    }
   else
      printf("OpenService() is OK\n");


    // Allocate a buffer for the configuration information.

    lpqscBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc(
        LPTR, 4096);
    if (lpqscBuf == NULL)
    {
        return FALSE;
    }

    lpqscBuf2 = (LPSERVICE_DESCRIPTION) LocalAlloc(
        LPTR, 4096);
    if (lpqscBuf2 == NULL)
    {
        return FALSE;
    }

    // Get the configuration information.
    if (! QueryServiceConfig(
        schService,
        lpqscBuf,
        4096,
        &dwBytesNeeded))
    {
      printf("QueryServiceConfig() failed, error: %d", GetLastError());
        bSuccess = FALSE;
    }
   else
      printf("QueryServiceConfig() is OK\n");

    if (!QueryServiceConfig2(
        schService,
        SERVICE_CONFIG_DESCRIPTION,
        (LPBYTE)lpqscBuf2,
        4096,
        &dwBytesNeeded) )
    {
        printf("QueryServiceConfig2() failed, error %d", GetLastError());
        bSuccess = FALSE;
    }
   else
      printf("QueryServiceConfig2() is OK\n");

    // Print the configuration information.
    printf("\n%S service configuration: \n", lpszServiceName);
    printf(" Type: 0x%x\n", lpqscBuf->dwServiceType);
    printf(" Start Type: 0x%x\n", lpqscBuf->dwStartType);
    printf(" Error Control: 0x%x\n", lpqscBuf->dwErrorControl);
    printf(" Binary path: %S\n", lpqscBuf->lpBinaryPathName);

    if (lpqscBuf->lpLoadOrderGroup != NULL)
        printf(" Load order group: %S\n", lpqscBuf->lpLoadOrderGroup);
    if (lpqscBuf->dwTagId != 0)
        printf(" Tag ID: %d\n", lpqscBuf->dwTagId);
    if (lpqscBuf->lpDependencies != NULL)
        printf(" Dependencies: %S\n", lpqscBuf->lpDependencies);
    if (lpqscBuf->lpServiceStartName != NULL)
        printf(" Start Name: %S\n", lpqscBuf->lpServiceStartName);
    if (lpqscBuf2->lpDescription != NULL)
        printf(" Description: %S\n", lpqscBuf2->lpDescription);

    LocalFree(lpqscBuf);
    LocalFree(lpqscBuf2);

    return bSuccess;
}


Sample outputs for Netlogon and SNMP services:

Code:
F:\vc2005project\cplus\d.e.b.u.g>cplus
OpenSCManager(), Open a handle to the SC Manager database looks OK.
OpenService() is OK
QueryServiceConfig() is OK
QueryServiceConfig2() is OK

SNMP service configuration:
Type: 0x10
Start Type: 0x2
Error Control: 0x1
Binary path: C:\WINDOWS\System32\snmp.exe
Load order group:
Dependencies: EventLog
Start Name: LocalSystem
Description: Includes agents that monitor the activity in network devices and report to the network console workstation.

F:\vc2005project\cplus\d.e.b.u.g>cplus
OpenSCManager(), Open a handle to the SC Manager database looks OK.
OpenService() is OK
QueryServiceConfig() is OK
QueryServiceConfig2() is OK

NetLogon service configuration:
Type: 0x20
Start Type: 0x3
Error Control: 0x1
Binary path: C:\WINDOWS\system32\lsass.exe
Load order group: RemoteValidation
Dependencies: LanmanWorkstation
Start Name: LocalSystem
Description: Supports pass-through authentication of account logon events for computers in a domain.

F:\vc2005project\cplus\d.e.b.u.g>


Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C. Click Project menu->your_project_name Properties->Configuration Properties->C/C++->Advanced->Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged. Need to add Advapi32.lib (Advapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'Advapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.
To do: Getting a Windows service information
To show: Windows processes and services

_________________
_::kurt cobain::_


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Skin by Lucas Kane