Determine the Process ID of the Client Process communicating with a COM RPC Server
Asked Answered
K

1

3

In a COM RPC Model, if the Server is running on a separate process, is it possible to know, the Process ID of the client Process communicating with the Server?

Use Case

I have an Out Process RPC Server which can receive request from one or more client process. Occasionally, the server needs to know the Client Process to write data back to the client address space using Write Process Memory.

enter image description here

Also Note, the API Signatures, the way the buffer is getting allocated and the APIs are getting called are beyond my control.

Kharkov answered 12/9, 2013 at 17:27 Comment(5)
Good question. I don't think you can get process identifier. Yes you can get security information such as caller, but it's not as detailed as caller process.Incorporator
@RomanR.: I somehow need the handle to the Client Process, what ever feasible ways possible to achieve my goal.Kharkov
That just isn't possible. Very much by design, that machine could be halfway around the world. Workaround number one is to make sure that you don't care. Much further down the list of hacks to consider is to simply let the server tell you. Or provide an interface that lets you do what you might want to do with the process ID. Or for the client to start the server explicitly instead of leaving it up to COM to do it for you and for the server to register its factories in the running object table.Esmerolda
@HansPassant: Can you please enlighten me on these two points assuming you are dealing with a noob Much further down the list of hacks to consider is to simply let the server tell you. Or provide an interface that lets you do what you might want to do with the process ID..Kharkov
@HansPassant: I finally adopted somewhat similar to what you proposed. Created an interface to register the process ID of the Client with the Server. As I am running STA, its not a problem for me when multiple clients are running. In any case as your reply was a comment there is nothing much I can do but just upvote your comment.Kharkov
S
1

I know that I'm almost 9 years late but just answering this in case anyone in the future needed it.

So there's a way to do this.... but through a private/undocumented COM interface, which is ICallingProcessInfo

Interface definition:

MIDL_INTERFACE("68C6A1B9-DE39-42C3-8D28-BF40A5126541")
ICallingProcessInfo : public IUnknown
{
public:
    virtual STDMETHOD(OpenCallerProcessHandle)(DWORD dwDesiredAccess, HANDLE* handle) = 0;
};

Usage:

HANDLE handle;
ComPtr<ICallingProcessInfo> callingProcessInfo; // ComPtr is from WRL, you can use the interface directly instead
CoGetCallContext(__uuidof(ICallingProcessInfo), (void**)callingProcessInfo.GetAddressOf());
callingProcessInfo->OpenCallerProcessHandle(PROCESS_QUERY_LIMITED_INFORMATION, &handle);

auto pid = GetProcessId(handle);

I tested this on a WinRT OOP Server but since WinRT Servers are just COM Servers it should work with normal COM Servers too

Shriver answered 10/9, 2022 at 0:8 Comment(1)
Where did you get the information about this interface? There is literally nothing but your two posts on stackoverflow found when googling. It works though, thank you!Teetotum

© 2022 - 2024 — McMap. All rights reserved.