Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I am writing a piece of code that employs ObjectAPI of an application. The API documentation has examples showing how to dynamically run a new instance of the application. The examples show:
C#
string pathToExe = System.IO.Path.Combine(System.Environment.GetEnvironmentVariable("PROGRAMFILES"), "CompanyName", "ProductName", "app.exe");
System.Reflection.Assembly AppAssembly = System.Reflection.Assembly.LoadFrom(pathToExe);
someObject theObject= (someObject)AppAssembly.CreateInstance("someObjectTypeString");
theObject.ApplicationStart();


the last line causes the application to start and further manipulation to the application is provided through objects available inside "theObject".
The object "theObject" is actually responsible for actions like starting and exiting the application.

My intention is: instead of creating a new instance of application (i.e. starting a new session of it) get handle of an already running one and perform custom actions on it.
I guess the "AppAssembly.CreateInstance()" method must be replaced by some other one such as "getInstance()" but my search could not bring any results.

I would be thankful if someone could help me.
Posted
Updated 17-Dec-13 4:42am
v2

1 solution

Hi Ron,

I hope that these chunk resolve the problem.

C#
public static Process RunningInstance()
     {
         Process current = Process.GetCurrentProcess();
         Process[] processes = Process.GetProcessesByName(current.ProcessName);

         //Loop through the running processes in with the same name
         foreach (Process process in processes)
         {
             //Ignore the current process
             if (process.Id != current.Id)
             {
                 //Make sure that the process is running from the exe file.
                 if (Assembly.GetExecutingAssembly().Location.
                      Replace("/", "\\") == current.MainModule.FileName)
                 {
                     //Return the other process instance.
                      return process;

                 }
             }
         }
         //No other instance was found, return null.
         return null;
     }


C#
if (Form1.RunningInstance() != null)
           {
               MessageBox.Show("Duplicate Instance");
               //TODO:
               //Your application logic for duplicate
               //instances would go here.
               Form1.RunningInstance().Kill();
           }
           else
           {
               RunningInstance();
           }



Thanks
Mohit
 
Share this answer
 
Comments
Ron Beyer 17-Dec-13 13:05pm    
I'm not the one asking the question, I just edited the post.
mohitsaxena1 17-Dec-13 13:25pm    
Hi Ron,
you should be put in article section not in question.
SAJalal 21-Dec-13 8:23am    
Thank Mohit
The routine you provided is useful for getting a process which is running using its name and then retrieve other information about it.
But, situation I drew is about employing COM through the API provided by the application. In the code snippet, the object whose type is passed via a string is instantiated and then the procedures provided by the API are called through this object.
However, I am wondering if I could get access of the object from an already running application instead of starting a new one.

Thanks
SAJala

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