I have a Windows Service that is exposing some COM functions to be invoked via IDispatch Invoke. There are different clients (Windows applications) that at any moment can Invoke any of these functions. I need to introduce a change in the Windows Service that does not require any change in the clients (I cannot modify them). I need to differentiate (like an ID) which application is Invoking a function. NOTE: I do not need to know the exact client application, just to distinct between the different clients in the current session is enough.
For example: Let's say there are two clients C1 and C2. When C1 Invokes function F1 in the Windows Service (WS), WS knows that every time that (during the current session) a client X1 (note I said X1, because WS does not need to know exactly that C1 Invoked F1, just to assign an ID to C1 like a PID~process ID~). So, now C3 Invokes F2, WS knows that a client X2 (which is different that X1) invoked F2. And, then again C1 Invokes FX (any COM function exposed by WS) WS can distinct that X1 invoked FX, instead that a client Xn (like X2) Invoked FX.
Is there anything that can help me to achieve this situation without the need of an ID argument from the clients?
My first thought would be to simply compare the client IUnknown addresses in the server. If two calls have the same IUnknown they are using the same proxy and should be coming from the same client.
You can also use CoGetCallerTID to obtain a client identifier of a sort. This COM API function returns an ID for the caller's apartment. Typically this is the thread ID of the thread running in the callers process. This may not be guaranteed to be unique among all callers as you would need the process ID as well to ensure this.
Digging around in the other COM API functions may reveal some other possibilities. CoGetObjectContext looks like it might be useful if the properties made available via the IContext interface can be determined. The documentation isn't too forthcoming about those.