I am creating an desktop application to print label from Cognitive Advantage DLX printer. I am trying to print line by line in Visual C#.Net. My program and printer are working properly. I had try to take "Test Print" from the printer and it gives my print test successfully. But when I try to take print from my printer it is not working. Also, I am not getting any error.
Here is my code from print class,
class LPrinter
32 {
33 // Structure and API declarions:
34 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
35 public class DOCINFOA
36 {
37 [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
38 [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
39 [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
40 }
41 [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
42 public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
43
44 [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
45 public static extern bool ClosePrinter(IntPtr hPrinter);
46
47 [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, Ch开发者_如何学运维arSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
48 public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
49
50 [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
51 public static extern bool EndDocPrinter(IntPtr hPrinter);
52
53 [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
54 public static extern bool StartPagePrinter(IntPtr hPrinter);
55
56 [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
57 public static extern bool EndPagePrinter(IntPtr hPrinter);
58
59 [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
60 public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
61
62 /*=================================================*/
63
64 private IntPtr HandlePrinter;
65 private PrinterSettings ps;
66
67 public LPrinter()
68 {
69 HandlePrinter = IntPtr.Zero;
70 ps = new PrinterSettings();
71 }
72
73 public string PrinterName
74 {
75 get
76 {
77 return ps.PrinterName;
78 }
79 set
80 {
81 ps.PrinterName = value;
82 }
83 }
84
85 public bool ChoosePrinter()
86 {
87 PrintDialog pd = new PrintDialog();
88 pd.PrinterSettings = ps;
89 if(pd.ShowDialog()==DialogResult.OK)
90 {
91 ps = pd.PrinterSettings;
92 return true;
93 }
94 else return false;
95 }
96
97 public bool Open(string DocName)
98 {
99 // see if printer is already open
100 if(HandlePrinter!=IntPtr.Zero) return false;
101
102 // opens the printer
103 bool risp = OpenPrinter(ps.PrinterName,out HandlePrinter,IntPtr.Zero);
104 if(risp==false) return false;
105
106 // starts a print job
107 DOCINFOA MyDocInfo = new DOCINFOA();
108 MyDocInfo.pDocName = DocName;
109 MyDocInfo.pOutputFile = null;
110 MyDocInfo.pDataType = "RAW";
111
112 if(StartDocPrinter(HandlePrinter, 1, MyDocInfo))
113 {
114 StartPagePrinter(HandlePrinter); //starts a page
115 return true;
116 }
117 else return false;
118 }
119
120 public bool Close()
121 {
122 if(HandlePrinter==IntPtr.Zero) return false;
123 if(!EndPagePrinter(HandlePrinter)) return false;
124 if(!EndDocPrinter(HandlePrinter)) return false;
125 if(!ClosePrinter(HandlePrinter)) return false;
126 HandlePrinter = IntPtr.Zero;
127 return true;
128 }
129
130 public bool Print(string outputstring)
131 {
132 if(HandlePrinter==IntPtr.Zero) return false;
133
134 IntPtr buf = Marshal.StringToCoTaskMemAnsi(outputstring);
135
136 Int32 done = 0;
137 bool ok = WritePrinter(HandlePrinter,buf,outputstring.Length,out done);
138
139 Marshal.FreeCoTaskMem(buf);
140
141 if(!ok) return false;
142 else return true;
143 }
144
145 }
Can any help me out.
精彩评论