Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working with a plugin which is basically a loaded DLL. I may have both instances of the same application that loads the plugin. I want to be able to find out the process ID from the application that loaded the DLL.
Posted
Comments
Frankie-C 22-May-15 7:10am    
If I well understood your requirements calling the functions I indicated will give you the Id of the invoking process.
Is this what you were looking for? Or you required the process Id to do something else?

Using process (System.Diagnostics):
C#
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();

//Process id
int Id = currentprocess.Id;
...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-May-15 12:44pm    
I voted 4. I'm not sure that this is what the inquirer wanted, but this is not so important. What's more important is that his problem is probably some general confusion about processes and especially "DLLs", so the real answer would hav to provide some explanations. I tried to add some (credited your solution, of course). Please see Solution 2.
—SA
Frankie-C 22-May-15 7:06am    
Thanks Sergey.
I have not added any more explanation because what I have understood is that he created an application and a shared library. Because the library is included in assembly at runtime he want a system to identify the the process that is running the app that in turn referenced the shared code.
A Dll, managed or unmanaged, runs in the calling process, so getting current process Id give the referencing process. :-)
The question is not quite clear, but it's possible that you misunderstand processes and DLLs. First of all, in .NET, the concept of "DLL" is made insignificant. There are processes; and the central concept is assembly, and DLL or EXE are just some modules. With VS, an assembly has only one module, but there can be more, which is not directly supported by VS, but still can be done.

Now, assembly does not "know" about the processes. The same assembly (and hence its module) can be executed in different processes. No matter where your code is, in the entry assembly or not, it can request the current process the way Solution 1 tells you. It cannot give you anything unless you somehow communicate with other processes or some inter-process facilities, something collectively known as "IPC", and even then you would not need the ID. You can get more help if your explain your ultimate goals.

—SA
 
Share this answer
 
I just have to apologize, I didn't make myself clear.
I am developing addons which runs in parallel with the main program. and I don't have any way to get the process ID of the main application that opened the addon.
The problem is that there can be multiple instances of the main application running.
 
Share this answer
 
v2
Comments
Sascha Lefèvre 21-May-15 15:27pm    
This isn't a solution and the authors of solution 1 and 2 weren't notified of it. Please delete it and edit your original question to include this and if you want the authors of solution 1 and 2 to get notified, leave them a comment to their solutions.

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