?
Solved

How to get whether any Windows Services installed in a drive

Posted on 2009-02-11
5
Medium Priority
?
323 Views
Last Modified: 2013-12-03
Hi,

I want to get the drives which contains Windows services. For example if apache is installed as windows service in E:\ then i have to show E:\ which contains windows services i.e., without specifying the service name(apache).

I using C++.

Thanks in advance.

Regards,
Prince
0
Comment
Question by:ilava
  • 3
5 Comments
 
LVL 7

Expert Comment

by:johnnash1180
ID: 23611220
You can enumerate the registry keys under the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" path and check "ImagePath" value to get the executable path. Using this value, you can find out whether any service was installed in the given drive or not.
0
 
LVL 7

Expert Comment

by:johnnash1180
ID: 23611276
If you wish to get the services list programmatically, you ca use the following APIs:
OpenSCManager
OpenService
EnumServicesStatus
0
 
LVL 3

Author Comment

by:ilava
ID: 23611342
this is nt what i want.

without specifying the service name you cant get the imagepath value. imagepath value differs for each service.  I mentioned already "without specifying the service name".  I think you are getting confused ... let me explain with example see the attached code


bool IsWindowsServicesInstalledInThisDrive(string driveName)
{
  bool isAvail = false;
 
  code here ( this is what i want )
 
  retrun isAvail; 
}
 
main()
{
bool isWinServiceAvail = IsWindowsServicesInstalledInThisDrive(E:\);
}

Open in new window

0
 
LVL 7

Expert Comment

by:johnnash1180
ID: 23612077
Hmmmm.

As I mentioned in my post, you can write code to enumerate the services as below:

bool IsWindowsServicesInstalledInThisDrive(string driveName)
{
  bool isAvail = false;
 
  OpenSCManager(....)
 
  for(int i=0; i<totalServices; i++)
  {
    OpenService(i....);
    Get Service Configuration
    Get Service Configuration . Binary path
    If Service Binary Path start with driveName
    {
         isAvail = true;
         break;
    }
  }
 
  retrun isAvail;
}
 
main()
{
bool isWinServiceAvail = IsWindowsServicesInstalledInThisDrive(E:\);
}

Clear?

Anyway, you need to explore how to use the OpenSCManager, OpenService, EnumServicesStatus functions.

Refer http://msdn.microsoft.com/en-us/library/ms684330(VS.85).aspx, http://www.chami.com/tips/delphi/040698D.html URLs.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 23612170
>>without specifying the service name you cant get the imagepath value

That's not quite true, you can enumerate all keys without knowing their names - just like e.g. http://www.codeproject.com/KB/system/installed_programs.aspx ("
How to get a list of installed applications") demonstrates it. E.g.
#include <windows.h>
#include <tchar.h>
 
#include <string>
#include <list>
using namespace std;
 
#define SVC_KEY_LEN MAX_PATH
 
struct SvcEntry {
 
    string sName;
    string sPath;
 
};
 
list<SvcEntry> lstServices;
 
void BuildServiceList(list<SvcEntry>& lst)
{
  HKEY hKey;
  if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("CurrentControlSet\\Services"), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
   return;
 
  DWORD dwIndex = 0;
  LONG lRet;
  DWORD cbName = SVC_KEY_LEN;
  TCHAR szSubKeyName[SVC_KEY_LEN];
 
  while ((lRet = ::RegEnumKeyEx(hKey, dwIndex, szSubKeyName, &cbName, NULL,
   NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS)
  {
   // Do we have a key to open?
 
   if (lRet == ERROR_SUCCESS)
   {
    // Open the key and get the value
 
    HKEY hItem;
    if (::RegOpenKeyEx(hKey, szSubKeyName, 0, KEY_READ, &hItem) != ERROR_SUCCESS)
     continue;
    // Opened - look for "DisplayName"
 
    TCHAR szImagePath[SVC_KEY_LEN];
    DWORD dwSize = sizeof(szImagePath);
    DWORD dwType;
 
    if (::RegQueryValueEx(hItem, _T("ImagePath"), NULL, &dwType,
     (LPBYTE)&szImagePath, &dwSize) == ERROR_SUCCESS)
    {
        SvcEntry se;
 
        se.sName = szSubKeyName;
        se.sPath = szImagePath;
 
        lst.push_back(se);
    }
    ::RegCloseKey(hItem);
   }
   dwIndex++;
   cbName = IS_KEY_LEN;
  }
  ::RegCloseKey(hKey);
 }
 
bool IsWindowsServicesInstalledInThisDrive(string driveName)
{
  bool isAvail = false;
 
  BuildServiceList(lstServices);
 
  for ( list<SvcEntry>::iterator i = lstServices.begin(); i != lstServices.end(); ++i) {
 
    if (i->sPath[0] = driveName[0]) return true;
  }
 
  retrun isAvail; 
}
 
int main()
{
 
 
bool isWinServiceAvail = IsWindowsServicesInstalledInThisDrive("E:\\");
 
return 0;
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question