How to create components with caller information

youqiang wu 21 Reputation points
2020-09-24T09:09:48.783+00:00

Currently app_a creates instance app_b by using CoCreateInstance, but how does app_b know which app created it?

Is there a function equivalent to CoCreateInstance and can pass input parameters?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,387 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,482 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 39,451 Reputation points
    2020-09-26T13:50:19.173+00:00

    As previously suggested, you can use the MFC support for Automation to add a COM object to your server with an interface that can be used for customization information passed by the calling client. Client applications call CoCreateInstance and request the customization interface. The COM runtime will start the server process and it can be configured through the customization interface BEFORE any documents have been opened.


1 additional answer

Sort by: Most helpful
  1. RLWA32 39,451 Reputation points
    2020-09-24T12:52:04.463+00:00

    Short answer is no.

    Now for the long answer -

    First, its important to understand that when app_a calls CoCreateInstance that it does not directly create an instance of app_b (out-of-process COM server). Rather, it is the COM runtime that instantiates app_b. If you use process explorer to examine the process tree you can see that app_a is NOT the parent process of app_b.

    Also, since a running COM Server can service more than one client, it is common that after app_b (the COM server) begins running to service app_a the same instance of app_b will also service app_c, app_d, etc. when they call CoCreateInstance. So although there can be multiple COM client processes, only one COM server process will be running.

    After a successful call to CoCreateInstance the client process will have an interface pointer for the instantiated COM object hosted by the server. Assuming that you are the author of the COM Server you can write an interface method that clients can call to pass data to the server. Generally, a running out-of-process COM Server is capable of identifying the process associated with an incoming call.