Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Create the ATL COM DLL

Start Visual Studio .NET
Start a "New Project"
Select "Visual C++ Projects"
Select "ATL Project"
Click "OK"
Name the project "ATLDemo"
Click "OK"
As the default option is "Dynamic-link library (DLL)", Click "Next
Select Support MFC, click "Finish"

Add ATL COM Control

Right-click on the project name
Go to the "Add" option
Select "Add Class…"
Select "ATL COM Control"
Select Options as per Requirement, Click "Finish"

Add Method in ATL Contol

Right-click on the interface class named in Class View
Go to "Add"
Select "Add Method"
Enter method name
Check the "in" checkbox under the "Parameter attributes" section
Select BSTR for the parameter type
Name the parameter "bsName"
Click "Add"
Click "Finish"


Code to Register COM Object in Running Object Table

C++
STDMETHODIMP CPPName::FunctionName(BSTR bsName)
{ 
    ConInitialize(NULL);
    CComPtr<IBindCtx> pbc;
    CComPtr<IRunningObjectTable> pROT;
    CComPtr<IMoniker> spMoniker;
    CComPtr<IUnknown> spUnk;
    CComPtr<IEnumMoniker> IMonikerEnum;
    DWORD dWCookie;

    CreateBindCtx(0,&pbc);
    pbc->GetRunningObjectTable(&pROT);
    CreateItemMoniker(NULL,bsName,&spMoniker);
    QueryInterface(IID_IUNKNOWN, (void**)&spUnk);
    pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, spUnk, spMoniker, &dWCookie);
    CoUnInitialize();
}


Use Register COM Object in other COM Object


C++
STDMETHODIMP CPPName::FunctionName(BSTR bsName)
{
    ConInitialize(NULL);
    CComPtr<IBindCtx> pbc;
    CComPtr<IRunningObjectTable> pROT;
    CComPtr<IMoniker> spMoniker;
    CComPtr<IUnknown> spUnk;
    CComPtr<IEnumMoniker> IMonikerEnum;
    DWORD dWCookie;

    CreateBindCtx(0,&pbc);
    pbc->GetRunningObjectTable(&pROT);
    
    <Loop For Moniker, Checking Name Provided>
       hr = pROT->GetObject(pMon, &pInterface);
       <IF Get Name of Moniker> == bsName>
          hr = pInterface->QueryInterface(IID_IAttach, (void **)pAttach);
       <End IF>
    <Loop End>
}


"pAttach" object having access to object that is Registered.
By this "pAttach" having access to methods of object.

Problem:

I am having One Exe with Object that is registed.
In another exe I am using another object giving name that is already registered.
Now my task is from first exe when i m clicking left button then second exe should get message.

Is there any way to access register object's event in another exe??
Posted

1 solution

The simple answer is No. But a program can transmit a message to say "my button was clicked" and either send it to a specific process or broadcast it for anything to pick up

Starting point here[^]

However, if you are having specific problems with the code you posted, then there is a Comments section at the end of Boris' article
 
Share this answer
 
Comments
pooja work 30-Mar-14 8:03am    
Sir,
As you said that there is not a way to catch event click from register object exe to next exe with same class's another instance with register object's reference.

So i have 2 question in my mind
(1) Why we are using AddRef() as reference counting addition, it doesnt mean same objects reference we are in use? it doesnt mean register objects methods and events we can use?
(2) VBA is using even using COM Controls. is it with different mechanism?
CHill60 30-Mar-14 10:20am    
I'm not fully sure I've understood your questions but ... (1) yes you can use the object's methods and events but not from another exe.
(2) VBA uses the same mechanism. COM is COM - there are no differences based on language.
This article gives a nice introduction to COM Introduction to COM - What It Is and How to Use It.[^] but it was written several years ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900