声明为:
[DllImport("winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
 public static extern bool WritePrinter(IntPtr hPrinter, byte[] pBytes, int dwCount, ref int dwWritten);
调用:
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten); 但是只要打印内容是中文出来的就是乱码,英文字母和数字则没有问题
请问各位高手  怎么解决

解决方案 »

  1.   

    byte[] pBytes 编码格式的问题,
    这个api用的是本机编码的吧,windows问unicode
    在形成byte[]时用Encoding的unicode编码,
    不行的话再用用别的编码形式试试
      

  2.   

    代码为:
    public static bool SendBytesToPrinter(string szPrinterName, byte[] pBytes, Int32 dwCount) 

    IntPtr hPrinter =  System.IntPtr.Zero; 
    Int32 dwError; 
    DOCINFOW di = new DOCINFOW(); 
    Int32 dwWritten = 0; 
    bool bSuccess; 
    di.pDocName = "Document"; 
    di.pDataType = "RAW"; 
    bSuccess = false; 
    if (OpenPrinter(szPrinterName,ref hPrinter, 0)) 

    if (StartDocPrinter(hPrinter, 1,ref di)) 

    if (StartPagePrinter(hPrinter)) 

    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten); 
    EndPagePrinter(hPrinter); 

    EndDocPrinter(hPrinter); 

    ClosePrinter(hPrinter); 

    if (bSuccess == false) 

    dwError = Marshal.GetLastWin32Error(); 

    return bSuccess;  }  public static void SendStringToPrinter(string szPrinterName, string szString) 
    //调用

    IntPtr pBytes; 
    Int32 dwCount; 
    byte [] f=Encoding.Unicode.GetBytes(szString);
    dwCount = f.Length;
    SendBytesToPrinter(szPrinterName, f, dwCount); 
    }
      

  3.   

    我在转换byte []  的时候就是用的 Unicode编码转换的
    可是打出来还是不行
      

  4.   

    我用的是EPSON KIII 的可以打印中文,代码和你的一样!
      

  5.   

    我用的是  西铁城的GSX-230
      

  6.   

    可能是驱动问题,驱动程序不正确就打印不了中文!因为很多激光和喷墨打印机都没有汉字库,所以无法直接打印中文,在Windows下只有借助驱动程序,打印机才可以打印中文!
      

  7.   

    我也不知道是什么原因造成的
    如果是打印机的问题
    谁知道西铁城的GSX-230
    的设置方法
      

  8.   

    我用的EPSON是针式的,机子上有宋体可调,不知道你的是不是激光的还是喷墨打印机.
      

  9.   

    驱动程序安装正确吗?不用API代码打印,其它文档打印正不正常?
      

  10.   

    西铁城的GSX-230是个老机子
    没找到说明书
    用WOOD打印中文没有问题
      

  11.   

    因为API是直接打开打印机端口打印的,所以没有调用驱动程序,可能你的打印机没的中文字库,所以打不了!具体编码转换我也不是太懂!
      

  12.   

    楼主肯定是在做小票打印机的,呵呵,一般直接用wRiteFile,写口就行,速度很快的,一秒钟就可以打出来,:)
    网上有相关类,你找一下就好另外如果你用的西铁城的话,必须找他的手册 如果不写口,
    直接用OPOS就好了
    在C#写几句话就完了,但速度肯定没有写口的快.