Solved

Passing COM smart pointer as Reference

Posted on 2006-10-24
7
364 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now