Hi,
My printer supports UTF8 data and I need to convert a string which contains UTF-8 data to IntPtr. Below is the original code.
public static bool SendStringToPrinter(string szPrinterName,
string docname,
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, docname, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
As you can see, it is assume the string contains only ASCII but this will not work for my UTF-8 data. I would greatly appreciate if you can advice me how to do the trick to make it work for String with UTF-8 data.
Thanks and best regards,

Sending UTF-8 data to Printer Driver
BlackManWah
Begemot
Your code snippet is from the example How to send raw data to a printer by using Visual C# .NET .
Change Marshal.StringToCoTaskMemAnsi to Marshal.StringToCoTaskMemUni and in the line where Charset=Ansi, try Charset=Auto or something more appropriate.
Regards
Peca
markovuksanovic
Thanks Peca. You are indeed helpful.
Anyway, I have solved the problem myself. I have tried your approach but somehow it didn't work.
I used Marshall.copy() to solve the problem after I converted the string into Unicode bytes.
Good day.