Results 1 to 9 of 9

Thread: Add printer in windows 10 problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Add printer in windows 10 problem

    • Im having problems with adding printer's in windows 10
    • After adding the port, the program just crash
    • The code im using works very well in windows 7
    • Can anybody help? Thanks



    Code:
        'IR2520 W10
        Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    
            'SETS LOAD DRIVERS PRIVILEGE.
            Dim strComputer As String
            Dim objWMIService As Object
            strComputer = "."
            objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")
    
            'SETS PRINTER PORT
            Dim objNewPort = objWMIService.get("Win32_TCPIPPrinterPort").SpawnInstance_
            objNewPort.Name = "10.71.197.217"
            objNewPort.Protocol = 1
            objNewPort.HostAddress = "10.71.197.217"
            objNewPort.PortNumber = 9100 
            objNewPort.SNMPEnabled = False
            objNewPort.Put_
    
            'INSTALL DRIVERS
            Dim objDriver = objWMIService.Get("Win32_PrinterDriver")
            objWMIService.Security_.Privileges.AddAsString("SeLoadDriverPrivilege", True)
            objDriver.Name = "Canon iR2520 UFRII LT"
            objDriver.SupportedPlatform = "Windows NT x86" 
            objDriver.Version = "3"
            objDriver.FilePath = "$(ProjectDir)..\..\Drivers\Canon\Canon IR2520\x64\Driver" 
            objDriver.Infname = "$(ProjectDir)..\..\Drivers\Canon\Canon IR2520\x64\Driver\CNLB0MA64.INF" 
            Dim intResult = objDriver.AddPrinterDriver(objDriver)
    
            'SETS PRINTER TO THE PORT
            Dim objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
            objPrinter.DriverName = "Canon iR2520 UFRII LT" 
            objPrinter.PortName = "10.71.197.217" 
            objPrinter.DeviceID = "Canon 2520 test UFRII W10" 
            objPrinter.Location = "TTEST W10"
            objPrinter.Default = 1
            objPrinter.Network = True
            objPrinter.Shared = False
            objPrinter.Put_"
    
       End Sub
    Last edited by xirica; Feb 25th, 2021 at 05:53 AM.

  2. #2
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,031

    Re: Add printer in windows 10 problem

    Hi Xirica,

    Are you using Visual Basic .NET ?

    Are these instructions within a subroutine ?

    Please post your code, as written, in the expected manner in this forum, i.e. inside square parentheses (use the # tab in the heading of the page)


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Re: Add printer in windows 10 problem

    Quote Originally Posted by Poppa Mintin View Post
    Hi Xirica,

    Are you using Visual Basic .NET ?

    Are these instructions within a subroutine ?

    Please post your code, as written, in the expected manner in this forum, i.e. inside square parentheses (use the # tab in the heading of the page)


    Poppa
    Thank you Poppa, already edit the post and yes its VB .net

    OBS: Im still newbie to VB .NET

  4. #4
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,031

    Re: Add printer in windows 10 problem

    Hi Xirica,

    Well I'm still not completely sure what you're trying to do exactly, and I have never used '.SpawnInstance_' however...

    The first thing I notice is that you haven't defined the variable type, whilst VB.NET can assign a type, I find it better to do it in the way I've shown. This may be the problem, although I suspect it has more to do with '.SpawnInstance_'.

    The problem may also be with file paths, it's my belief that these need to be explicit. I'm sure someone with more knowledge than I will be able to help.
    Take a look at This.

    Since you say you are new to VB.NET I've also demonstrated the very handy 'With' command.
    You will also notice the two commands at the top of the code, adding these will ensure that Intellisence will flag any errors.


    Poppa
    Code:
    Option Explicit On
    Option Strict On
    
    'IR2520 W10
        Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    
            'SETS LOAD DRIVERS PRIVILEGE.
            Dim strComputer As String = "."
            Dim objWMIService As Object = GetObject("winmgmts:{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")
    
            'SETS PRINTER PORT
            Dim objNewPort as Object = objWMIService.get("Win32_TCPIPPrinterPort").SpawnInstance_
            With objNewPort
    	    .Name = "10.71.197.217"
    	    .Protocol = 1
    	    .HostAddress = "10.71.197.217"
    	    .PortNumber = 9100 
    	    .SNMPEnabled = False
    	    .Put_
            End With
    
            'INSTALL DRIVERS
            Dim objDriver as Object = objWMIService.Get("Win32_PrinterDriver")
            objWMIService.Security_.Privileges.AddAsString("SeLoadDriverPrivilege", True)
            With objDriver
    	    .Name = "Canon iR2520 UFRII LT"
      	    .SupportedPlatform = "Windows NT x86" 
    	    .Version = "3"
    	    .FilePath = "$(ProjectDir)..\..\Drivers\Canon\Canon IR2520\x64\Driver" 
    	    .Infname = "$(ProjectDir)..\..\Drivers\Canon\Canon IR2520\x64\Driver\CNLB0MA64.INF" 
            End With
            Dim intResult as Object = objDriver.AddPrinterDriver(objDriver)
    
            'SETS PRINTER TO THE PORT
            Dim objPrinter as Object = objWMIService.Get("Win32_Printer").SpawnInstance_
            With objPrinter
    	    .DriverName = "Canon iR2520 UFRII LT" 
    	    .PortName = "10.71.197.217" 
    	    .DeviceID = "Canon 2520 test UFRII W10" 
    	    .Location = "TTEST W10"
    	    .Default = 1
    	    .Network = True
    	    .Shared = False
    	    .Put_"
            End With
    
       End Sub
    Last edited by Poppa Mintin; Feb 25th, 2021 at 08:44 AM.
    Along with the sunshine there has to be a little rain sometime.

  5. #5
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,031

    Re: Add printer in windows 10 problem

    Hi again Xirica,

    Do you know how to 'Step through' your code ?

    You can 'Set a BreakPoint', on Form1.vb (any Form .vb) place your cursor in the pale blue edge (to the immediate left of the code), opposite the command 'With objNewPort' and click. This will place a (red dot) breakpoint at that command.

    Now run your project and click Button1, the program will stop at that point and there will be a panel on the right showing the values of any variables at that point in your program.
    You may have to find 'Locals' and 'Autos' to see the values. You can also use 'Watch' (There are four of those) in which you can specify just the variables that you're interested in, Global, or Local to the particular subroutine or function.

    There will be buttons (or use function keys) to 'Step through' your code, line by line so that you can see what's happening.


    Poppa


    Oh!

    Just click the red dot again to remove the breakpoint.

    Pop
    Last edited by Poppa Mintin; Feb 25th, 2021 at 08:35 AM.
    Along with the sunshine there has to be a little rain sometime.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Re: Add printer in windows 10 problem

    Quote Originally Posted by Poppa Mintin View Post
    Hi again Xirica,

    Do you know how to 'Step through' your code ?

    You can 'Set a BreakPoint', on Form1.vb (any Form .vb) place your cursor in the pale blue edge (to the immediate left of the code), opposite the command 'With objNewPort' and click. This will place a (red dot) breakpoint at that command.

    Now run your project and click Button1, the program will stop at that point and there will be a panel on the right showing the values of any variables at that point in your program.
    You may have to find 'Locals' and 'Autos' to see the values. You can also use 'Watch' (There are four of those) in which you can specify just the variables that you're interested in, Global, or Local to the particular subroutine or function.

    There will be buttons (or use function keys) to 'Step through' your code, line by line so that you can see what's happening.


    Poppa


    Oh!

    Just click the red dot again to remove the breakpoint.

    Pop
    Thanks alot Poppa, I will test it out and let know here if it worked.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Re: Add printer in windows 10 problem

    Hi Poppa,

    So I've been trying and both codes (mine and yours) and both works well in the machine (PC) im developing the software.

    When I Publish the project and install it in another machine with Windows 10(64bits) it crashes, the strange thing is the same project intalled in Windows 7(32bits) works very well.

    I check and all machines have the .NET Framework 4.7.2 (that's the target framework I want).

    I dunno what I'm doing wrong, any help? Thanks again.
    Last edited by xirica; Mar 1st, 2021 at 07:54 AM.

  8. #8
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,031

    Re: Add printer in windows 10 problem

    Hi Xirica,

    I'm sorry Xirica that's beyond my knowledge.

    Short of checking that the application is set for all CPUs I don't know what to suggest.

    Form1.vb: Tab 'Project' > '(Name of project) Properties' (Bottom of menu) > Compile (or Debug).
    Check 'Platform' says 'Active (Any CPU)'. (Platform is on the top line)

    I don't know what to do if it doesn't say 'Active (Any CPU)', I suspect that it's set when you first make the project.

    Hopefully someone who can answer this for you will jump in with a post.


    Poppa


    Oh !
    Just a thought... Is the printer driver compatible with Win.10 ?

    Pop
    Last edited by Poppa Mintin; Mar 1st, 2021 at 09:03 AM.
    Along with the sunshine there has to be a little rain sometime.

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Re: Add printer in windows 10 problem

    I don't know what to do if it doesn't say 'Active (Any CPU)', I suspect that it's set when you first make the project.
    Was one of the first thing I check and it is active.

    Just a thought... Is the printer driver compatible with Win.10 ?
    Yes it is compatible W10.


    Thanks alot Poppa, I will keep trying and mabye I come with a solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width