Click here to Skip to main content
15,913,669 members
Home / Discussions / COM
   

COM

 
QuestionHow to set the URL property of a WMP object in a Word .doc file? Pin
wangdave2-Apr-09 14:49
wangdave2-Apr-09 14:49 
QuestionLooking for links, books, articles on building .NET assemblies for use as COM servers Pin
cpkilekofp2-Apr-09 10:07
cpkilekofp2-Apr-09 10:07 
AnswerRe: Looking for links, books, articles on building .NET assemblies for use as COM servers Pin
Baltoro4-Apr-09 8:50
Baltoro4-Apr-09 8:50 
QuestionControl connection to EXE when calling CreateDispatch Pin
Tim Cook2-Apr-09 5:50
Tim Cook2-Apr-09 5:50 
AnswerRe: Control connection to EXE when calling CreateDispatch [modified] Pin
Baltoro4-Apr-09 10:00
Baltoro4-Apr-09 10:00 
GeneralSorry Pin
Baltoro4-Apr-09 11:11
Baltoro4-Apr-09 11:11 
GeneralRe: Sorry Pin
Tim Cook6-Apr-09 8:00
Tim Cook6-Apr-09 8:00 
GeneralI'll Try to Explain Clearly Pin
Baltoro9-Apr-09 10:32
Baltoro9-Apr-09 10:32 
Unfortunately, I don't use MFC, because it hides the underlying implementation, often making it difficult to make alterations without problems.

But, I'll try to explain clearly. If your 'My Program' is instantiated by a user, and your 'My Program' calls CoRegisterClassObject (this is the default, and your MFC implemenatation may actually do this), you can locate this running instance with the creation of a Class Moniker (presumably, implemented in your 'MyManager'). The Class Moniker must be initialized with a Binding Context to work properly. And, you pass the CLSID of a COM class exported by 'MyProgam' to IMoniker->BindToObject (which is where all the work is done), also, passing the IID_IUnknown Interface ID (this is a system IID, and you must link to uuid.lib, for the symbol to be exported correctly) as the REFIID parameter, the function should return the IUnknown interface pointer to the CLSID from 'MyProgram' that you supplied to the CreateClassMoniker function.
(From Matt Pietrek, "If you've used OLE, COM, or ActiveX, you probably remember that there are .LIB files that are used for predefined class IDs (CLSIDs) and interface IDs (IIDs). Both CLSIDs and IIDs are forms of GUIDs, which are 16-byte unique values".)
IMoniker->BindToObject returns a COM typed interface pointer (to the IUnknown of whatever interface you passed to CreateClassMoniker. This can then be used with QueryInterface.) You can then call, IMoniker->GetObject to determine if the instance is already running. That's the basic idea.
To intialize the Binding Context, call, CreateBindCtx, and then, pass it a BIND_OPTS2 structure (which must also be initialized). You must call, GetBindOptions and then, SetBindOptions to do this correctly.

Here's what the code might look like:

