Solved

Passing COM smart pointer as Reference

Posted on 2006-10-24
7
368 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 115
Change local server setting in php 6 94
How to print into std::string like sprintf using C++11? 12 78
max float value 3 51
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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