Here is the code I use to "demand" full trust and exit gracefully if not.
HTH,
Greg
Option Strict On
Imports System.Data.SqlClient
Imports System.Security
Imports System.Threading
Module App
'The main entry point for the application.
<STAThread()> Public Sub Main()
Application.EnableVisualStyles()
Try
'Demand full trust permissions
Dim fullTrust As New
PermissionSet(Permissions.PermissionState.Unrestri cted)
fullTrust.Demand()
SubMain()
Catch ex As SecurityException
' Report that permissions were not full trust
MessageBox.Show("This application requires full-trust
security permissions to execute.", Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As Exception
HandleUnhandledException(ex)
End Try
End Sub
Private Sub SubMain()
' // Setup unhandled exception handlers
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
OnUnhandledException
'AppDomain.CurrentDomain.UnhandledException += // CLR
' new UnhandledExceptionEventHandler(OnUnhandledExceptio n);
AddHandler Application.ThreadException, AddressOf
OnGuiUnhandedException
'Application.ThreadException += // Windows Forms
' new System.Threading.ThreadExceptionEventHandler(
' OnGuiUnhandedException);
'Application.EnableVisualStyles()
Try
SingletonApp.Run(New Login,
"8ca35a66-6e9a-41d4-a87d-d9755b1f88c4")
Catch ex As SingletonException
MessageBox.Show("Cannot start because a previous instance of
this application is already running!", Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
'// CLR unhandled exception
Private Sub OnUnhandledException(ByVal sender As Object, ByVal e As
UnhandledExceptionEventArgs)
HandleUnhandledException(e.ExceptionObject)
End Sub
' // Windows Forms unhandled exception
Private Sub OnGuiUnhandedException(ByVal sender As Object, ByVal e
As System.Threading.ThreadExceptionEventArgs)
HandleUnhandledException(e.Exception)
End Sub
Private Sub HandleUnhandledException(ByVal o As Object)
Dim ex As Exception = CType(o, Exception)
If Not (ex Is Nothing) Then ' // Report System.Exception info
' Debug.WriteLine("Exception = " + e.GetType());
' Debug.WriteLine("Message = " + e.Message);
' Debug.WriteLine("FullText = " + e.ToString());
Else ' // Report exception Object info
' Debug.WriteLine("Exception = " + o.GetType());
' Debug.WriteLine("FullText = " + o.ToString());
End If
My.Application.Log.WriteException(ex, TraceEventType.Error,
ex.ToString)
MessageBox.Show("An unhandled exception occurred and the
application is shutting down.", Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
Environment.Exit(1) ' // Shutting down
End Sub
End Module
Public Class SingletonApp
Private Shared _guid As String
Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form, ByVal guid As String)
_guid = guid
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
Else
Throw New SingletonException
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean
m_Mutex = New Mutex(False, _guid)
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function
Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex()
m_Mutex.Close()
End Sub
End Class
Public Class SingletonException
Inherits System.ApplicationException
Public Sub New()
MyBase.New("Cannot start because a previous instance of this
application is already running!")
End Sub
Public Sub New(ByVal InnerException As Exception)
MyBase.New("Cannot start because a previous instance of this
application is already running!", InnerException)
End Sub
End Class
"mike2036" <mi******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
I have an application (that has unmanaged code) and when I launch it
without
'FullTrust' permissions (LocalIntranet_Zone), it crashes. When I set
'FullTrust' permissions, it launches fine. Is there a way I can compile
the
application such that it won't even attempt to launch without correct
permissions instead of just crashing? Thanks for any help.
-Mike