Solved

Passing COM smart pointer as Reference

Posted on 2006-10-24
7
365 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse and java version isssue 2 39
Getting IP address 8 71
Create a path if not exists 7 69
Copy output image from TWindowsMediaPlayer 6 23
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

15 Experts available now in Live!

Get 1:1 Help Now