prnadmin.dll via Win32::OLE

Discussion in 'Perl Misc' started by alfredo.blanco@verizon.net, Jul 29, 2005.

  1. Guest

    I am stuck on line 283 of the followig script.

    I am trying to put together a Perl/TK utility to add printers to given
    server. But I am getting "80070709: "The printer name is invalid" while
    trying to add the Port. It doesn't even get up to the add printer
    section (which is why I am double confused by this error"


    Please, Please, Please nobody reply /w "why don't you just use
    VBscript" ...the reasons are numerous and should be self explanitory.
    :)

    Please advise.

    <SCRIPT>
    #
    ======================================================================
    #
    # NAME: AddPrinter.pl
    # VERSION: 0.1a
    #
    # AUTHOR: Alfredo Blanco, 1st Technologies, Inc.
    # DATE: 7/28/05
    #
    # PURPOSE: To Automate adding of printers to a server
    #
    # REQUIRES: NOTES: Perl 5.8+, Requires prnadmin.dll from Windows
    Resource Kit
    #
    ======================================================================



    # Load Modules
    # use warnings;
    use strict;
    use Win32::OLE qw(in with OVERLOAD);
    $Win32::OLE::Warn = 3;
    # Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);
    Win32::OLE->Initialize(Win32::OLE::COINIT_APARTMENTTHREADED);
    use Net::ping;
    # use Config::IniFiles;
    use Tk;
    require Tk::Message;
    require Tk::BrowseEntry;



    # Declare Variables & Setup environment
    # === Product Information
    ==============================================
    my $scriptname = "Add Printer";
    my $scriptver = "1.0";
    my $scriptauth = "Alfredo Blanco";
    my $scriptcopy = "1st Technologies, Inc.";
    # === Public Main Windows Objects ============================
    my $main_window;
    my @topwinsize= qw (420 190 420 190);
    my $server_entry;
    my $driver_entry;
    my $printer_entry;
    my $host_entry;
    my $location_entry;
    my $comment_entry;
    # === Public Vars
    ======================================================
    my $server;
    my $driver;
    my $printer;
    my $host;
    my $location;
    my $comment;
    my @drivers;
    my $oleerror='false';
    # === Objects
    ==========================================================
    my $oPing;
    my $oPrnAdmin;
    my $oPort;
    my $oPrinter;
    #
    ======================================================================
    my $rawport = '9100';
    my $helpfile;
    my $pingtimeout=3;
    my @printservers = qw (
    <SERVER LIST>
    );
    my @required = qw (
    PRNADMIN.DLL
    PRINTUI.DLL
    RUNDLL32.EXE
    );
    #
    ======================================================================


    # Create Global Objects
    $oPing = Net::ping->new("icmp",($pingtimeout));
    $oPrnAdmin = Win32::OLE->new("PrintMaster.PrintMaster.1");
    if(Win32::OLE->LastError() != 0) {
    $oleerror=Win32::FormatMessage(Win32::OLE->LastError);
    }
    $oPort = Win32::OLE->new("Port.Port.1");
    if(Win32::OLE->LastError() != 0) {
    $oleerror=Win32::FormatMessage(Win32::OLE->LastError);
    }
    $oPrinter = Win32::OLE->new("Printer.Printer.1");
    if(Win32::OLE->LastError() != 0) {
    $oleerror=Win32::FormatMessage(Win32::OLE->LastError);
    }



    # Main
    &drawmain();



    sub drawmain {

    # Top Level Window
    $main_window=MainWindow->new;
    $main_window->minsize($topwinsize[0], $topwinsize[1]);
    $main_window->maxsize($topwinsize[2], $topwinsize[3]);
    $main_window->title("$scriptname v$scriptver");
    $main_window->configure();

    # Main windows Frames
    my $r1_frame=$main_window->Frame()->pack(-side=>'top', -fill=>'both',
    pady=>'2');
    my $r2_frame=$main_window->Frame()->pack(-side=>'top', -fill=>'both',
    pady=>'2');
    my $r3_frame=$main_window->Frame()->pack(-side=>'top', -fill=>'both',
    pady=>'2');
    my $r4_frame=$main_window->Frame()->pack(-side=>'top', -fill=>'both',
    pady=>'2');
    my $r5_frame=$main_window->Frame()->pack(-side=>'top', -fill=>'both',
    pady=>'2');
    my $r6_frame=$main_window->Frame()->pack(-side=>'top', -fill=>'both',
    pady=>'2');
    my $r7_frame=$main_window->Frame()->pack(-side=>'top', -fill=>'both',
    pady=>'2');
    my $r7c1_frame=$r7_frame->Frame()->pack(-side=>'left', -expand=>'x',
    -fill=>'both', padx=>'2');
    my $r7c2_frame=$r7_frame->Frame()->pack(-side=>'left', -expand=>'x',
    -fill=>'both', padx=>'2');


    # Build Menu Bar
    $main_window->configure(-menu =>my $menubar =
    $main_window->Menu(-tearoff => 'yes'));
    my $file_menu = $menubar->cascade(qw/-label File -underline 0
    -menuitems/ =>[
    [command => 'E~xit',
    -command => sub{$main_window->destroy}],
    ], -tearoff => '');

    my $edit_menu = $menubar->cascade(qw/-label Edit -underline 0
    -menuitems/ =>[
    [command => 'Cu~t',
    -accelerator => 'Ctrl+X',
    -command => \&editmenu_cut],

    [command => 'C~opy',
    -accelerator => 'Ctrl+C',
    -command => \&editmenu_copy],

    [command => 'P~aste',
    -accelerator => 'Ctrl+V',
    -command => \&editmenu_paste],
    '',

    [command => '~Preferences',
    -command => \&editmenu_preferences],
    ], -tearoff => '');

    my $help_menu = $menubar->cascade(qw/-label Help -underline 0
    -menuitems/ =>[
    [command => '~Help',
    -accelerator => 'F1',
    -command => \&helpmenu_help],
    '',

    [command => '~About',
    -command => \&helpmenu_about],
    ], -tearoff => '');


    # Main Windows widgets
    my $server_label=$r1_frame->Label(
    -text=>"Server:",
    -width=>8,
    -anchor=>'w')->pack(side=>'left', anchor=>'w');

    $server_entry=$r1_frame->BrowseEntry(
    -state => 'readonly',
    -choices=>\@printservers,
    -variable => \$server,
    -browsecmd => \&getdriverlist,
    -width=>20)->pack(side=>'left', anchor=>'w');

    my $driver_label=$r2_frame->Label(
    -text=>"Driver:",
    -width=>8,
    -anchor=>'w')->pack(side=>'left', anchor=>'w');

    $driver_entry=$r2_frame->BrowseEntry(
    -state => 'readonly',
    -choices=>\@drivers,
    -variable => \$driver,
    -width=>40)->pack(side=>'left', anchor=>'w');

    my $add_driver_button=$r2_frame->Button(
    -width=>2,
    -height=>1,
    -command=>sub{
    if ($server) {
    system ("rundll32 printui.dll,PrintUIEntry /s /t2 /n\\\\$server");
    } else {
    &error_popup ("Please select a server from the list\n");
    }
    })->pack(-side=>'left', anchor=>'w', -padx=>6);

    my $printer_label=$r3_frame->Label(
    -text=>"Printer:",
    -width=>8,
    -anchor=>'w')->pack(side=>'left', anchor=>'w');

    $printer_entry=$r3_frame->Entry(
    -textvariable => \$printer,
    -width=>20,
    -justify=>'left')->pack(side=>'left', anchor=>'w', -padx=>6);

    my $host_label=$r4_frame->Label(
    -text=>"Host:",
    -width=>8,
    -anchor=>'w')->pack(side=>'left', anchor=>'w');

    $host_entry=$r4_frame->Entry(
    -textvariable => \$host,
    -width=>60)->pack(side=>'left', anchor=>'w', -padx=>6);

    my $location_label=$r5_frame->Label(
    -text=>"Location:",
    -width=>8,
    -anchor=>'w',
    -justify=>'right')->pack(side=>'left', anchor=>'w');

    $location_entry=$r5_frame->Entry(
    -textvariable => \$location,
    -width=>60)->pack(side=>'left', fill=>'x', anchor=>'w', -padx=>6);

    my $comment_label=$r6_frame->Label(
    -text=>"Comment:",
    -width=>8,
    -anchor=>'w',
    -justify=>'right')->pack(side=>'left', anchor=>'w');

    $comment_entry=$r6_frame->Entry(
    -textvariable => \$comment,
    -width=>60)->pack(side=>'left', anchor=>'w', -padx=>6);


    # Buttons
    my $cancel_button=$r7c1_frame->Button(
    -text=>'Cancel',
    -width=>5,
    -height=>1,
    -command=>sub{$main_window->destroy})->pack(-side=>'right',
    anchor=>'e', padx=>'3', pady=>'3');

    my $ok_button=$r7c2_frame->Button(
    -text=>'OK',
    -width=>5,
    -height=>1,
    -command=>\&addprinter)->pack(-side=>'left', anchor=>'w', padx=>'3',
    pady=>'3');


    &keybindings();
    if ($oleerror ne 'false') {&oleerrorsub};
    foreach (@required) {
    &checkdep("$_");
    }
    &MainLoop();
    }



    sub addprinter {
    print " Server: $server\n";
    print " Driver: $driver\n";
    print " Printer: $printer\n";
    print " Host: $host\n";
    print "Location: $location\n";
    print " Comment: $comment\n";

    # Add Port
    # $oPort->{ServerName} = $server;
    $oPort->{PortName} = $printer;
    $oPort->{PortType} = 3;
    $oPort->{HostAddress} = $host;
    $oPort->{PortNumber} = $rawport;
    $oPort->{SNMP} = 0;
    $oPrnAdmin->PortAdd($oPort);

    # Add Printer
    # $oPrinter->{ServerName} = "\\\\$server";
    # $oPrinter->{PrinterName} = $printer;
    # $oPrinter->{DriverName} = $driver;
    # $oPrinter->{PortName} = $printer;
    # $oPrnAdmin->PrinterAdd($oPrinter);
    # if(Win32::OLE->LastError() != 0) {
    # $oleerror=Win32::FormatMessage(Win32::OLE->LastError);
    # }
    }



    # Edit > Cut Menu
    sub editmenu_cut {
    my $currentwindow=$main_window->focusCurrent();
    $currentwindow->clipboardCut;
    }

    # Edit > Copy Menu
    sub editmenu_copy {
    my $currentwindow=$main_window->focusCurrent();
    $currentwindow->clipboardCopy;
    }

    # Edit > Paste Menu
    sub editmenu_paste {
    my $currentwindow=$main_window->focusCurrent();
    $currentwindow->clipboardPaste;
    }

    # Edit > Preferences Menu
    sub editmenu_preferences {
    # &drawprefs;
    }



    # Help > Help Menu
    sub helpmenu_help {
    my $reformathelpfile = $helpfile;
    system ("start /separate winhlp32.exe $helpfile");
    }

    # Help > About Menu function
    sub helpmenu_about {
    my $popup = $main_window->messageBox(
    '-icon' =>'info',
    -type =>'OK',
    -title => 'About',
    -message => "$scriptname v$scriptver\nby
    $scriptauth\n$scriptcopy");
    }



    sub keybindings {
    $main_window->bind('Tk::Entry', '<F1>', \&main::helpmenu_help);
    }



    # Error Popup
    sub error_popup {
    my $popup = $main_window->messageBox(
    '-icon' =>'error',
    -type =>'OK',
    -title => 'Error',
    -message => "$_[0]");
    }



    sub getdriverlist {
    if ($_[0]) {
    @drivers=undef;
    foreach my $oDriver (in $oPrnAdmin->Drivers("\\\\$server")) {
    push (@drivers,$oDriver->ModelName);
    }
    &updatedriverlist(@drivers);
    }
    }



    sub updatedriverlist {
    sort (@drivers);
    # reverse (@drivers);
    $driver_entry->delete(0,'end');
    $driver_entry->insert(0,@drivers);
    $driver_entry->update();
    }



    sub oleerrorsub {
    error_popup ("Error creating PrintMaster Object\nCheck if prnadmin.dll
    is properly registered\n$oleerror");
    $main_window->destroy();
    }



    sub checkdep {
    my $foundit;
    unless (-e $_[0]) {
    foreach (split /;/,($ENV{"PATH"})) {
    if (-e "$_\\$_[0]") {
    $foundit=$_;
    }
    }
    unless (-e "$foundit\\$_[0]") {
    error_popup ("Could not find $_[0]");
    $main_window->destroy();
    }
    }
    }
    , Jul 29, 2005
    #1
    1. Advertising

  2. Mothra Guest

    wrote:
    > I am stuck on line 283 of the followig script.
    >
    > I am trying to put together a Perl/TK utility to add printers to given
    > server. But I am getting "80070709: "The printer name is invalid"
    > while trying to add the Port. It doesn't even get up to the add
    > printer section (which is why I am double confused by this error"
    >


    http://perl.plover.com/Questions.html

    Mothra
    Mothra, Jul 29, 2005
    #2
    1. Advertising

  3. Guest

    Got it. I was so frustrated with this but it turned out to be a typo.
    Thanks...
    , Jul 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick.O.Ige

    Ole ole

    Patrick.O.Ige, Jul 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    468
    Patrick.O.Ige
    Jul 16, 2006
  2. C. Martin

    Killing IE via win32::ole ??

    C. Martin, Jul 25, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    102
    C. Martin
    Jul 25, 2003
  3. Lance Hoffmeyer
    Replies:
    0
    Views:
    218
    Lance Hoffmeyer
    Nov 17, 2003
  4. Tom Welch
    Replies:
    0
    Views:
    162
    Tom Welch
    May 26, 2004
  5. James
    Replies:
    4
    Views:
    518
    James
    Jan 27, 2006
Loading...

Share This Page