• C

n00b question about declaring a variable be a pointer to a structure

I'm new to C and trying to mess around with some of the functions from windows.h

I'm trying out this one:

BOOL EnumServicesStatusEx(
  SC_HANDLE hSCManager,
  SC_ENUM_TYPE InfoLevel,
  DWORD dwServiceType,
  DWORD dwServiceState,
  LPBYTE lpServices,
  DWORD cbBufSize,
  LPDWORD pcbBytesNeeded,
  LPDWORD lpServicesReturned,
  LPDWORD lpResumeHandle,
  LPCTSTR pszGroupName

I've declared my variables as:
DWORD            dwServiceType = SERVICE_WIN32,
            dwServiceState = SERVICE_STATE_ALL,
                      chBufSize = 30720,
            dwResumeHandle = 0,
            dwDesiredAccess = SC_MANAGER_ENUMERATE_SERVICE,
LPCTSTR            pszGroupName = NULL;

But i'm having a problem with the lpServices variable which should be a pointer to the buffer that receives the status information.

My question is how do I specify a variable to contain this information ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

so lpServices is of type LPBYTE, which means pointer to a byte.  therefore, above your function call, declare a variable of type LPBYTE, such as:

LPBYTE lpbyteFoo;

then pass the address of lpbyteFoo to the function call.  (&lpbyteFoo)

note, your pszGroupName is Null, so anything writing to that will crash...you need to do a similar thing as what I describe above...declare an array of char's and send in the address of that.

lpServices is a pointer to the buffer that receives the information about the enumerated service on output.
The buffer will contain a number of ENUM_SERVICE_STATUS_PROCESS structures.
You need to declare a buffer big enough to hold the returned information. Then pass the address of this buffer to the function call.

For example :

BYTE Services[1000] ;

and pass &Services as lpServices parameter.

If the buffer is not big enough, the function call will fail, GetLastError will return ERROR_MORE_DATA,
and you'll get the size needed in pcbBytesNeeded parameter.

For more detail on how to know the exact size needed and what the buffer contains, see :


You can pass NULL to pszGroupName. In that case, there is no filter on services group name, and you get an enumeration
of all services whatever group they belong to.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.