Converting string to unmanaged byte pointer RRS feed

  • Question

  • Hi,

    I'm trying to use this class: http://support.microsoft.com/kb/322091 from Microsoft (sending raw data to a printer). I'm using the method SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount). When using the method it returns true (= successfully) but nothing comes out from the printer.

    Now I'm unsecure if it is the right way I'm converting the string to the IntPtr. Can anyone help me to see if I've converted correctly?

    My code:

    private void button3_Click(object sender, EventArgs e)
                string printerToUse = cbPrinter.Text;
                System.Text.Encoding enc = System.Text.Encoding.ASCII;
                byte[] myByteArray = enc.GetBytes("LD11PGpGSGA21F8405005050007000199590101Q0001E");
                //string myString = enc.GetString(myByteArray);
                // Initialize unmanged memory to hold the array.
                int size = Marshal.SizeOf(myByteArray[0]) * myByteArray.Length;
                IntPtr pnt = Marshal.AllocHGlobal(size);
                    // Copy the array to unmanaged memory.
                    Marshal.Copy(myByteArray, 0, pnt, myByteArray.Length);
                    // Free the unmanaged memory.
                //RawPrinterHelper.SendBytesToPrinter(printerToUse, pnt, size);
                RawPrinterHelper.SendBytesToPrinter(printerToUse, pnt, myByteArray.Length);

    Kind regards
    simsen :-)
    • Moved by Helen Zhou Tuesday, November 1, 2011 1:17 AM (From:Windows Forms General)
    Wednesday, October 26, 2011 11:56 AM

All replies

  • Without being 100% if your way should work or not, I would have just pinned the byte array:

    GCHandle h = GCHandle.Alloc(myByteArray, GCHandleType.Pinned);
    IntPtr pnt = h.AddrOfPinnedObject();
    RawPrinterHelper.SendBytesToPrinter(printerToUse, pnt, myByteArray.Length);

    Jose R. MCP
    Wednesday, October 26, 2011 1:07 PM
  • Hi,

    There is no problem in converting the string to IntPtr.

    You are just freeing the pointer even before you are using it, I mean before passing it to the RawPrinterHelper.SendBytesToPrinter function.

    You need to move this statement to try block.


    • Proposed as answer by SVentrapragada Wednesday, October 26, 2011 3:27 PM
    • Unproposed as answer by simsen Thursday, October 27, 2011 11:44 AM
    Wednesday, October 26, 2011 3:27 PM
  • The code in that kb is useless.  You'd spend months correcting the errors and it still wouldn't work on Win7.  There's current code (sep 2011) using the PrintForm VB6 Printer on codeproject.

    • Edited by JohnWein Wednesday, October 26, 2011 5:53 PM
    Wednesday, October 26, 2011 5:51 PM
  • webJose

    Thank you for your answer. I try you suggestion but it didn't work. It don't come with any errors, but still don't print anything out.



    Thank you for your answer. I moved the RawPrinterHelper.SendBytesToPrinter function into the try block. But still it doesn't print anything out and no errors.



    Thank you for your answer. I tried looking for that code on codeproject but cannot find it. Do you have a link to the code?


    Kind regards,
    simsen :-)

    Thursday, October 27, 2011 11:49 AM
  • Actually the date of the article was sep 2010:

    POS Printing: The Easy Way

    Thursday, October 27, 2011 2:07 PM
  • Hi simsen,

    This question is more related to Common Language Runtime forum, I'm moving it there to help it get better support. Thank you for your understanding.


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 28, 2011 5:39 AM