How do I call the ExtEscape with PASSTRHOUGH function from c#?

Viewed 22

I am adding RFID programming support for Zebra printers to an existing printing application. In order program the RFID chip I need to send some raw printer codes using the Windows API call ExtEscape with the PASSTHROUGH flag.

I have imported the function like this.

[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern int ExtEscape(IntPtr hDC, int nEscape, int cbInput, IntPtr inData, int cbOutput, IntPtr outData);

Problem I have is that when used with the PASSTHROUGH flag, the IntPtr needs to point to a struct with the size and the data. I have defined the struct like this.

    public struct PasstroughData
    {
        public Int32 Size;
        public byte[] Data;
    }

So question is; how do I convert this to something I can use to call ExtEscape?

1 Answers

Try code like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.InteropServices;


namespace ConsoleApplication40
{
    class Program
    {

        static void Main(string[] args)
        {
            int size = 12;

            PasstroughData data = new PasstroughData();
            data.Size = 32;
            data.Data = new byte[size];

            int sz = Marshal.SizeOf(data);

            //add 4 bytes in the Size property which is an integer
            IntPtr BLOB = Marshal.AllocHGlobal(size + 4);
            Marshal.WriteInt32(BLOB, 0, data.Size);
            //copy to offset 4
            Marshal.Copy(data.Data, 0, BLOB + 4, size);

        }
        public struct PasstroughData
        {
            public Int32 Size;
            public byte[] Data;
        }
    }
}
Related