Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi Experts,

I wanted to know if there is any way to instantiate a COM component other than cocreateInstance and its variant.

Regards,
Vikas Mishra
Posted

There's no really sensible or safe way but there is a way. Essentially you can do what CoCreateInstance does by yourself. There are a number of steps and I don't know them all by heart but they will include.
Load the Dll with LoadLibrary
Get one or more function pointers to the functions exported by the standard IClassFactory derived factory class using GetProcAddress
Call these entry points to create a class factory.
Use the class factory to create an instance of your CoClass
Free up everything in the reverse order from how you acquired it.

If you need more details you could look at the implementation of CoCreateInstance within the source of the Wine project which builds on Linux or under Cygwin and emulates the Win32 API including COM.

There are other open source COM implementations for non Windows platforms which will have CoCreateInstance implementations although they may be too simplified to help you much with bypassing the real thing on Windows.
 
Share this answer
 
There are some 'ufficial ways' for creating a COM objects (Matthew covered well the 'unofficial ways'). You may use CoGetClassObject and the call the IClassFactory::CreateInstance.
MSDN's Instance Creation Helper Functions[^] shows other possibilities.
However, I don't know if you consider the above just CoCreatInstance 'variants'.
 
Share this answer
 
If the COM DLL has to be treated as a COM object, then see solutions #1 and #2. If the DLL does not have to be treated as COM, then you could simply link to it and call the COM interfaces via conventional calls. You would still use the same marshalling, return code and exception handling techniques, but you would call the APIs directly instead of going through smart pointers etc.
 
Share this answer
 
In additionally

How to initialize object from existing DLL without registering see my answer here: importing com dll dynmaically[^]
The CoCreateInstance and CoCreateInstanceEx used if case you object registered in a system or on remote PC.
Once your object is OLE control or document which you want to link in your app you should use OleCreate API.
Other helper APIs for instansing object is CoCreateInstanceFromFile and CoCreateInstanceFromIStorage
Along with CLSID, objects can be identified as a Class Monikers and can be easy activated this way.
In case COM object is registered in Running Objects Table (for example you implementing COM service) you can obtaing that object instance directly from ROT - it is singleton access (so you use only once instance).

Regards,
Maxim.
 
Share this answer
 
Thank you so much friends..well it was asked in one of the interview and i had answered the person that we can use co create instance or all helper methods invoked by cocreateinstance seprately ...

C#
CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF);
hresult = pCF->CreateInstance(pUnkOuter, riid, ppvObj)
pCF->Release();


but it seems he was not satisfied..

Not i problem interviewer sometime asks weird question..:P

Thank you so much guys..

Regards,
Vikas Mishra
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900