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
 
Hi

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:

Code:

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)
Try
Dim sqlCommand AsNew SqlCommand("POPULATE_TIME_DIMENSION", sConn)
With sqlCommand
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue(
"@dtstartdate", dtpStart)
.Parameters.AddWithValue(
"@dtenddate", dtpEnd)
EndWith
sConn.Open()
 
sqlCommand.ExecuteNonQuery()
Catch ex As Exception
Finally
sConn.Close()
EndTry
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
Try
feedback("Started Building Medefeed")
sConn.Open()
myDA.Fill(myDs,
"DateFinder")
tot_rec = myDs.Tables(0).Rows.Count
If tot_rec = 0 Then
Process.Start("\\server\f$\dir\dir\PatientCosting.exe")
MedeSwitch = 0
Else
MedeSwitch = 1
EndIf
'End If
Catch ex As Exception
Finally
myDA = Nothing
myDs = Nothing
sConn.Close()
EndTry

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.

Rob

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:

Code:

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")
context.Enter()
' 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)
context.Leave()
Console.WriteLine("Current user: " & WindowsIdentity.GetCurrent().Name)
 
 
EndSub
EndModule

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:

Code:


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
Else
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 04:45 AM.

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