Process class and .NET security

Discussion in 'ASP .Net Security' started by Louis-Philippe Carignan, Sep 9, 2004.

  1. Hi all,

    I posted this message a couple of days ago on
    microsoft.public.dotnet.framework and never got a response. I am trying
    here because it involves security but it's not ASP. If someone has a better
    idea where this post should go, please let met know.

    Here is my problem:

    I am using the Process class in my application, right at the beginning of
    the static Main method. I am using the Process class to know if another
    instance of my application is currently running. If my application is
    already running, I will exit.

    This works fine on my development machine. But if I run it on a deployment
    machine, I get the error "Common Language Runtime Debugging Services :
    Application has generated an exception that could not be handled". I cannot
    run the remote debugger since the application is running in another domain
    and this domain doesn't trust my dev machine (which is in another domain)

    Currently, my only workaround is to comment the code. I am not satisfied
    with this behavior. I though of going throught the Win32 API but I would
    prefer a .NET solution to my problem. I know the problem is with the
    Process class. In the .NET SDK documentation, there is 2 lines of text
    about a permission used to access the Process class. How do I bypass the
    security? How do I give the required permission to my application?

    Thank you,

    Louis-Philippe


    Here is the code commented out

    // Before starting the application, check if the same application

    // is already running. If so, show a message box to the user

    // indicating that the application is already running and exit the program

    try

    {

    Process currentProcess = Process.GetCurrentProcess();

    foreach (Process process in
    Process.GetProcessesByName(currentProcess.ProcessName))

    {

    if (currentProcess.Id != process.Id)

    {

    LogManager.Log(typeof(ClientApp), APP_ERROR_TEXT, LogType.Error,
    LogLevel.High);

    MessageBox.Show(APP_ERROR_TEXT, APP_ERROR_CAPTION,
    MessageBoxButtons.OK, MessageBoxIcon.Error);

    Shutdown();

    return;

    }

    }

    }

    catch

    {

    LogManager.Log(typeof(ClientApp), APP_GETPROCESS_EXCEPTION_TEXT,
    LogType.Error, LogLevel.High);

    }
    Louis-Philippe Carignan, Sep 9, 2004
    #1
    1. Advertising

  2. Did you consider setting a Mutex (from System.Threading) in your start up
    code and then check to see if that Mutex is held already in the same code?
    The idea would be that if the mutex is already held, the application is
    already running, so you would just exit or something.

    You would then get rid of the mutex when you stop your program.

    It doesn't solve your exact problem now, but it might work better. I think
    there is a similar trick you can do with .NET remoting, but I'm not sure how
    that works.

    HTH,

    Joe K.

    "Louis-Philippe Carignan" <> wrote in message
    news:...
    > Hi all,
    >
    > I posted this message a couple of days ago on
    > microsoft.public.dotnet.framework and never got a response. I am trying
    > here because it involves security but it's not ASP. If someone has a
    > better
    > idea where this post should go, please let met know.
    >
    > Here is my problem:
    >
    > I am using the Process class in my application, right at the beginning of
    > the static Main method. I am using the Process class to know if another
    > instance of my application is currently running. If my application is
    > already running, I will exit.
    >
    > This works fine on my development machine. But if I run it on a
    > deployment
    > machine, I get the error "Common Language Runtime Debugging Services :
    > Application has generated an exception that could not be handled". I
    > cannot
    > run the remote debugger since the application is running in another domain
    > and this domain doesn't trust my dev machine (which is in another domain)
    >
    > Currently, my only workaround is to comment the code. I am not satisfied
    > with this behavior. I though of going throught the Win32 API but I would
    > prefer a .NET solution to my problem. I know the problem is with the
    > Process class. In the .NET SDK documentation, there is 2 lines of text
    > about a permission used to access the Process class. How do I bypass the
    > security? How do I give the required permission to my application?
    >
    > Thank you,
    >
    > Louis-Philippe
    >
    >
    > Here is the code commented out
    >
    > // Before starting the application, check if the same application
    >
    > // is already running. If so, show a message box to the user
    >
    > // indicating that the application is already running and exit the program
    >
    > try
    >
    > {
    >
    > Process currentProcess = Process.GetCurrentProcess();
    >
    > foreach (Process process in
    > Process.GetProcessesByName(currentProcess.ProcessName))
    >
    > {
    >
    > if (currentProcess.Id != process.Id)
    >
    > {
    >
    > LogManager.Log(typeof(ClientApp), APP_ERROR_TEXT,
    > LogType.Error,
    > LogLevel.High);
    >
    > MessageBox.Show(APP_ERROR_TEXT, APP_ERROR_CAPTION,
    > MessageBoxButtons.OK, MessageBoxIcon.Error);
    >
    > Shutdown();
    >
    > return;
    >
    > }
    >
    > }
    >
    > }
    >
    > catch
    >
    > {
    >
    > LogManager.Log(typeof(ClientApp), APP_GETPROCESS_EXCEPTION_TEXT,
    > LogType.Error, LogLevel.High);
    >
    > }
    >
    >
    >
    Joe Kaplan \(MVP - ADSI\), Sep 9, 2004
    #2
    1. Advertising

  3. Thanks Joe for the advice. I didn't tought about the Mutex idea.

    But I would still like to know why the Process class is giving me security
    problems.

    "Joe Kaplan (MVP - ADSI)" <> wrote
    in message news:...
    > Did you consider setting a Mutex (from System.Threading) in your start up
    > code and then check to see if that Mutex is held already in the same code?
    > The idea would be that if the mutex is already held, the application is
    > already running, so you would just exit or something.
    >
    > You would then get rid of the mutex when you stop your program.
    >
    > It doesn't solve your exact problem now, but it might work better. I

    think
    > there is a similar trick you can do with .NET remoting, but I'm not sure

    how
    > that works.
    >
    > HTH,
    >
    > Joe K.
    >
    > "Louis-Philippe Carignan" <> wrote in message
    > news:...
    > > Hi all,
    > >
    > > I posted this message a couple of days ago on
    > > microsoft.public.dotnet.framework and never got a response. I am trying
    > > here because it involves security but it's not ASP. If someone has a
    > > better
    > > idea where this post should go, please let met know.
    > >
    > > Here is my problem:
    > >
    > > I am using the Process class in my application, right at the beginning

    of
    > > the static Main method. I am using the Process class to know if another
    > > instance of my application is currently running. If my application is
    > > already running, I will exit.
    > >
    > > This works fine on my development machine. But if I run it on a
    > > deployment
    > > machine, I get the error "Common Language Runtime Debugging Services :
    > > Application has generated an exception that could not be handled". I
    > > cannot
    > > run the remote debugger since the application is running in another

    domain
    > > and this domain doesn't trust my dev machine (which is in another

    domain)
    > >
    > > Currently, my only workaround is to comment the code. I am not

    satisfied
    > > with this behavior. I though of going throught the Win32 API but I

    would
    > > prefer a .NET solution to my problem. I know the problem is with the
    > > Process class. In the .NET SDK documentation, there is 2 lines of text
    > > about a permission used to access the Process class. How do I bypass

    the
    > > security? How do I give the required permission to my application?
    > >
    > > Thank you,
    > >
    > > Louis-Philippe
    > >
    > >
    > > Here is the code commented out
    > >
    > > // Before starting the application, check if the same application
    > >
    > > // is already running. If so, show a message box to the user
    > >
    > > // indicating that the application is already running and exit the

    program
    > >
    > > try
    > >
    > > {
    > >
    > > Process currentProcess = Process.GetCurrentProcess();
    > >
    > > foreach (Process process in
    > > Process.GetProcessesByName(currentProcess.ProcessName))
    > >
    > > {
    > >
    > > if (currentProcess.Id != process.Id)
    > >
    > > {
    > >
    > > LogManager.Log(typeof(ClientApp), APP_ERROR_TEXT,
    > > LogType.Error,
    > > LogLevel.High);
    > >
    > > MessageBox.Show(APP_ERROR_TEXT, APP_ERROR_CAPTION,
    > > MessageBoxButtons.OK, MessageBoxIcon.Error);
    > >
    > > Shutdown();
    > >
    > > return;
    > >
    > > }
    > >
    > > }
    > >
    > > }
    > >
    > > catch
    > >
    > > {
    > >
    > > LogManager.Log(typeof(ClientApp), APP_GETPROCESS_EXCEPTION_TEXT,
    > > LogType.Error, LogLevel.High);
    > >
    > > }
    > >
    > >
    > >

    >
    >
    Louis-Philippe Carignan, Sep 10, 2004
    #3
  4. I have no idea what was causing the error you were seeing, so I can't help
    with that. Sorry!

    Hopefully the mutex will work though.

    Good luck,

    Joe K.

    "Louis-Philippe Carignan" <> wrote in message
    news:uUvU%...
    > Thanks Joe for the advice. I didn't tought about the Mutex idea.
    >
    > But I would still like to know why the Process class is giving me security
    > problems.
    >
    > "Joe Kaplan (MVP - ADSI)" <> wrote
    > in message news:...
    >> Did you consider setting a Mutex (from System.Threading) in your start up
    >> code and then check to see if that Mutex is held already in the same
    >> code?
    >> The idea would be that if the mutex is already held, the application is
    >> already running, so you would just exit or something.
    >>
    >> You would then get rid of the mutex when you stop your program.
    >>
    >> It doesn't solve your exact problem now, but it might work better. I

    > think
    >> there is a similar trick you can do with .NET remoting, but I'm not sure

    > how
    >> that works.
    >>
    >> HTH,
    >>
    >> Joe K.
    >>
    >> "Louis-Philippe Carignan" <> wrote in message
    >> news:...
    >> > Hi all,
    >> >
    >> > I posted this message a couple of days ago on
    >> > microsoft.public.dotnet.framework and never got a response. I am
    >> > trying
    >> > here because it involves security but it's not ASP. If someone has a
    >> > better
    >> > idea where this post should go, please let met know.
    >> >
    >> > Here is my problem:
    >> >
    >> > I am using the Process class in my application, right at the beginning

    > of
    >> > the static Main method. I am using the Process class to know if
    >> > another
    >> > instance of my application is currently running. If my application is
    >> > already running, I will exit.
    >> >
    >> > This works fine on my development machine. But if I run it on a
    >> > deployment
    >> > machine, I get the error "Common Language Runtime Debugging Services :
    >> > Application has generated an exception that could not be handled". I
    >> > cannot
    >> > run the remote debugger since the application is running in another

    > domain
    >> > and this domain doesn't trust my dev machine (which is in another

    > domain)
    >> >
    >> > Currently, my only workaround is to comment the code. I am not

    > satisfied
    >> > with this behavior. I though of going throught the Win32 API but I

    > would
    >> > prefer a .NET solution to my problem. I know the problem is with the
    >> > Process class. In the .NET SDK documentation, there is 2 lines of text
    >> > about a permission used to access the Process class. How do I bypass

    > the
    >> > security? How do I give the required permission to my application?
    >> >
    >> > Thank you,
    >> >
    >> > Louis-Philippe
    >> >
    >> >
    >> > Here is the code commented out
    >> >
    >> > // Before starting the application, check if the same application
    >> >
    >> > // is already running. If so, show a message box to the user
    >> >
    >> > // indicating that the application is already running and exit the

    > program
    >> >
    >> > try
    >> >
    >> > {
    >> >
    >> > Process currentProcess = Process.GetCurrentProcess();
    >> >
    >> > foreach (Process process in
    >> > Process.GetProcessesByName(currentProcess.ProcessName))
    >> >
    >> > {
    >> >
    >> > if (currentProcess.Id != process.Id)
    >> >
    >> > {
    >> >
    >> > LogManager.Log(typeof(ClientApp), APP_ERROR_TEXT,
    >> > LogType.Error,
    >> > LogLevel.High);
    >> >
    >> > MessageBox.Show(APP_ERROR_TEXT, APP_ERROR_CAPTION,
    >> > MessageBoxButtons.OK, MessageBoxIcon.Error);
    >> >
    >> > Shutdown();
    >> >
    >> > return;
    >> >
    >> > }
    >> >
    >> > }
    >> >
    >> > }
    >> >
    >> > catch
    >> >
    >> > {
    >> >
    >> > LogManager.Log(typeof(ClientApp), APP_GETPROCESS_EXCEPTION_TEXT,
    >> > LogType.Error, LogLevel.High);
    >> >
    >> > }
    >> >
    >> >
    >> >

    >>
    >>

    >
    >
    Joe Kaplan \(MVP - ADSI\), Sep 10, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,710
    Thomas Weidenfeller
    Oct 12, 2005
  2. Earl Eiland
    Replies:
    0
    Views:
    307
    Earl Eiland
    May 10, 2005
  3. Dinis Cruz

    Asp.Net Security Analyser (new security tool by DDPlus)

    Dinis Cruz, Oct 8, 2003, in forum: ASP .Net Security
    Replies:
    2
    Views:
    127
    Dinis Cruz
    Oct 11, 2003
  4. Michael Randrup
    Replies:
    3
    Views:
    284
    Henning Krause [MVP]
    Mar 27, 2006
  5. Kursat
    Replies:
    1
    Views:
    298
    Dominick Baier
    May 7, 2007
Loading...

Share This Page