473,657 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Process that copies a prn file to a network printer under username

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.Diagnost ics

Public Class Form1

Public watchfolder As FileSystemWatch er
Public ComputerName As String
Public PrintQueue As String
Public Password As String
Public ini As New iniFile(Environ ment.CurrentDir ectory + "\settings.ini" )

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Password = "P@ssw0rd"

watchfolder = New System.IO.FileS ystemWatcher()

'this is the path we want to monitor
watchfolder.Pat h = 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.Not ifyFilter = IO.NotifyFilter s.DirectoryName
watchfolder.Not ifyFilter = watchfolder.Not ifyFilter Or _
IO.NotifyFilter s.FileName
watchfolder.Not ifyFilter = watchfolder.Not ifyFilter Or _
IO.NotifyFilter s.Attributes

' add the handler to each event
AddHandler watchfolder.Cre ated, AddressOf CopyJob
MsgBox(ini.File Name)
MsgBox(watchfol der.Path)
'Set this property to true to start watching
watchfolder.Ena bleRaisingEvent s = True
End Sub

Private Sub StartCommand(By Val Command As String, ByVal Args As String,
ByVal User As String, ByVal Password As String, ByVal WorkDir As String)
Dim ProcessInfo As New ProcessStartInf o
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.Use ShellExecute = False
ProcessInfo.Use rName = User
ProcessInfo.Pas sword = sstr
ProcessInfo.Fil eName = Command
ProcessInfo.Arg uments = Args
ProcessInfo.Wor kingDirectory = WorkDir
ProcessInfo.Win dowStyle = ProcessWindowSt yle.Hidden
ProcessInfo.Cre ateNoWindow = True

Dim myProcess As New Process()
myProcess.Start Info = ProcessInfo
myProcess.Start ()

End Sub

Private Sub CreateUser(ByVa l User As String)
'Create Account
Dim strUser As String = User
Dim oDomain As Object = GetObject("WinN T://" + ComputerName)
Dim oUser As Object = oDomain.Create( "user", strUser)
If (Err.Number = 0) Then
oUser.SetInfo()
oUser.SetPasswo rd(Password)
oUser.SetInfo()
End If
End Sub

Private Sub DeleteUser(ByVa l User As String)
Dim strUser As String = User
Dim oDomain As Object = GetObject("WinN T://" + ComputerName)
Try
Dim oUser As Object = oDomain.Delete( "user", strUser)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try

End Sub

Private Sub CopyJob(ByVal source As Object, ByVal e As
System.IO.FileS ystemEventArgs)
Dim filename As String = e.FullPath
MsgBox(filename )
ComputerName = System.Environm ent.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(User name)
CreateUser(User name)
StartCommand("c :\windows\syste m32\cmd.exe", "/c copy " &
filename & " " & PrintQueue, Username, Password, Environment.Cur rentDirectory)
DeleteUser(User name)
MsgBox("Done")
End If
End Sub

End Class
Jan 17 '06 #1
8 6725
Hi,

Based on my understanding, copy a file onto a file share folder under
certain username.
I think we can call the impersonate code to run the File.Copy code under
certain user credentials to achive the job.

Here is an KB about how to call logon user to impersonate a user credential.
841699 How to validate Windows user rights in a Visual Basic .NET
application
http://support.microsoft.com/default...b;EN-US;841699

If I have misunderstood, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 17 '06 #2
Can this be used to copy a file to a network printer?

PCL files can be copied to a network printer with copy myprintjob.prn
//server/printername

This works from the shell command.

""Peter Huang" [MSFT]" wrote:
Hi,

Based on my understanding, copy a file onto a file share folder under
certain username.
I think we can call the impersonate code to run the File.Copy code under
certain user credentials to achive the job.

Here is an KB about how to call logon user to impersonate a user credential.
841699 How to validate Windows user rights in a Visual Basic .NET
application
http://support.microsoft.com/default...b;EN-US;841699

