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 8241155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:44:27+00:00 2026-06-07T20:44:27+00:00

I got the process id of the running service using the code below as

  • 0

I got the process id of the running service using the code below as well as the process name, but i all i really want is the service name/key.Is there a way to get that from either the process id or the process name? Using c++

    DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;

if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
{
    return 1;
}


// Calculate how many process identifiers were returned.

cProcesses = cbNeeded / sizeof(DWORD);

// Print the name and process identifier for each process.

for ( i = 0; i < cProcesses; i++ )
{
    if( aProcesses[i] != 0 )
    {
        PrintProcessNameAndID( aProcesses[i] );
    }

}

and..

void tt_coreutils_ns::PrintProcessNameAndID( DWORD processID )
{
 TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

// Get a handle to the process.

HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
    PROCESS_VM_READ,
    FALSE, processID );

// Get the process name.

if (NULL != hProcess )
{
    HMODULE hMod;
    DWORD cbNeeded;

    if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
        &cbNeeded) )
    {
        GetModuleBaseName( hProcess, hMod, szProcessName, 
            sizeof(szProcessName)/sizeof(TCHAR) );
    }
}

// Print the process name and identifier.

_tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );

// Release the handle to the process.

CloseHandle( hProcess );

}

updated code

    DWORD pId=GetCurrentProcessId();
SC_HANDLE hSCM    = NULL;
PUCHAR  pBuf    = NULL;
ULONG  dwBufSize   = 0x00;
ULONG  dwBufNeed   = 0x00;
ULONG  dwNumberOfService = 0x00;


LPENUM_SERVICE_STATUS_PROCESS pInfo = NULL;

hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_CONNECT);

if (hSCM == NULL)
{
    printf_s("OpenSCManager fail \n");
    return 0xffff0001;
}

EnumServicesStatusEx(
    hSCM,
    SC_ENUM_PROCESS_INFO,
    SERVICE_WIN32, // SERVICE_DRIVER
    SERVICE_STATE_ALL,
    NULL,
    dwBufSize,
    &dwBufNeed,
    &dwNumberOfService,
    NULL,
    NULL);

if (dwBufNeed < 0x01)
{
    printf_s("EnumServicesStatusEx fail ?? \n");
    return 0xffff0002;
}

dwBufSize = dwBufNeed + 0x10;
pBuf  = (PUCHAR) malloc(dwBufSize);

EnumServicesStatusEx(
    hSCM,
    SC_ENUM_PROCESS_INFO,
    SERVICE_WIN32,  // SERVICE_DRIVER,
    SERVICE_ACTIVE,  //SERVICE_STATE_ALL,
    pBuf,
    dwBufSize,
    &dwBufNeed,
    &dwNumberOfService,
    NULL,
    NULL);

pInfo = (LPENUM_SERVICE_STATUS_PROCESS)pBuf;
for (ULONG i=0;i<dwNumberOfService;i++)
{
    cout<<"display name "<<pInfo[i].lpDisplayName<<"\t service name: ";
    cout<< pInfo[i].lpServiceName<<"\tid: "<<pInfo[i].ServiceStatusProcess.dwProcessId<<endl<<endl;

    if(pId==pInfo[i].ServiceStatusProcess.dwProcessId)
    {
        cout<<pInfo->lpServiceName;
    }
}
  • 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-06-07T20:44:29+00:00Added an answer on June 7, 2026 at 8:44 pm

    Enumerate all the services using EnumServicesStatusEx (pass SERVICE_WIN32 as a service type). In the output, you’ll get ENUM_SERVICE_STATUS_PROCESS structs that contain service name and another SERVICE_STATUS_PROCESS struct, which has DWORD dwProcessId field.

    This way you can map process id to a service name/key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have running process but it's executable file has got deleted. If I try
I'm running a process from inside a windows service using: ProcessStartInfo processStartInfo = new
I've got a WCF service using a HttpBinding. The service is running in a
We've got a process currently which causes ASP.NET websites to be redeployed. The code
Im the process of learning C++. I got stuck debugging some of my code
I got a geolocation service. When force killed using some Task Killing apps, in
I've got two services running in the same process, hosting the same classes, on
I've got a WCF service running on a Windows 2003 Server, which I wrote
Quick question here, i've got a process running that grabs RSS feeds and adds
I've got a simple little winforms app that performs a long running process on

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.