?
Solved

Passing COM smart pointer as Reference

Posted on 2006-10-24
7
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
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.
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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