Printer sharing

Printer sharing

Post by Ayma » Tue, 06 Jan 2004 23:40:51

How can I programmaticaly share my printer?

Printer sharing

Post by Arkady Fre » Wed, 07 Jan 2004 17:05:34

NetShareAdd() that work for different resources due to type


Printer sharing

Post by Ayma » Wed, 07 Jan 2004 20:07:29

I use NetShareAdd() function, It returns NEER_success.
But I face the following problems:
1. The share sign is not appeared on my printer.
2. In "Add Printer Wizard" on an another computer, when I
select my shared printer to add it as a network printer,
there is an Error: The printer name is invalid.

Would you help me, please?


Printer sharing

Post by Kirk Ferdm » Thu, 08 Jan 2004 15:46:35

On what OS?


Printer sharing

Post by Arkady Fre » Thu, 08 Jan 2004 21:29:07

Try to WNetEnumResource() from other host to check if system can see that
shared one

Printer sharing

Post by Ayma » Fri, 09 Jan 2004 20:41:05

Hi Kirk
I use Windows 2000


Printer sharing

Post by Kirk Ferdm » Sat, 10 Jan 2004 14:31:57

That's a decent one. I was thinking maybe you're using W9x which IIRC used
to have some peculiarities with regard of adding shares. Can you show you


Printer sharing

Post by Ayma » Mon, 12 Jan 2004 22:09:00

W9x which IIRC used
Can you show you

Hi Kirk
This is the code I use to share my printer. It is written
in VB6.

To a form add a command button (Command1) and five text
boxes (Text1 - Text5). Labels are optional.
Add the following to the form:


Option Explicit

Private Const NERR_SUCCESS As Long = 0&

'share types
Private Const STYPE_DISKTREE As Long = 0
Private Const STYPE_PRINTQ As Long = 1
Private Const STYPE_DEVICE As Long = 2
Private Const STYPE_IPC As Long = 3
Private Const STYPE_SPECIAL As Long = &H80000000

Private Const ACCESS_READ As Long = &H1
Private Const ACCESS_WRITE As Long = &H2
Private Const ACCESS_CREATE As Long = &H4
Private Const ACCESS_EXEC As Long = &H8
Private Const ACCESS_DELETE As Long = &H10
Private Const ACCESS_ATRIB As Long = &H20
Private Const ACCESS_PERM As Long = &H40
Private Const ACCESS_ALL As Long = ACCESS_READ Or _

Private Type SHARE_INFO_2
shi2_netname As Long
shi2_type As Long
shi2_remark As Long
shi2_permissions As Long
shi2_max_uses As Long
shi2_current_uses As Long
shi2_path As Long
shi2_passwd As Long
End Type

Private Declare Function NetShareAdd Lib "netapi32" _
(ByVal servername As Long, _
ByVal level As Long, _
buf As Any, _
parmerr As Long) As Long

Private Sub Form_Load()

Text1.Text = "\\" & Environ$("COMPUTERNAME")
Text2.Text = "<Enter the name of printer here>"
Text3.Text = "<Enter the Share Name here>"
Text4.Text = "<Enter your comment here>"
Text5.Text = ""

End Sub

Private Sub Command1_Click()

Dim success As Long

success = ShareAdd(Text1.Text, _
Text2.Text, _
Text3.Text, _
Text4.Text, _

Select Case success
Case 0: MsgBox "share created successfully!"
Case 2118: MsgBox "share name already exists"
Case Else: MsgBox "create error " & success
End Select

End Sub

Private Function ShareAdd(sServer As String, _
sSharePath As String, _
sShareName As String, _
sShareRemark As String, _
sSharePw As String) As Long

Dim dwServer As Long
Dim dwNetname As Long
Dim dwPath As Long
Dim dwRemark As Long
Dim dwPw As Long
Dim parmerr As Long
Dim si2 As SHARE_INFO_2

'obtain pointers to the server, share and path
dwServer = StrPtr(sServer)
dwNetname = StrPtr(sShareName)
dwPath = StrPtr(sSharePath)

'if the remark or password specified,
'obtain pointer to those as well
If Len(sShareRemark) > 0 Then
dwRemark = StrPtr(sShareRemark)
End If

If Len(sSharePw) > 0 Then
dwPw = StrPtr(sSharePw)
End If

'prepare the SH

Printer sharing

Post by Kirk Ferdm » Tue, 13 Jan 2004 04:17:42


Apparently NetShareAdd can't share printers. You have to use SetPrinter on
an NT-derived OS. And there is no way to programmatically share a printer on

Here is the KB: ;en-us;176704


Printer sharing

Post by Ayma » Wed, 14 Jan 2004 18:08:22

Hi Kirk
Thank you very much.