代码可以直接输出到远程打印机,但是A4纸上的却是乱码,代码如下,急求解,请高手指教!谢谢public partial class Form1 : Form
{
    public struct DOCINFO
    {
       [MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
       [MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
       [MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
    }    public class PrintDirect
    {
       [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false,
           CallingConvention = CallingConvention.StdCall)]
            public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter,
            int 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.Unicode, ExactSpelling = true,
           CallingConvention = CallingConvention.StdCall)]
            public static extern long StartPagePrinter(IntPtr hPrinter);
            [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 EndPagePrinter(IntPtr hPrinter);            [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);
        }
        
        //点击按钮,直接输出字符串到远程打印机
        private void button2_Click(object sender, EventArgs e)
        {
            if (printStr == "" || printStr == null)
            {
                MessageBox.Show("对不起,现在没有要打印的数据!");
            }
            else 
            {
                System.IntPtr lhPrinter = new System.IntPtr();                DOCINFO di = new DOCINFO();
                int pcWritten = 0;                PrintDirect.OpenPrinter("\\\\192.168.112.5\\1150PCL 5e", ref lhPrinter, 0);
                PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
                PrintDirect.StartPagePrinter(lhPrinter);                myReader = new StringReader(this.richTextBox1.Text);                try
                {
                    PrintDirect.WritePrinter(lhPrinter, ds.Tables["print"].Rows[0]["data"].ToString(), printStr.Length, ref pcWritten);
                }
                catch (Exception ee)
                {
                    Console.WriteLine(ee.Message);
                }                PrintDirect.EndPagePrinter(lhPrinter);
                PrintDirect.EndDocPrinter(lhPrinter);
                PrintDirect.ClosePrinter(lhPrinter);
            }
        }
}PS:
打印代码为:
PrintDirect.WritePrinter(lhPrinter, ds.Tables["print"].Rows[0]["data"].ToString(), printStr.Length, ref pcWritten);ds.Tables["print"].Rows[0]["data"].ToString()//这是一段带格式的字符串

解决方案 »

  1.   

    ms-help://MS.MSDNQTR.2003FEB.2052/gdi/prntspol_93g2.htm
      

  2.   

    楼上兄弟,你给的连接在MSDN2005里面打不开啊
      

  3.   

    ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.WIN32COM.v10.en/gdi/prntspol_93g2.htm
      

  4.   

    谢谢楼上的兄弟,我把找到的代码贴出来,还有问题请教一下:// RawDataToPrinter - sends binary data directly to a printer
          // 
          // szPrinterName: NULL-terminated string specifying printer name
          // lpData:        Pointer to raw data bytes
          // dwCount        Length of lpData in bytes
          // 
          // Returns: TRUE for success, FALSE for failure.
          // 
           BOOL RawDataToPrinter(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
          {
            HANDLE     hPrinter;
            DOC_INFO_1 DocInfo;
            DWORD      dwJob;
            DWORD      dwBytesWritten;        // Need a handle to the printer.
            if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) )
              return FALSE;        // Fill in the structure with info about this "document."
            DocInfo.pDocName = "My Document";
            DocInfo.pOutputFile = NULL;
            DocInfo.pDatatype = "RAW";
            // Inform the spooler the document is beginning.
            if( (dwJob = StartDocPrinter( hPrinter, 1, (LPSTR)&DocInfo )) == 0 )
            {
              ClosePrinter( hPrinter );
              return FALSE;
            }
            // Start a page.
            if( ! StartPagePrinter( hPrinter ) )
            {
              EndDocPrinter( hPrinter );
              ClosePrinter( hPrinter );
              return FALSE;
            }
            // Send the data to the printer.
            if( !WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
            {
              EndPagePrinter( hPrinter );
              EndDocPrinter( hPrinter );
              ClosePrinter( hPrinter );
              return FALSE;
            }
            // End the page.
            if( ! EndPagePrinter( hPrinter ) )
            {
              EndDocPrinter( hPrinter );
              ClosePrinter( hPrinter );
              return FALSE;
            }
            // Inform the spooler that the document is ending.
            if( ! EndDocPrinter( hPrinter ) )
            {
              ClosePrinter( hPrinter );
              return FALSE;
            }
            // Tidy up the printer handle.
            ClosePrinter( hPrinter );
            // Check to see if correct number of bytes were written.
            if( dwBytesWritten != dwCount )
              return FALSE;
            return TRUE;
          }BOOL RawDataToPrinter(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount)LPSTR szPrinterName 是不是远程打印机名称?格式是"\\\\打印IP\\打印机器名" ?
    LPBYTE lpData 指什么?格式是什么?
    DWORD dwCount 是不是指输出的字符串,可不可以是带格式的字符串?格式是怎么样的?
      

  5.   

    编码问题吧?CharSet 里改一下看看
    1、试下byte[]  编码格式,
    这个api用的是本机编码的吧,在形成byte[]时用Encoding的unicode编码,或者UTF8编码试下,在System.Text里,
    不过得将声明改一下
    [DllImport("winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.??, SetLastError=true, ExactSpelling=true)]
     public static extern bool WritePrinter(IntPtr hPrinter, byte[] pBytes, int dwCount, ref int dwWritten);2、可能是驱动问题,驱动程序不正确就打印不了中文!因为很多激光和喷墨打印机都没有汉字库,所以无法直接打印中文,在Windows下只有借助驱动程序,打印机才可以打印中文!
      

  6.   

    try
    LPBYTE lpData ===> System.IntPtr;
    string str = ...
    System.IntPtr lpdata = System.Text.Encoding.Default.GetBytes( str );
      

  7.   

    谢谢,楼上兄弟,请教一下:
    找不到类型或命名空间名称“LPSTR”(是否缺少 using 指令或程序集引用?) 在MSDN里面找到的是:using System.Runtime.InteropServices; 
    但是没用麻烦告诉一下命名空间
      

  8.   

    我想把需要打印的带格式的字符串生成到word文档里面,然后打印这个word,这样的话,格式比较好控制
    请高手兄弟们指教啊!