By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

Application crashes using .NET 2.0 framework because of permission

P: n/a
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

Dec 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
mike2036 wrote:
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


I believe if you wrap main with a try/catch block you can intercept the
message and give your user a "nice message". I could be wrong though.

Chris
Dec 22 '05 #2

P: n/a
Hi Chris,

I have try/catch handling in my mainline, but it never gets executed. If
this is any help, this what the event log looks like:

Source: .Net Runtime 2.0 Error
Event: 1000

Description:

Faulting application myapp.exe, version 5.0.2182.21680, stamp 43ab272f,
faulting module ntdll.dll, version 5.1.2600.2180, stamp 411096b4, debug? 0,
fault address 0x0004eafa.

-Mike
"I Don't Like Spam" wrote:
mike2036 wrote:
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


I believe if you wrap main with a try/catch block you can intercept the
message and give your user a "nice message". I could be wrong though.

Chris

Dec 22 '05 #3

P: n/a
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

Dec 23 '05 #4

P: n/a
Hi Greg,

Unfortunately this didn't fix the problem. When I test my app in the
development environment, I get an unhandled 'FileLoadException' before I ever
execute a line of code. So it looks to me like the loader checks with the
framework if the user has permissions and since they don't, an exception is
thrown and the app blows up. What *should* happen is if the loader or the
framework is going to deny the app's execution, it should produce a "nice"
message to the user saying something to the effect that they don't have
permissions. Is anybody else seeing this with .NET 2? I should note I'm
running this app from the network (via mapped drive).

-Mike
"Greg Burns" wrote:
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


Dec 23 '05 #5

P: n/a
My apps generally run from a network (via mapped drive) as well. That code
seems to work pretty well for me. If I forget to give a computer (I don't
bother being very fine grained) full Trust security for Local Intranet, then
my global error handling catches it as expected.

Are you starting your app via a Form or Sub Main?

Greg
"mike2036" <mi******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
Hi Greg,

Unfortunately this didn't fix the problem. When I test my app in the
development environment, I get an unhandled 'FileLoadException' before I
ever
execute a line of code. So it looks to me like the loader checks with the
framework if the user has permissions and since they don't, an exception
is
thrown and the app blows up. What *should* happen is if the loader or the
framework is going to deny the app's execution, it should produce a "nice"
message to the user saying something to the effect that they don't have
permissions. Is anybody else seeing this with .NET 2? I should note I'm
running this app from the network (via mapped drive).

-Mike
"Greg Burns" wrote:
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
>


Dec 23 '05 #6

P: n/a
Hi,

Try enabling the clickonce security settings. Open the project
properties. Open the security tab and check the enable clickonce security
settings.

Ken
-----------------------
"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

Dec 23 '05 #7

P: n/a
Sorry for the late reply. The app starts at Sub Main. I can see in the
debugger I never enter the mainline...I get the following error:

System.IO.FileLoadException was unhandled
Message: Could not load file or assembly 'MyApp, Version=5.0.2187.18914,
Culture=neutral, PublicKeyToken=1a097bd2d767e26f' or one of its dependencies.
Failed to grant permission to execute. (Exception from HRESULT: 0x80131418)

In fact as a test I created a very simple app that's just a form with a
command button that says "hello world" when pressed...and I'm getting the
exact same error as above and I blow up when running outside the debugger.
This looks to me like I either have a corrupt .NET framework or there's a bug
in the framework. I'm going to try some additional tests. Anybody from
Microsoft reading this by chance? Or has anybody else seen similar behavior?

-Mike

"Greg Burns" wrote:
My apps generally run from a network (via mapped drive) as well. That code
seems to work pretty well for me. If I forget to give a computer (I don't
bother being very fine grained) full Trust security for Local Intranet, then
my global error handling catches it as expected.

Are you starting your app via a Form or Sub Main?

Greg
"mike2036" <mi******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
Hi Greg,

Unfortunately this didn't fix the problem. When I test my app in the
development environment, I get an unhandled 'FileLoadException' before I
ever
execute a line of code. So it looks to me like the loader checks with the
framework if the user has permissions and since they don't, an exception
is
thrown and the app blows up. What *should* happen is if the loader or the
framework is going to deny the app's execution, it should produce a "nice"
message to the user saying something to the effect that they don't have
permissions. Is anybody else seeing this with .NET 2? I should note I'm
running this app from the network (via mapped drive).

-Mike
"Greg Burns" wrote:
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
>


Dec 27 '05 #8

P: n/a
Hi Ken,

I tried this, to no avail. Thanks for the suggestion.

-Mike
"Ken Tucker [MVP]" wrote:
Hi,

Try enabling the clickonce security settings. Open the project
properties. Open the security tab and check the enable clickonce security
settings.

Ken
-----------------------
"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


Dec 27 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.