Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Prevent a .NET program for starting if the .exe is in us
Message
From
27/03/2017 01:22:43
 
 
To
27/03/2017 01:09:24
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01649285
Message ID:
01649352
Views:
30
Gregory
Thank you for looking in.
I've discovered that when it comes to the terminology surrounding this topic, I'm quickly confused.

In terms that I can understand (VERY SIMPLE) I want just one person - anywhere- on Earth or anywhere else in this galaxy to run the named executable that resides on that location on that hard drive at any given time.

Will this code do that?




>Bill,
>
>Just browsing through your code and one thing has got my attention
>
>string mutexId = string.Format("Global\\{{{0}}}", appGuid);
>
>
>If you are running on a terminal server, it means that only one user can use the application at any time since you create a Global mutex.
>Don't know whether that is your intention
>
>Here https://msdn.microsoft.com/en-us/library/9zf2f5bz(v=vs.110).aspx
>
>On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.
>
>
>
>>>>>>Can that be done?
>>>>>>How?
>>>>>
>>>>>Click on "Project\[Project] properties\Application\Windows application framework properties\Make single instance application".
>>>>
>>>>Thank you, Michel.
>>>>I'll check that out
>>>
>>>I think that's a VB only option. The standard way is to use a mutex. Example here (with a few options on how to handle attempts to open a second instance):
>>>http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
>>
>>Thank you for link, Viv
>>After looking at several links and borrowing freely, here's what I wound up with
>>It's working well.
>>
>>
>>            static void Main()
>>        {
>>
>>            // get application GUID as defined in AssemblyInfo.cs
>>            string appGuid =
>>                ((GuidAttribute)Assembly.GetExecutingAssembly().
>>                    GetCustomAttributes(typeof(GuidAttribute), false).
>>                        GetValue(0)).Value.ToString();
>>
>>            // unique id for global mutex - Global prefix means it is global to the machine
>>            string mutexId = string.Format("Global\\{{{0}}}", appGuid);
>>
>>            // Need a place to store a return value in Mutex() constructor call
>>            bool createdNew;
>>
>>            var allowEveryoneRule =
>>                new MutexAccessRule(
>>                    new SecurityIdentifier(WellKnownSidType.WorldSid, null),
>>                    MutexRights.FullControl, AccessControlType.Allow);
>>            var securitySettings = new MutexSecurity();
>>            securitySettings.AddAccessRule(allowEveryoneRule);
>>
>>            using (var mutex = new Mutex(false, mutexId, out createdNew, securitySettings))
>>            {
>>                var hasHandle = false;
>>                try
>>                {
>>                    try
>>                    {
>>                        hasHandle = mutex.WaitOne(5000, false);
>>                        if (hasHandle == false)
>>                        {
>>                            throw new System.TimeoutException("Timeout waiting for exclusive access");
>>                        }
>>                    }
>>                    catch (AbandonedMutexException)
>>                    {
>>                        // Log the fact that the mutex was abandoned in another process,
>>                        // it will still get acquired
>>                        hasHandle = true;
>>                    }
>>
>>                    // Perform your work here.
>>                    SQLServerMethods sqlServerMethods = new SQLServerMethods();
>>                    sqlServerMethods.StartApp();
>>
>>
>>                }
>>
>>                catch (System.TimeoutException)
>>                {
>>                    // Log the fact that the mutex was abandoned in another process,
>>                    // it will still get acquired
>>                    MessageBox.Show("This application is limited to one user at a time. Try again later.");
>>
>>                }
>>                finally
>>                {
>>                    if (hasHandle)
>>                        mutex.ReleaseMutex();
>>                }
>>
>>            }
>>        }
>>
>>
Anyone who does not go overboard- deserves to.
Malcolm Forbes, Sr.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform