p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro Visual Basic 2005 (http://p2p.wrox.com/forumdisplay.php?f=217)
-   -   Starting a console App from a vb.net windows app (http://p2p.wrox.com/showthread.php?t=74922)

RobCarter June 24th, 2009 05:00 AM

Starting a console App from a vb.net windows app

I am a kinda VB.net noob so I am after as much constructive advice as possible for my issue:

I have a console app that was built by a contractor who has now left our company. It needs to be run every 7th working day.

Obviously, once the console app has finished its task it shuts down so I need a persistent application to begin the task again.

(it is at this point I would envisage people mentioning windows services but at this moment in time the mechanics of that is beyond my limited scope)

I have built a timer that looks at 7th working day and I now need for that windows app to open my console app. The exe opens but then I get a security permissions exception.

The code that invokes the console app is:


Imports LE22ExtractManager.clsDates
Imports System.Data.sqlclient
Imports System.Data.Sql
Imports System.Data
Imports Microsoft.VisualBasic.DateAndTime
Imports System.Diagnostics.Process
Namespace Medefeed
PublicClass clsMedeFeed
PublicSharedSub Check7thDay(ByRef MedeSwitch AsInteger)
Dim myFYst AsInteger = FiscalYearStartMonth.Apr
Dim dtpEnd AsString = Format(DateAdd(DateInterval.Day, -1, Today), "yyyyMMdd")
Dim dtpStart AsString = fiscalYear(myFYst, Today) & Right("00" & myFYst, 2) & "01"
Dim dDate AsString = Right("000" & Day(Today).ToString, 2) & "/" & Right("000" & Month(Today()).ToString, 2) & "/" & Year(Today()).ToString
Dim tot_rec AsInteger
Dim strConnection AsString = "Server=myserver;Database=Mydb;User ID=user;Password=pwd;Trusted_Connection=False;"
Dim sConn AsNew SqlConnection(strConnection)
Dim sqlCommand AsNew SqlCommand("POPULATE_TIME_DIMENSION", sConn)
With sqlCommand
.CommandType = CommandType.StoredProcedure
"@dtstartdate", dtpStart)
"@dtenddate", dtpEnd)
Catch ex As Exception
Dim sSql AsString = "select * from dim_time where "
sSql += "fulldatealternatekey = '" & dDate & "' and "
sSql += "numworkdays = 7"
Dim myDA As SqlDataAdapter = New SqlDataAdapter(sSql, sConn)
Dim myDs AsNew DataSet
feedback("Started Building Medefeed")
tot_rec = myDs.Tables(0).Rows.Count
If tot_rec = 0 Then
MedeSwitch = 0
MedeSwitch = 1
'End If
Catch ex As Exception
myDA = Nothing
myDs = Nothing

The exception that I get displays: System.Security.Permissions.SecurityPermission

If more information is needed I would be happy to clarify further.

Thanks for any help anyone can provide - I am totally stuck with this.


gbianchi June 24th, 2009 08:10 AM

Hi there..

Which line throws the error?? the process start??

Are you sure that your program has enough permissions to run another one between a network???

RobCarter June 24th, 2009 08:40 AM

Its the process.start that generates the error - The exe file opens a console window but then the console app code is shown in the debugger with the error in the declarations:


Module mCosting
PrivateConst sSource = "\\Server\f$\dir\"
Private sDest AsString = "\\Server\f$\dir\sdir\" & Format(Today, "yyyyMMdd") & "\"
PrivateConst sFileSource = "\\correctfilepath"
PrivateConst sFileDest = "\\correctfilepath"
Public myFYst AsInteger = FiscalYearStartMonth.Apr
Public dtpEnd AsString = Format(DateAdd(DateInterval.Day, -1, Today), "yyyyMMdd")
Public dtpStart AsString = fiscalYear(myFYst, Today()) & Right("00" & myFYst, 2) & "01"
Sub Main()
'Console.WriteLine("Current user: " & WindowsIdentity.GetCurrent().Name)
Dim context AsNew WrapperImpersonationContext("domain", "user", "pwd")
' Execute code under other uses context
Console.WriteLine("Current user: " & WindowsIdentity.GetCurrent().Name)
GetSheetToUnprotect("Immunology", "myeloma")
'backup last months files
Archive(sSource, sDest)
runDTS("DTS_Export_OutpatientFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_InpatientFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_AlliedFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_ProsthesisFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_WastedProsthesisFeed", dtpStart, dtpEnd) '"20080401", "20090331")
runDTS("DTS_Export_RadiologyFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_WardFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_TheatreFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_EndoscopyFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_PathologyFeed", dtpStart, dtpEnd) '"20080401", "20090331") '
runDTS("DTS_Export_ROCSFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_MacMillanFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_ImmunologyFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
runDTS("DTS_Export_NeuroPhysiologyFeed", dtpStart, dtpEnd) '"20080401", "20090331")'
Archive(sFileSource, sFileDest)
Console.WriteLine("Current user: " & WindowsIdentity.GetCurrent().Name)

I have difficulty understanding the ins and outs of .net security, so no, I'm not sure about the security issue - can you point me to any guides that are sufficiently un-techie? Most that I have googled don't seem to apply.

If not could you give me an idea of search terms to achieve the best results?

Thanks for your reply.

RobCarter June 24th, 2009 08:43 AM

BTW the underlined section is where the debugger always lands. It points at the following function in its class:


Public Class clsDates
Public Enum FiscalYearStartMonth
Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
Jun = 6
Jul = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12
End Enum
Shared Function fiscalYear(ByVal FYStartMonth As Integer, ByVal aDate As DateTime) As String
Dim theYear As Integer = aDate.Year
If aDate.Month >= FYStartMonth Then
'theYear += 1
theYear -= 1
End If
Return theYear.ToString
End Function
End Class

RobCarter June 24th, 2009 09:20 AM

I actually sorted this out myself by going into the framework config and changing the permissions on our intranet zone to full trust.

Thanks for your questions gbianchi, they pointed me in the right direction.

All times are GMT -4. The time now is 11:58 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.