Marshal.StringToCoTaskMemAnsi converting non-Latin characters when sending raw data to a printer
Posted
by rem
on Stack Overflow
See other posts from Stack Overflow
or by rem
Published on 2010-04-25T16:20:09Z
Indexed on
2010/04/25
16:23 UTC
Read the original article
Hit count: 536
For sending raw data to a thermal DATAMAX printer I'm using RawPrinterHelper
class from this Microsoft KB article.
When a string sent to printer contains only Latin characters, everything is OK. But non-Latin, in my case Russian characters in a string, are not printed correct. I think the problem is in using Marshal.StringToCoTaskMemAnsi
method for converting the string:
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
Just to note, Russian characters in the string are put in hex format, like "\x83", but nevertheless the method doesn't put this hex value in unmanaged memory as it is, but converts it, I think, according with ANSI code page to a character and then printer can not read it correctly.
If I try to compose a file, using Hex editor and put correct hex values in place of non-Latin characters and then send the file to a printer using another method from the same class SendFileToPrinter
, everything, including Russian characters is printed correctly.
How in this case the problem with sending string, containing non-Latin characters, could be solved?
© Stack Overflow or respective owner