IUnknown *ppvUnknown  = NULL ;    
CLSID YourClassCLSID ;  //  This is your Interface CLSID from 'MyProgram'    
BIND_OPTS2 BindOptns ;    
IBindCtx *BindContx = NULL ;   
IMoniker *ClsMoniker = NULL ;   
ZeroMemory (BindOptns, sizeof (BIND_OPTS2)) ;   
HRESULT hrBnd = CreateBindCtx (0, &BindContx) ;       
if (hrBnd != NULL)    
{    
BindContx->GetBindOptions (&BindOptns) ;   
BindOptions.cbStruct = sizeof (BIND_OPTS2) ;   
BindOptions.dwClassContext = CLSCTX_ALL ;  //  COM will select the most effecient type.       
BindContx->SetBindOptions (&BindOptns) ;   
HRESULT hrMonikr = CreateClassMoniker (YourClassCLSID, &ClsMoniker) ;   
if (FAILED(hrMonikr))    
{    
//  Release any valid COM interface pointers and exit   
}    
HRESULT hrBndObj = ClsMoniker->BindToObject (BindContx, NULL, IID_IUnknown, reinterpret_cast<void**>(&ppvUnknown)) ;    
if ((SUCCEEDED(hrBndObj) && (ppvUnknown != NULL))    
{    
HRESULT hrRunning = ClsMoniker->IsRunning (BindContx, NULL, NULL) ;   
//   hrRunning is either S_OK or S_FALSE. 
}    
else    
{   
//  FAILED, call Release on existing COM interfaces.
//  If you call Release on an interface that is a NULL pointer, the application will crash.       
ClsMoniker->Release () ;   
BindContx ->Release () ;    
ppvUnknown ->Release () ;    
}    
}    

'MyManager' can use this technique to determine if an instance of 'MyProgram' is already running, and instantiate another one for it's exclusive use. You should read up on exactly what is the default COM procedure for the MFC class that you are using.
However, COM does NOT allow you to implement IUnknown, QueryInterface to return a NULL interface pointer, conditionally, if it has already returned a valid interface pointer. In other words, you cannot rewrite the QueryInterface implementation of 'MyProgram' to return a NULL pointer based on some determination, unless it ALWAYS does that.
Also, you might want to read this article: The COM Macro-Architecture Topology[^], which explains the default COM behavior with activation requests.
GeneralRe: I'll Try to Explain Clearly Pin
Tim Cook17-Apr-09 5:31
Tim Cook17-Apr-09 5:31 
Questionactivex loading from tomcat 6 Pin
aysl2-Apr-09 5:28
aysl2-Apr-09 5:28 
QuestionTroubles embedding IE8 WebBrowser control Pin
Vlasta_1-Apr-09 5:11
Vlasta_1-Apr-09 5:11 
QuestionUsing COM to receive cell updates from excel in VB .NET Pin
mmiller0391-Apr-09 5:07
mmiller0391-Apr-09 5:07 
Questionhow to get mail address in outlook express Pin
nikhil3131-Mar-09 22:25
nikhil3131-Mar-09 22:25 
QuestionC++ COM EventHandler: How to catch COM events from Delphi COM Server / VC6 Pin
ipforce30-Mar-09 4:57
ipforce30-Mar-09 4:57 
AnswerRe: C++ COM EventHandler: How to catch COM events from Delphi COM Server / VC6 Pin
Jonathan Davies2-Apr-09 5:32
Jonathan Davies2-Apr-09 5:32 
GeneralRe: C++ COM EventHandler: How to catch COM events from Delphi COM Server / VC6 Pin
ipforce2-Apr-09 6:08
ipforce2-Apr-09 6:08 
GeneralRe: C++ COM EventHandler: How to catch COM events from Delphi COM Server / VC6 Pin
ipforce2-Apr-09 6:59
ipforce2-Apr-09 6:59 
QuestionUpdating Resource Pin
sumedh_code30-Mar-09 0:55
sumedh_code30-Mar-09 0:55 
QuestionAccess Denied in VC++ COM Pin
RevathiRamakumar30-Mar-09 0:20
RevathiRamakumar30-Mar-09 0:20 
AnswerRe: Access Denied in VC++ COM Pin
Roger Stoltz30-Mar-09 3:35
Roger Stoltz30-Mar-09 3:35 
GeneralRe: Access Denied in VC++ COM [modified] Pin
RevathiRamakumar30-Mar-09 19:39
RevathiRamakumar30-Mar-09 19:39 
AnswerRe: Access Denied in VC++ COM Pin
Roger Stoltz30-Mar-09 22:17
Roger Stoltz30-Mar-09 22:17 
GeneralRe: Access Denied in VC++ COM Pin
RevathiRamakumar30-Mar-09 22:46
RevathiRamakumar30-Mar-09 22:46 
AnswerRe: Access Denied in VC++ COM Pin
Roger Stoltz30-Mar-09 23:04
Roger Stoltz30-Mar-09 23:04 
GeneralRe: Access Denied in VC++ COM Pin
RevathiRamakumar30-Mar-09 23:10
RevathiRamakumar30-Mar-09 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.