0
這是我的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;
}
我的問題是我不知道如何使追加到字符串的結尾我的數據。任何幫助將不勝感激,如果我這樣做是完全錯誤的,我很好地採取一種完全不同的方式(例如以某種方式在將二進制數據連接到字符串之前移動到非託管空間前
PS 作爲第二個問題,ReAllocCoTaskMem會在呼叫新位置之前移動位於其中的數據嗎?
難道我不需要移動緩衝區出來的託管代碼的調用。示例微軟給出的kb使用'公共靜態extern bool WritePrinter(IntPtr hPrinter,IntPtr pBytes,Int32 dwCount,out Int32 dwWritten);' – 2010-04-07 17:27:45
編組字節數組有很多種方法。我沒有測試它,但在方法簽名中用'byte []'替換'IntPtr'應該可以工作。 – dtb 2010-04-07 17:53:36
我用你的方法,但有一個跟進問題,我遇到了一個問題。 http://stackoverflow.com/questions/2595393/ – 2010-04-07 19:45:37