views:

738

answers:

4

What is the C++ equivalent to GetObject in JavaScript and VBScript?

The closest match I found to my question is:

http://codewiz51.blogspot.com/2008/06/vb-script-getobject-c-api-cogetobject.html

However the sample use an unexisting interface and asking for the IUnknown returns null. Did someone have an example that works?

+1  A: 

The article you linked to is correct. You may be providing the wrong interface ID, or the display name could be wrong. You should check the return value from the CoGetObject call.

1800 INFORMATION
A: 

If asking for IUnknown returns NULL, there is no object by that name. Each COM object implements IUnknown.

MSalters
A: 

Have you initialized COM before making any COM calls?

Look up CoInitializeEx.

Daniel Earwicker
+2  A: 

I figured out the issue. The object I wanted to access was

winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv

I mistakenly took \\ for an escapement. In C++ the correct query is :

::CoGetObject(L"winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv", NULL, IID_IUnknown, (void**)&pUnk);

Thank you :)

Emmanuel Caradec
That WMI object provides access to the registry. There are easier ways to do it in C++.
Roger Lipscombe