powershell - openprinter with winspool.drv not working -


as part of project learning how handle printers using "winspool.drv" , "printui.dll,printuientry" new. using powershell project. problem code throwing error when trying printer handle openprinter method error: method invocation failed because [openprinter1.program1] not contain method named 'openprinterhandle'.

code:

$code = @'   using system;   using system.collections.generic;   using system.componentmodel;   using system.drawing;   using system.linq;   using system.text;   using system.runtime.interopservices;   using system.io;      namespace openprinter1     {    public class program1     {     [dllimport("winspool.drv", entrypoint = "openprinter", setlasterror =true)]      internal static extern bool openprinter(string pprintername, ref intptr   phprinter, printer_defaults pdefault);      [dllimport("winspool.drv", entrypoint = "closeprinter", setlasterror = true)]   internal static extern int closeprinter(intptr hprinter);    [structlayout(layoutkind.sequential)]   public class printer_defaults   {   public string pdatatype;   public intptr pdevmode;   public int desiredaccess;   }   public struct openprinteraccesscodes {   public const int delete = 0x10000; // delete - allowed delete printers   public const int read_control = 0x20000; // read_control -    public const int write_dac = 0x40000; // write_dac -    public const int write_owner = 0x80000; // write_owner -    public const int server_access_administer = 0x1;   public const int server_access_enumerate = 0x2;   public const int printer_access_administer = 0x4;   public const int printer_access_use = 0x8;   public const int standard_rights_required = 0xf0000;   public const int printer_all_access = (standard_rights_required |printer_access_administer | printer_access_use);   public const int server_all_access = (standard_rights_required | server_access_administer | server_access_enumerate);      public const int max_portname_len = 64;     public const int max_networkname_len = 49;     public const int max_snmp_community_str_len = 33;     public const int max_queuename_len = 33;     public const int max_ipaddr_str_len = 16;      public const int error_insufficient_buffer = 122;     public const int error_invalid_flags = 1004;     }     public intptr openprinterhandle(string printername)     {     var def = new printer_defaults { pdatatype = null, pdevmode = intptr.zero, desiredaccess = openprinteraccesscodes.printer_all_access };     var hprinter = intptr.zero;     if (!openprinter(printername, ref hprinter, def))     {         var lastwin32error = new win32exception(marshal.getlastwin32error());          throw lastwin32error;     }     return hprinter; } } } '@   cls   add-type -typedefinition $code -language csharp       if(!([openprinter1.program1]:: openprinterhandle("hp laserjet 1320 pcl 6 (copy 1)")))     {     throw (new-object componentmodel.win32exception )     } 

this syntax:

[namespace.class]::method() 

denotes invocation of static method. openprinterhandle() method in type definition instance method.

to change behavior, introduce static keyword in method signature:

public static intptr openprinterhandle(string printername) 

and should able do:

[openprinter1.program1]::openprinterhandle($printername) 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -