<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Printing Win32 API</Title>
      <Shortcut>PrintingWin32API</Shortcut>
      <Description>Printing Win32 API [C#]</Description>
      <Author>Milan B.</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=02f52163-3b30-425b-a529-84f9b37417d8</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[	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();
	}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>