Q:

c++ CoCreateInstance method return "No Such Interface supported"

I am getting the error "No such interface supported" when I call the CoCreateInstance() in dll. I am trying to create an instance of IGroupPolicyObject. When I run this sample code snippet through exe it returns S_OK but if I run through DLL then I get an error.
I have added the sample code snippet.
HRESULT hr =S_OK;
IGroupPolicyObject* pLGPO = NULL;

// MSVC is finicky about these ones => redefine them
const IID my_IID_IGroupPolicyObject = 
 { 0xea502723, 0xa23d, 0x11d1, {0xa7, 0xd3, 0x0, 0x0, 0xf8, 0x75, 0x71, 0xe3} };

const IID my_CLSID_GroupPolicyObject = 
 { 0xea502722, 0xa23d, 0x11d1, {0xa7, 0xd3, 0x0, 0x0, 0xf8, 0x75, 0x71, 0xe3} };

GUID ext_guid = REGISTRY_EXTENSION_GUID;

// This next one can be any GUID you want
GUID snap_guid = { 0x3d271cfc, 0x2bc6, 0x4ac2, {0xb6, 0x33, 0x3b, 0xdf, 0xf5, 0xbd, 0xab, 0x2a} };

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

// Create an instance of the IGroupPolicyObject class
hr = CoCreateInstance(my_CLSID_GroupPolicyObject,
                      NULL,
                      CLSCTX_INPROC_SERVER,
                      my_IID_IGroupPolicyObject,
                      (LPVOID*)&pLGPO
                     );

if (SUCCEEDED(hr))
{   
    hr = pLGPO->OpenLocalMachineGPO( GPO_OPEN_LOAD_REGISTRY);
}

Please tell me am I doing anything wrong?

A:

E_NOINTERFACE result of CoCreateInstance call means that the API successfully created the instance in question, but failed to obtain the interface requested.
This certainly can happen when the interface is simply not available on the object (not implemented at all), however if you see it succeeding in some scenarios and failing in other, the likely reason is that the interface is there but it cannot pass through apartments and marshaling. If your code is in one apartment, and the server is in another (esp. the server is STA and your code is MTA), then it takes marshaling to connect the two. Which in turn assumes that the interface can be marshaled using custom proxy/stub pair or universal. 
You need to either use interfaces that have proxy/stubs associated and available, or you need to move your code into the same COM apartment such as background thread initialized respectively. The COM server you are trying to use is for STA. So you need a background thread initialized as STA, then you instantiate the object there and pass data to original caller in a non-COM way (or COM but not via IGroupPolicyObject).
Your code is doing CoInitializeEx but it does not check for result. You should be seeing RPC_E_CHANGED_MODE there.

