Newbie: Error message based on how program is called

B

Bari Allen

On the first line of sub Main() in my console application, I check for the
correct number of command line arguments, as follows:

If Environment.GetCommandLineArgs.Length < 3 Or
Environment.GetCommandLineArgs.Length > 4 Then

When I run the .exe file from the machine it's sitting on (and I've tried
copying it to several drives & running it locally from them) it works, fine,
using the following syntax:
myprogram.exe /switch 7/30/04
-or-
c:\myprogram.exe /switch 7/30/04

However, if I copy it to a server, and try to call it using a UNC, as
follows:

\\mypath\myprogram.exe /switch 7/30/04

I get the following error message, highlighting that first line in Sub
Main(), shown above:
An unhandled exception of type
'System.Security.SecurityException'
occured in mscorlib.dll

Additional information: Request for the permission of type
System.Security.Permissions.EnvironmentPermission, mscorlib,
Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
failed.

Ultimately, I would like to attach this app to a sql job, but I don't want
to have to keep it on the server. Thanks in advance for any help you can
offer on this.

Bari
 
D

David Williams , VB.NET MVP

..NET by default includes a fair amount of built-in security. One of the
security features is that you can not run an application from a UNC
drive. You need to set the permissions for your application to run from
the remote drive. Go to Start->Control Panel->Admistration Tools->.NET
Configuration 1.1->Runtime Security Policy->Increase Assembly Trust to
change it.

HTH

David
 
B

Bari Allen

Hi David,
Thanks for your quick reply. I'm sorry, but I'm a little confused, here.
If I make changes to my current computer (the dev box), will that change the
application, so that 1) when I locate it on another machine it will still be
recognized through the UNC, or 2) do I have to Increase Assembly Trust on
the machine that is going to be calling the application?

Also, if the answer is 2), do I need to re-run the "increase assembly
trust", every time I make a change to the executable file & relocate it?
Thanks.

Bari
 
G

Greg Burns

It's #2. And yes you have to re-run it everytime you recompile your app. A
real PITA.

He is what I do, which you should probably not take as good advice. :^)

Run the .NET Framework Wizard. Choose Adjust .NET Security. Bump the Local
Intranet to full trust. You still have to do this for every machine it will
run on, but only one time per machine. Now any program you write will run
from an UNC path on that machine. Of course, any program you didn't write
could also run. Caveat Emptor!

I also started add a little bit of code (see below) to my program to display
a friendly message when this hasn't not been done for a machine. Rather
than show the rather cryptic ones you get by default.

Greg

Imports System.Security

Module App
<STAThread()> Public Sub Main()

Try
'Demand full trust permissions
Dim fullTrust As New
PermissionSet(Permissions.PermissionState.Unrestricted)
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.")

Catch ex As Exception
HandleUnhandledException(ex)
End Try

End Sub

Private Sub SubMain()

' // Setup unhandled exception handlers
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
OnUnhandledException


AddHandler Application.ThreadException, AddressOf
OnGuiUnhandedException


'Application.EnableVisualStyles()
'SingletonApp.Run(New Login)
Application.Run(New Login)




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 e As Exception = CType(o, Exception)

If Not (e 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

MessageBox.Show("An unhandled exception occurred and the
application is shutting down.")
Environment.Exit(1) ' // Shutting down

End Sub
End Module
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top