【发布时间】:2010-04-07 16:55:13
【问题描述】:
这是我的last question 的后续。
我现在有一个byte[] 的位图图像值。最终,我会将一个字符串传递给格式为String.Format("GW{0},{1},{2},{3},", X, Y, stride, _Bitmap.Height) + my binary data; 的打印后台处理程序,我正在使用来自here 的SendBytesToPrinter 命令。
这是我目前发送到打印机的代码
public static bool SendStringPlusByteBlockToPrinter(string szPrinterName, string szString, byte[] bytes)
{
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);
pBytes = Marshal.ReAllocCoTaskMem(pBytes, szString.Length + bytes.Length);
Marshal.Copy(bytes,0, SOMTHING GOES HERE,bytes.Length); // this is the problem line
// Send the converted ANSI string + the concatenated bytes to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
我的问题是我不知道如何将我的数据附加到字符串的末尾。任何帮助将不胜感激,如果我这样做完全错误,我可以采用完全不同的方式(例如,在移动到非托管空间之前以某种方式将二进制数据连接到字符串上。
附: 作为第二个问题,ReAllocCoTaskMem 会在调用之前将其中的数据移动到新位置吗?
【问题讨论】:
标签: c# memory unmanaged concatenation