?
Solved

Win32 api, remote admin type stuff

Posted on 1999-11-08
3
Medium Priority
?
252 Views
Last Modified: 2012-05-04
I'm looking at writing some C++ code to help with remote administration of NT servers.  I am familar with C, and not sure if I need to learn more about C++ in order to develope some utilities.  I have some Perl scripts that I would like to replace with custom C/C++ routines for things like:  retrieving a list of drives and free space on a remote NT server; check remote servers services to verify "automatic/started" status.  I know there is a resource kit utility for the service stuff, but I would still like to try to develop some of these for myself.  Sample code would be great, but even some pointers on "read up on rpc", "read up on dcom", "no, you can't do this without some additional tools" would help.  Thank you.
0
Comment
Question by:b_cros
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2192472
At least regarding the services this can be done easily:

The services API (and 'OpenSCManager()' in particular) take a machine name as a parameter that can be used to specify the target computer.

But I also don't have a clue on the drive space issue...
0
 
LVL 1

Accepted Solution

by:
Zizzer earned 800 total points
ID: 2192565
Here's a C++ snippet you can use to start a service on a remote computer.  Stopping a service or checking status is very similar.

class AutoSCHandle {
public:
      AutoSCHandle(SC_HANDLE handle);
      ~AutoSCHandle();
      operator SC_HANDLE() const;
protected:
      SC_HANDLE m_handle;
};

AutoSCHandle::AutoSCHandle(SC_HANDLE handle)
      : m_handle(handle)
{
}

AutoSCHandle::~AutoSCHandle()
{
      BOOL flag = CloseServiceHandle(m_handle);
      if (!flag)
            THROW(HRESULT_FROM_WIN32(GetLastError()));
}

AutoSCHandle::operator SC_HANDLE() const
{
      return m_handle;
}

void StartService(LPCTSTR computer, LPCTSTR service)
{
            // open service control manager on target machine
      AutoSCHandle hSCM = OpenSCManager((LPCTSTR) computer, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
      if (hSCM == NULL)
            THROW(HRESULT_FROM_WIN32(GetLastError()));

            // open the requested service
      AutoSCHandle hService = OpenService(hSCM, service, SERVICE_START | SERVICE_QUERY_STATUS);
      if (hService == NULL)
            THROW(HRESULT_FROM_WIN32(GetLastError()));

            // start the service
      BOOL flag = StartService(hService, 0, NULL);
      if (!flag)
            THROW(HRESULT_FROM_WIN32(GetLastError()));

            // wait until the service is finished starting
      SERVICE_STATUS status;
      do {
            Sleep(100);
            flag = QueryServiceStatus(hService, &status);
            if (!flag)
                  THROW(HRESULT_FROM_WIN32(GetLastError()));
      } while (status.dwCurrentState == SERVICE_START_PENDING);
}
0
 

Author Comment

by:b_cros
ID: 2208815
The information provided was excellent, but I didn't really get an answer to the remote disk information.  Thanx.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

592 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