WSHNetwork.AddWindowsPrinterConnection in VB.NET

  • Thread starter Thread starter Craig Matthews
  • Start date Start date
C

Craig Matthews

I have the following code snippit written in VB.NET to
map a network drive to the local machine. This works
perfectly and throws the proper exceptions when the
parameters are wrong or unreachable .. no problem.

'begin code
Imports IWshRuntimeLibrary
Public Sub MapDrive()
Dim oNet As New WshNetwork()
oNet.MapNetworkDrive("D:","\\Server\Data")
oNet = Nothing
End Sub
'end code

Now, whenever I try to run this code below to map a
printer, I get:
"An unhandled exception of
type 'System.NullReferenceException' occurred in
AppName.exe Additional information: Invalid Pointer"

The Exception stops with the AddWindowsPrinterConnection
line highlighted.

As far as I can tell, the code is fine. The code works
perfectly in WSH. Is there an incompatibility between
VB.NET and the WshRuntimeLibrary? If I can't do it this
way, is there another way to install a network based
printer to the machine where the application is running?

'begin code
Imports IWshRuntimeLibrary
Public Sub MapPrinter()
Dim oNet as New WshNetwork()
oNet.AddWindowsPrinterConnection("\\Server\Printer")
oNet = Nothing
End Sub
' end code

Thanks!
Craig Matthews
 
Back
Top