I'm curious if it's a good idea to use a union when accessing Win32 APIs that return variable length structures, to avoid manually managing the allocated memory.
Consider the following:
void displayServices(std::wostream& log, std::tr1::shared_ptr<void> manager, LPENUM_SERVICE_STATUS currentServiceToDisplay, DWORD number, bool whitelist)
{
static union //Don't care about being thread safe
{
QUERY_SERVICE_CONFIG svcConfig;
unsigned char bufferSizer[8000];
};
for(DWORD idx = 0; idx < number; currentServiceToDisplay++, idx++)
{
DWORD garbage = 0;
std::tr1::shared_ptr<void> currentServiceHandle(
OpenService(reinterpret_cast<SC_HANDLE>(manager.get()), currentServiceToDisplay->lpServiceName, SERVICE_QUERY_CONFIG),
serviceCloser);
QueryServiceConfig(reinterpret_cast<SC_HANDLE>(currentServiceHandle.get()),
&svcConfig, 8000, &garbage);
//Use svcConfig for something here.
}
}
Are there any major issues with this?