将字符串和字节数组连接到非托管内存中

发布于 2024-08-28 00:29:49 字数 1310 浏览 6 评论 0原文

这是我的最后一个问题的后续问题。

我的位图图像现在有一个 byte[] 值。最终,我将向打印后台处理程序传递一个格式为 String.Format("GW{0},{1},{2},{3},", X, Y, stride, _Bitmap.Height ) + 我的二进制数据; 我正在使用 中的 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 是否会将调用之前位于其中的数据移动到新位置?

This is a followup to my last question.

I now have a byte[] of values for my bitmap image. Eventually I will be passing a string to the print spooler of the format String.Format("GW{0},{1},{2},{3},", X, Y, stride, _Bitmap.Height) + my binary data; I am using the SendBytesToPrinter command from here.

Here is my code so far to send it to the printer

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;
}

My issue is I do not know how to make my data appended on to the end of the string. Any help would be greatly appreciated, and if I am doing this totally wrong I am fine in going a entirely different way (for example somehow getting the binary data concatenated on to the string before the move to unmanaged space.

P.S.
As a second question, will ReAllocCoTaskMem move the data that is sitting in it before the call to the new location?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-09-04 00:29:49

我建议您尽可能留在受管理的空间内。使用 Encoding.ASCII< 将字符串转换为字节数组/a>,连接两个字节数组,然后使用结果调用本机方法。

byte[] ascii = Encoding.ASCII.GetBytes(szString);

byte[] buffer = new buffer[ascii.Length + bytes.Length];
Buffer.BlockCopy(ascii, 0, buffer, 0, ascii.Length);
Buffer.BlockCopy(bytes, 0, buffer, ascii.Length; bytes.Length);

...
bool success = WritePrinter(printer, buffer, buffer.Length, out written);
...

[DllImport("winspool.drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, byte[] pBytes, int dwCount, out int dwWritten);

I recommend you try to stay in managed space as much as possible. Convert the string to a byte array using Encoding.ASCII, concatenate the two byte arrays and then invoke the native method with the result.

byte[] ascii = Encoding.ASCII.GetBytes(szString);

byte[] buffer = new buffer[ascii.Length + bytes.Length];
Buffer.BlockCopy(ascii, 0, buffer, 0, ascii.Length);
Buffer.BlockCopy(bytes, 0, buffer, ascii.Length; bytes.Length);

...
bool success = WritePrinter(printer, buffer, buffer.Length, out written);
...

with

[DllImport("winspool.drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, byte[] pBytes, int dwCount, out int dwWritten);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文