Solved

Passing COM smart pointer as Reference

Posted on 2006-10-24
7
367 Views
Last Modified: 2013-12-14
Hi

           I am having A smart pointer in main application and i want to pass the reference to a function in another class

which is instantiated in the main application. I am a beginner in COM and i dont know how to do this, i am posting the

sample code i experimented with. Could any one tell me how to do this? and where i am going wrong.

void main
{
      CoInitialize(NULL);
      
      IUtilPtr spUtil;
      spUttil.CreateInstance(__uuidof(Util)); //Util is a CoClass inside COM

      myclass mc[10];

      for(int i=0;i<10;i++)
      {
            mc[i].SetInterface(spUttil);
            mc[i].MakeUtilities();
      }

/*
      only one instance of Util should be instanciated for the whole application
      but Many Instances of Utilities will be created based on Util.
*/
      Sleep(10000);

      CoUninitialize();
}

class myclass
{
private:
      IUtilPtr spMyUtil;
      IUtilitiesPtr spMyUtilities;
public:
      void myclass()
      {
            spMyUtil.CreateInstance(__uuidof(Util));  //Util is a GUID of a CoClass inside the COM
            spMyUtilities.CreateInstance(__uuidof(Utilities)); //Utilities is a GUID of another CoClass

inside the COM
      }
      void SetInterface(IUtilPtr MyUt)
/*
I dont get any compile error but the actual ref of the Interface pointer is not
copied to the local smart pointer of the function
*/
      {
            spMyUtil = MyUt;
      }
      void MakeUtilities()
      {
            spMyUtilities =  (IUtilitiesPtr) spMyUtil -> CreateUtility();
            //Function CreateUtility Creates a Utilities object
            //since the return type is IUnknownPtr  it  is casted to IUtitlitiesPtr before it is assigned to
            //spMyUtilites
      }

};


Thanx in advance.
Regards
Shiju S N
0
Comment
Question by:Shiju Sasidharan
  • 4
7 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 17796122
    IUtilPtr spUtil;
     spUttil.CreateInstance(__uuidof(Util)); //Util is a CoClass inside COM

     Should it be spUtil instead of "spUttil"? I guess its a typo.

     Best Regards,
     DeepuAbrahamK
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17796207
Passing smart pointer between functions is real pain. There are three options:

void SetInterface(IUtilPtr MyUt)
void SetInterface(IUtilPtr& MyUt)
void SetInterface(IUtilPtr* MyUt)

I don't like any of them and prefer to pass plain interface pointer:

void SetInterface(IUtil* MyUt)

Plain interface pointer can be used to call interface members exactly like smart pointer, but it doesn't change smart pointer reference counter. This may be advantage and disadvantage. If you want to pass ownership of created COM component to other class, it is better to pass smart pointer. In your case, I would use plain pointer.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 17796318
IUtilPtr spUtil;

write this out side the main(), which should be global.
It should work.

Best Regards,
DeepuAbrahamK
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 17796596
Hi Shiju,
Oops I think I have overlooked the problem !! Sorry if I misslead you ! If it is inter thread you need to use CoMarshalInterThreadInterfaceInStream and do unmarshal it.But here in your case you can follow what AlexFM said and you could use Attach() function to attach it to smart pointer.

Best Regards,
DeepuAbrahamK
0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 17797760
Hi
            Deepu thanx for pointing out the mistake but that was a mistake in posting alone and not in the real project. I told u tht i compiled it successfully... The thing is i get an error "abnormal program termination" when i try to call MakeUtilities();

spMyUtilities =  (IUtilitiesPtr) spMyUtil -> CreateUtility(); <--- i think this is the error cause when i comment tht no error

            AlexFM could u please tell me how the following code is to be modified to achive that using any of the method suggested by u preferably The one with smart pointer one coz i have a lot of stuff to be done inside myclass using this object hence if i use an ordinary pointer it wont be easy to manage memory it will make the whole thing tedious. i am a beginner in COM and there arnt enough tutorials or help on this topic...

           It will be really helpfull if any one can tell me an online page which explains the concept very clearly...

Regards
Shiju S N
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 17798858
Hi Shiju,

 void myclass()
 {
          spMyUtil.CreateInstance(__uuidof(Util));  //Util is a GUID of a CoClass inside the COM
          spMyUtilities.CreateInstance(__uuidof(Utilities));
 }

I think you should change it to
myclass()
{}
I don't think you should have a *void* in front of ctor.Also I don't think you should do CreateInstance() inside the ctor. In any case you are passing it as a reference to the class.

One more thing,Are you able to try with IUtil* as AlexFM mentioned?Also check if the
CoCreateInstance() inside the main() is working fine or not.

Best Regards,
DeepuAbrahamK


0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question