Solved

Cocreateinstance() call

Posted on 2011-02-24
12
688 Views
Last Modified: 2013-12-04
Hello
Am new to COM concept, please help me understand the 4 lines about COM jargon written in c++.
Sham
0
Comment
Question by:mohet01
[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
  • 7
  • 3
  • 2
12 Comments
 

Author Comment

by:mohet01
ID: 34970153
Hello
Below are the 4 lines:
1) const IID IID_APP  =
{0xC2024555,0x3570,0x11D4,
{0xA5,0xC7,0x00,0x50,0xDA,0x63,0xC0,0xDD}
};
sham
0
 

Author Comment

by:mohet01
ID: 34970193
2)
const CLSID CLSID_APP   =
{0xC2024556, 0X3570,0X11D4,
{ 0xA5, 0xC7,0x00,0x50,0xDA ,0x63,0xC0, 0XDD}
};
SHAM
0
 

Author Comment

by:mohet01
ID: 34970272
3)
#define CLSCTX_SERVER
(CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER)
4)
hr = CoCreateInstance(CLSID_APP,NULL,CLSCTX_SERVER,IID_APP,(void **) pIsevprop);
Note: pIsevprop is just a pointer to the interface which is not clear for me.
Sham


0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:mohet01
ID: 34970316
Hello
I also see the error :
hr=-2147024882, sysmsg= not enough storage is available to complete this operation.

How do I approach this problem?

This code is running on windows 2003 machine

Sham
0
 
LVL 34

Accepted Solution

by:
sarabande earned 250 total points
ID: 34970526
the IID_APP and CLS_ID_APP are so-called GUID a 128-bit unique identifier. if you copied the code from somewhere you need to create new ids (use menu tools - create guid) or the uniqueness would not be valid.

the CLSCTX_SERVER is a defined macro  what most likely 'or's' some other integer constants where only one bit is set. by or-ing you get a bitmask which has all three bits set.

the CoCreateInstance "creates a single uninitialized object of the class associated with a specified CLSID" what means that it creates a valid handle for your COM app if it succeeds.

Sara
0
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 34971586
COM is challenging to understand, but it allows applications in different languages to talk to each other successfully (C++, Delphi, Perl, Visual Basic, .Net - you name it, it supports COM.)

COM is made up of Classes and Interfaces. A single class can have one or many interfaces. Classes are identified by their (CLSID) and interfaces are identified by the interface ID (IID).

In C++, a class is an object that inherits from one or more abstract base classes classes where all of the virtual functions are set =0). Each of the abstract interfaces corresponds to one interface. (It doesn't have to be this way, that's just typical.)

In C++, you create an object with "new". Most developers don't distinguish between the object and its interface. With COM, you use CoCreateInstance instead of new. An object has no default interface, so the IID_APP parameter says you want to start with the interface for the APP.

The CLSCTX_SERVER definition tells COM that the object can live anywhere - in a DLL, in an EXE, or even on another computer that may or may not be running Windows.

Also see the Wikipedi article at http://en.wikipedia.org/wiki/Component_object_model. The part you want is under "Technical Details".

0
 

Author Comment

by:mohet01
ID: 34971942
Hello
1) As per my latest update, I also an error in this problem, what is the meaning of this erroR?
2) sara, creating guids using menu tools, I did not get this point.
3)what exactly are we doing with this 4 lines conceptually?

Sham
0
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 34972569
1. This needs to be moved to a separate question. It's unrelated to your original questions.
2. Since you are working with existing code, Sara's comment was just "FYI" - it doesn't apply to what you are doing.
3. Conceptually you are creating a 128bit unique ID. Because it's 128 bits, you have to break it apart into pieces. You can't say (for example) 0x2cd823513259017af.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 34973123
you can create new guids like

 {0xC2024555,0x3570,0x11D4, {0xA5,0xC7,0x00,0x50,0xDA,0x63,0xC0,0xDD}}

in Visual Studio. use menu Tools -> Create GUID.

new guids are needed if you make a new app or a new com dll  and have new instances therefore. if you want to connect to an existing com object you need to use the GUID of that object.

the line 1 and 2 are defining a const IID global variable for later use.

the line 3 defines a macro that includes all three of

CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER

where each defines a possible 'context' as argument for CoCreateInstance. See help of CoCreateInstance.

the line 4 instantiates a new com object and returns a handle for further use.

The CoCreateInstance helper function provides a convenient shortcut by connecting to the class object associated with the specified CLSID, creating an uninitialized instance, and releasing the class object. As such, it encapsulates the following functionality:

CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF);
hresult = pCF->CreateInstance(pUnkOuter, riid, ppvObj)
pCF->Release();

Sara


0
 

Author Comment

by:mohet01
ID: 34985352
Hello
with respect to your point about new objects, CLSID_APP corresponds to which object and IID_APP corresponds to which object?
Sham
 
0
 

Author Closing Comment

by:mohet01
ID: 36152272
Thank you
0
 
LVL 34

Expert Comment

by:sarabande
ID: 36164885
CLSID_APP is key to the COM class of the application in registry.

IID_APP is an application specific identifier which might be used in the interface provided by the class.

Sara

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
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.

729 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