CodeXchange Wednesday, January 17, 2018
Home
What's it?
Download
Register
My Snippets
Add Snippet
Search
Faq
Contact Us
Sponsors

Website hosted by


Code Snippet Preview

Review the code snippet before inserting it on your project.

Snippet Metadata
Summary: Printing Win32 API
Language: C#
Author: Milan B.
Author snippets RSS:
Culture: en-US
Bytes: 6698
Visual Studio 2005 Snippet:

Snippet Stats
Downloads: 3
Overall rating : 0
Average rating : Snippet rating

Snippet Preview
Code:
   public class NativeMethods
   {
      static NativeMethods()
      {
      }

      [FlagsAttribute]
      public enum PrinterEnumFlags 
      {
         PRINTER_ENUM_DEFAULT     = 0x00000001,
         PRINTER_ENUM_LOCAL       = 0x00000002,
         PRINTER_ENUM_CONNECTIONS = 0x00000004,
         PRINTER_ENUM_FAVORITE    = 0x00000004,
         PRINTER_ENUM_NAME        = 0x00000008,
         PRINTER_ENUM_REMOTE      = 0x00000010,
         PRINTER_ENUM_SHARED      = 0x00000020,
         PRINTER_ENUM_NETWORK     = 0x00000040,
         PRINTER_ENUM_EXPAND      = 0x00004000,
         PRINTER_ENUM_CONTAINER   = 0x00008000,
         PRINTER_ENUM_ICONMASK    = 0x00ff0000,
         PRINTER_ENUM_ICON1       = 0x00010000,
         PRINTER_ENUM_ICON2       = 0x00020000,
         PRINTER_ENUM_ICON3       = 0x00040000,
         PRINTER_ENUM_ICON4       = 0x00080000,
         PRINTER_ENUM_ICON5       = 0x00100000,
         PRINTER_ENUM_ICON6       = 0x00200000,
         PRINTER_ENUM_ICON7       = 0x00400000,
         PRINTER_ENUM_ICON8       = 0x00800000,
         PRINTER_ENUM_HIDE        = 0x01000000
      }

      [StructLayout(LayoutKind.Sequential)]
      public struct PRINTER_INFO_2
      {
         [MarshalAs(UnmanagedType.LPStr)] public string pServerName; 
         [MarshalAs(UnmanagedType.LPStr)] public string pPrinterName; 
         [MarshalAs(UnmanagedType.LPStr)] public string pShareName; 
         [MarshalAs(UnmanagedType.LPStr)] public string pPortName; 
         [MarshalAs(UnmanagedType.LPStr)] public string pDriverName; 
         [MarshalAs(UnmanagedType.LPStr)] public string pComment; 
         [MarshalAs(UnmanagedType.LPStr)] public string pLocation; 
         public IntPtr pDevMode; 
         [MarshalAs(UnmanagedType.LPStr)] public string pSepFile; 
         [MarshalAs(UnmanagedType.LPStr)] public string pPrintProcessor; 
         [MarshalAs(UnmanagedType.LPStr)] public string pDatatype; 
         [MarshalAs(UnmanagedType.LPStr)] public string pParameters; 
         public IntPtr pSecurityDescriptor; 
         public Int32 Attributes; 
         public Int32 Priority; 
         public Int32 DefaultPriority; 
         public Int32 StartTime; 
         public Int32 UntilTime; 
         public Int32 Status; 
         public Int32 cJobs; 
         public Int32 AveragePPM; 
      }

      [StructLayout( LayoutKind.Sequential)]
      public struct DOCINFO 
      {
         [MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
         [MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile; 
         [MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
      } 

      [StructLayout(LayoutKind.Sequential)]
      public struct PRINTER_DEFAULTS
      {
         public int pDatatype;
         public int pDevMode;
         public int DesiredAccess;
      }
      [StructLayout( LayoutKind.Sequential)]
      public struct DEVMODE
      {
         [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
         public string dmDeviceName;
         public short  dmSpecVersion;
         public short  dmDriverVersion;
         public short  dmSize;
         public short  dmDriverExtra;
         public int    dmFields;


         public short dmOrientation;
         public short dmPaperSize;
         public short dmPaperLength;
         public short dmPaperWidth;


         public short dmScale;
         public short dmCopies;
         public short dmDefaultSource;
         public short dmPrintQuality;
         public short dmColor;
         public short dmDuplex;
         public short dmYResolution;
         public short dmTTOption;
         public short dmCollate;
         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
         public string dmFormName;
         public short dmLogPixels;
         public short dmBitsPerPel;
         public int   dmPelsWidth;
         public int   dmPelsHeight;


         public int   dmDisplayFlags;
         public int   dmDisplayFrequency;


         public int   dmICMMethod;
         public int   dmICMIntent;
         public int   dmMediaType;
         public int   dmDitherType;
         public int   dmReserved1;
         public int   dmReserved2;


         public int   dmPanningWidth;
         public int   dmPanningHeight;
      };

      [DllImport("Kernel32.dll", EntryPoint="CloseHandle")]
      private static extern int CloseHandle(int hObject);


      [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false, CallingConvention=CallingConvention.StdCall )]
      public static extern bool OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault);

      [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false, CallingConvention=CallingConvention.StdCall )]
      public static extern bool OpenPrinter(string pPrinterName, ref IntPtr phPrinter, ref PRINTER_DEFAULTS pDefault);

      [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false, CallingConvention=CallingConvention.StdCall )]
      public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo);

      [ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
      public static extern long WritePrinter(IntPtr hPrinter,string data, int buf,ref int pcWritten);            
      
      [ DllImport( "winspool.drv", CharSet=CharSet.Unicode,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
      public static extern long EndDocPrinter(IntPtr hPrinter);               

      [ DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true, CallingConvention=CallingConvention.StdCall )]
      public static extern long ClosePrinter(IntPtr hPrinter);

      [DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
      public static extern bool EnumPrinters(PrinterEnumFlags Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned);

      [DllImport("winspool.Drv", EntryPoint="DocumentPropertiesA", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
      public static extern int DocumentProperties (IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPStr)] string pDeviceNameg, IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode);

      [DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
      public static extern int GetPrinterDriver(IntPtr hPrinter, string pEnvironment, uint Level, IntPtr pDriverInfo, uint cbBuf, out uint pcbNeeded);

      [DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
      public static extern uint GetPrinterData(IntPtr hPrinter, string pValueName, out uint pType, byte[] pData, uint nSize, out uint pcbNeeded);

      [DllImport("winspool.Drv", EntryPoint="GetPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
      public static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel, IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);

      [DllImport("kernel32.dll", EntryPoint="GetLastError", SetLastError=false, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
      public static extern Int32 GetLastError();
   }

Snippet Comments
Comments:
No comments for this snippet

Other snippets that may interest you..
Related Snippets
TDT - GridPage - Page_Init (C#)
CustomValitaor (C#)
(C#)
Get the file extension from a file path or file name (VB.NET)
grab record from db and subtracts it from variable (VB.NET)



Copyright ©2009-2018 CodeXchange. Server version 1.0.3720.32855 Client Version 0.9.0.0

With from Barcelona

Most Helpful members
These are the members who have received the most points for their helpful samples
Zepho Zep
Robert Wagner
Galen Taylor

All time 'Hall of fame'
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
INI File Access (VB.NET)
Read XML from string into DataSet (C#)
Create Manifest File for your Application (VB.NET)
Round function to avoid banker's rounding (VB.NET)
Get Short and Long Path Names (VB.NET)
Sending Mail through authenticated SMTP server (C#)
One Way Hash for strings (C#)
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
How do I load an image from a URI address? (VB.NET)
Use our easy to use Visual Studio.NET addin client and start sharing code snippets with the CodeXchange community!
Refreshed: Wednesday, January 17, 2018