If I have misunderstood, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 17 '06 #3
And the kb is for framework 1.1, in 2.0 you can start a process under a
diffrent username.

"Philip Wagenaar" wrote:
Can this be used to copy a file to a network printer?

PCL files can be copied to a network printer with copy myprintjob.prn
//server/printername

This works from the shell command.

""Peter Huang" [MSFT]" wrote:
Hi,

Based on my understanding, copy a file onto a file share folder under
certain username.
I think we can call the impersonate code to run the File.Copy code under
certain user credentials to achive the job.

Here is an KB about how to call logon user to impersonate a user credential.
841699 How to validate Windows user rights in a Visual Basic .NET
application
http://support.microsoft.com/default...b;EN-US;841699

If I have misunderstood, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 17 '06 #4
Hi Philip,

From your description, I understand that you want to know how to use the
Process class to run a shell command using another user account in .NET 2.0.

Based on my research, we can use the Start method of the Process class to
achieve your job.

Here is the code snippet for your reference.

=============== =============== =============== =============== =============== =
============
'TextBox1: username
'TextBox2: password
'TextBox3: domain
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim pass As New SecureString
Dim s As String = TextBox2.Text
For Each c As Char In s.ToCharArray()
pass.AppendChar (c)
Next
Process.Start(" cmd", "/c copy c:\test.bmp \\servername\my share",
Me.TextBox1.Tex t, pass, TextBox3.Text)
End Sub
=============== =============== =============== =============== =============== =
============

If you still have any concern, please feel free to let me know.
I look forward to hearing from you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 18 '06 #5
This works for copying a file to another folder or drive. But this does not
work for copying a .prn file to a network printer. But when you try to copy a
..prn file to a network printer using the same commands using a dosbox it does
work.

So why does copying prn file work from a dosbox and not from process.start
(can you confirm this?)

""Peter Huang" [MSFT]" wrote:
Hi Philip,

From your description, I understand that you want to know how to use the
Process class to run a shell command using another user account in .NET 2.0.

Based on my research, we can use the Start method of the Process class to
achieve your job.

Here is the code snippet for your reference.

=============== =============== =============== =============== =============== =
============
'TextBox1: username
'TextBox2: password
'TextBox3: domain
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim pass As New SecureString
Dim s As String = TextBox2.Text
For Each c As Char In s.ToCharArray()
pass.AppendChar (c)
Next
Process.Start(" cmd", "/c copy c:\test.bmp \\servername\my share",
Me.TextBox1.Tex t, pass, TextBox3.Text)
End Sub
=============== =============== =============== =============== =============== =
============

If you still have any concern, please feel free to let me know.
I look forward to hearing from you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 18 '06 #6
Hi Philip,

Based on my test, I can not reproduce the problem.
Here is my test code.
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
'Dim p As New Process
Dim pass As New SecureString
Dim s As String = TextBox2.Text
For Each c As Char In s.ToCharArray()
pass.AppendChar (c)
Next
Dim par As String
par = "/c copy c:\test.prn \\servername\pr intername"
'par = "/c copy c:\test.bmp \\servername\my share"
Process.Start(" cmd", par, Me.TextBox1.Tex t, pass, TextBox3.Text)
End Sub

BTW: Did you run the code in a Window Service? Because the Window Service
will run under another desktop which is different from the logoned
interactive desktop, in that desktop, the network share printer did not
work as \\servername\pr intername, this is commonly user percific.

To isolate the problem, I suggest you run the code in a winform application
directly.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 19 '06 #7
For some reason today it works, I think it has to do with space in the
printername and quotation marks.

I changed my approach from a windows service to a forms application. When
the forms app works I will try to convert it to a window service.

I am doing this because I need to start a print job under a specific
username. I have solved the problem of the shared printername for when I
convert to service.

- I installed services for unix on both machines.
- Installed a localprinter that uses a LPR printer poort to the networked
printer
- Now I can print to a local printer (machine specific) from any account on
the machine

I will try

""Peter Huang" [MSFT]" wrote:
Hi Philip,

Based on my test, I can not reproduce the problem.
Here is my test code.
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
'Dim p As New Process
Dim pass As New SecureString
Dim s As String = TextBox2.Text
For Each c As Char In s.ToCharArray()
pass.AppendChar (c)
Next
Dim par As String
par = "/c copy c:\test.prn \\servername\pr intername"
'par = "/c copy c:\test.bmp \\servername\my share"
Process.Start(" cmd", par, Me.TextBox1.Tex t, pass, TextBox3.Text)
End Sub

BTW: Did you run the code in a Window Service? Because the Window Service
will run under another desktop which is different from the logoned
interactive desktop, in that desktop, the network share printer did not
work as \\servername\pr intername, this is commonly user percific.

To isolate the problem, I suggest you run the code in a winform application
directly.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 20 '06 #8
Hi

Thanks for your quickly reply!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2593
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set of classes that allow me to interact with network devices. In other words, if I have a path and filename inside of a string variable in python, how do I send the file to the printer?? Thanks in advance
5
14104
by: toby | last post by:
Can any one help? I'm trying to use System.Diagnostics.Process.Start to open legacy MS access aplications (ade and mdb files) and vb 6 .exe's using asp.net. There is something confusing going on?!? The files are all in the same network location, when I try and start the MSaccess files I get a "file not found" error (it does go as far as opening Msaccess, but doesn't load the DB), when I try to start the exe, the .exe is found and runs, but...
1
2040
by: Christian-Josef Schrattenthaler | last post by:
Hi! I have a batch-file (*.bat) which logon the client to our server, including printer and drive mappings. Now I want to replace the batch-file with a script for the windows scripting host, because I want the users to enter their username and password, and then the script should make the mappings suitable to the username... I lookd for the WSH and VBSCRIPT and JSCRIPT, but I didn't get any usable
1
1434
by: Homa | last post by:
I have a web service that want to print something to a printer connected on another computer in the network. And that printer happens to be a non-plug and play printer (It's a thermal printer). I tried to install the printer to the server directly and it works fine (I use PrinterSettings.PrinterName to set the printer). But when I set it up on another computer it doesn't work. I have installed the networked printer's driver to the server...
5
2071
by: Paul Bergson | last post by:
I have been trying to get a process to start up and run with arguments passed to it. I have gotten close (Thanks to help from this board) but I there is a failure while I'm running this because the c:\bin\xcacls starts nothing happens. How can I see output from the console i was trying to get the ProcessStartInfo.RedirectStandardOutput Property to work but even if I got the system to process it I didn;t know where the output was being...
6
14191
by: kk | last post by:
hello to all, I want to call an exe file on other system(on the same LAN)though a webservice(by placing the dll).For that i use the following code in C#...... Process ps = new Process(); ps.StartInfo.FileName = exeFilePath ps.StartInfo.Arguments = arguments; ps.Start();
11
13360
by: Kirk | last post by:
The following C# web service works fine until you uncomment the lines setting UserName and Password. Then the process starts as the specified user, but hangs in a suspended state. In fact, any executable will exhibit this problem; it is not specific to whoami.exe. This is with .NET 2.0, of course (1.1 does not support running a process as a different user). This appears to be a bug. Can anyone comment? <%@ WebService Language="C#"...
1
6042
by: empika | last post by:
Hi how do i open a network printer as a file in C#? I can open the printer and write a line to it in vb6 just like this: Open FindPrinter("\\ip123\thePrinter") For Output As #1 Print #1, "Hallo world!" Close #1
1
9631
by: ekynox | last post by:
Hi guys, have been playing with WMI to add a network printer connection to a Windows XP pc. My environment consists of a server running Windows Server 2003 and Visual Studio 2005 and a test pc running windows xp. Further, I have setup a full domain controller environment. I have managed to write the code to add the network printer and it works fine when installing the printer on the same machine the code is executing from i.e the server or...
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.