G
Guest
I need to send printjobs to a printqueue under diffrent usernames. The
printsoftware on the queue is not very 'secure' so I can create a user on 1
system and send a printjob under it's name to the printqueue.
This is why I created a program that picks the username out for the .PRN
file, creates a local user, copies the PRN file the network printer and
deletes the user.
I cannot get it to work. I tried first copying a normal text file from one
folder to another but that also does not work. I am having a difficult time
getting a new process to run cmd.exe with /c copy aaaa.txt c:\ as arguments,
let alone copy a .prn file to a network printer (which is also copy aaa.prn
//server/printqueue).
What am I doing wroing?
My code:
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Public watchfolder As FileSystemWatcher
Public ComputerName As String
Public PrintQueue As String
Public Password As String
Public ini As New iniFile(Environment.CurrentDirectory + "\settings.ini")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Password = "P@ssw0rd"
watchfolder = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = ini.GetString("main", "folder", "")
'Add a list of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
' add the handler to each event
AddHandler watchfolder.Created, AddressOf CopyJob
MsgBox(ini.FileName)
MsgBox(watchfolder.Path)
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
End Sub
Private Sub StartCommand(ByVal Command As String, ByVal Args As String,
ByVal User As String, ByVal Password As String, ByVal WorkDir As String)
Dim ProcessInfo As New ProcessStartInfo
Dim sstr As New System.Security.SecureString
Dim pwd As String = Password
Dim chars() As Char = pwd.ToCharArray()
Dim i As Integer
For i = 0 To chars.Length - 1
sstr.AppendChar(chars(i))
Next
ProcessInfo.UseShellExecute = False
ProcessInfo.UserName = User
ProcessInfo.Password = sstr
ProcessInfo.FileName = Command
ProcessInfo.Arguments = Args
ProcessInfo.WorkingDirectory = WorkDir
ProcessInfo.WindowStyle = ProcessWindowStyle.Hidden
ProcessInfo.CreateNoWindow = True
Dim myProcess As New Process()
myProcess.StartInfo = ProcessInfo
myProcess.Start()
End Sub
Private Sub CreateUser(ByVal User As String)
'Create Account
Dim strUser As String = User
Dim oDomain As Object = GetObject("WinNT://" + ComputerName)
Dim oUser As Object = oDomain.Create("user", strUser)
If (Err.Number = 0) Then
oUser.SetInfo()
oUser.SetPassword(Password)
oUser.SetInfo()
End If
End Sub
Private Sub DeleteUser(ByVal User As String)
Dim strUser As String = User
Dim oDomain As Object = GetObject("WinNT://" + ComputerName)
Try
Dim oUser As Object = oDomain.Delete("user", strUser)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub CopyJob(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Dim filename As String = e.FullPath
MsgBox(filename)
ComputerName = System.Environment.MachineName
PrintQueue = ini.GetString("main", "printqueue", "")
Dim Username As String = ""
If Not e.Name.IndexOf("_") = -1 Then
Dim NewOwner() As String = e.Name.ToString.Split("_")
Username = NewOwner(0)
End If
If Not Username = "" Then
DeleteUser(Username)
CreateUser(Username)
StartCommand("c:\windows\system32\cmd.exe", "/c copy " &
filename & " " & PrintQueue, Username, Password, Environment.CurrentDirectory)
DeleteUser(Username)
MsgBox("Done")
End If
End Sub
End Class
printsoftware on the queue is not very 'secure' so I can create a user on 1
system and send a printjob under it's name to the printqueue.
This is why I created a program that picks the username out for the .PRN
file, creates a local user, copies the PRN file the network printer and
deletes the user.
I cannot get it to work. I tried first copying a normal text file from one
folder to another but that also does not work. I am having a difficult time
getting a new process to run cmd.exe with /c copy aaaa.txt c:\ as arguments,
let alone copy a .prn file to a network printer (which is also copy aaa.prn
//server/printqueue).
What am I doing wroing?
My code:
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Public watchfolder As FileSystemWatcher
Public ComputerName As String
Public PrintQueue As String
Public Password As String
Public ini As New iniFile(Environment.CurrentDirectory + "\settings.ini")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Password = "P@ssw0rd"
watchfolder = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = ini.GetString("main", "folder", "")
'Add a list of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
' add the handler to each event
AddHandler watchfolder.Created, AddressOf CopyJob
MsgBox(ini.FileName)
MsgBox(watchfolder.Path)
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
End Sub
Private Sub StartCommand(ByVal Command As String, ByVal Args As String,
ByVal User As String, ByVal Password As String, ByVal WorkDir As String)
Dim ProcessInfo As New ProcessStartInfo
Dim sstr As New System.Security.SecureString
Dim pwd As String = Password
Dim chars() As Char = pwd.ToCharArray()
Dim i As Integer
For i = 0 To chars.Length - 1
sstr.AppendChar(chars(i))
Next
ProcessInfo.UseShellExecute = False
ProcessInfo.UserName = User
ProcessInfo.Password = sstr
ProcessInfo.FileName = Command
ProcessInfo.Arguments = Args
ProcessInfo.WorkingDirectory = WorkDir
ProcessInfo.WindowStyle = ProcessWindowStyle.Hidden
ProcessInfo.CreateNoWindow = True
Dim myProcess As New Process()
myProcess.StartInfo = ProcessInfo
myProcess.Start()
End Sub
Private Sub CreateUser(ByVal User As String)
'Create Account
Dim strUser As String = User
Dim oDomain As Object = GetObject("WinNT://" + ComputerName)
Dim oUser As Object = oDomain.Create("user", strUser)
If (Err.Number = 0) Then
oUser.SetInfo()
oUser.SetPassword(Password)
oUser.SetInfo()
End If
End Sub
Private Sub DeleteUser(ByVal User As String)
Dim strUser As String = User
Dim oDomain As Object = GetObject("WinNT://" + ComputerName)
Try
Dim oUser As Object = oDomain.Delete("user", strUser)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub CopyJob(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Dim filename As String = e.FullPath
MsgBox(filename)
ComputerName = System.Environment.MachineName
PrintQueue = ini.GetString("main", "printqueue", "")
Dim Username As String = ""
If Not e.Name.IndexOf("_") = -1 Then
Dim NewOwner() As String = e.Name.ToString.Split("_")
Username = NewOwner(0)
End If
If Not Username = "" Then
DeleteUser(Username)
CreateUser(Username)
StartCommand("c:\windows\system32\cmd.exe", "/c copy " &
filename & " " & PrintQueue, Username, Password, Environment.CurrentDirectory)
DeleteUser(Username)
MsgBox("Done")
End If
End Sub
End Class