Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ COM
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 3-Apr-13 6:38am
tasumisra1.2K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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'.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 ...
 
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100