C # - Ability to use IOCTL
I am trying to code a Point Of Sale system that allows you to bind the "Cash Drawer" cache. The code is provided in the tutorial to open the cash drawer (in C ++ using IOCTL). Since I am coding in C # .NET, is it possible to do something like this from C # or would I have to write some kind of unmanaged code?
Can I get the handle to "\\. \ ADVANSYS" from C #? Do I need to use DLLImport?
Would be grateful if someone could point me in the right direction.
// IOCTL Codes
#define GPD_TYPE 56053
#define ADV_OPEN_CTL_CODE CTL_CODE(GPD_TYPE, 0x920, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define ADV_STATUS_CTL_CODE CTL_CODE(GPD_TYPE, 0x900, METHOD_BUFFERED, FILE_ANY_ACCESS)
void OpenDrawer(UCHAR uWhichDrawer) // uWhichDrawer = 1 => CD#1, uWhichDrawer = 2 => CD#2
{
HANDLE hFile;
BOOL bRet
UCHAR uDrawer = uWhichDrawer;
// Open the driver
hFile = CreateFile(TEXT("\\\\.\\ADVSYS"),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (m_hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox("Unable to open Cash Drawer Device Driver!");
return;
}
// Turn on the Cash Drawer Output (Fire the required solenoid)
bRet = DeviceIoControl(hFile, ADV_CD_OPEN_CTL_CODE,
&uDrawer, sizeof(uDrawer),
NULL, 0,
&ulBytesReturned, NULL);
if (bRet == FALSE || ulBytesReturned != 1)
{
AfxMessageBox("Failed to write to cash drawer driver");
CloseHandle(hFile);
return;
}
CloseHandle(hFile);
}
a source to share
C ++ is riddled with bugs, not sure if I got it right. Your best bet is to declare DeviceIoControl () with modified argument types so that they can be called easily. You also have to P / Invoke CreateFile because FileStream cannot open devices. It should look like this:
using System;
using System.IO;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program {
static void Main(string[] args) {
IntPtr hdl = CreateFile("\\\\.\\ADVSYS", FileAccess.ReadWrite,
FileShare.None, IntPtr.Zero, FileMode.Open,
FileOptions.None, IntPtr.Zero);
if (hdl == (IntPtr)(-1)) throw new Win32Exception();
try {
byte drawer = 1;
bool ok = DeviceIoControl(hdl, CTLCODE, ref drawer, 1, IntPtr.Zero,
0, IntPtr.Zero, IntPtr.Zero);
if (!ok) throw new Win32Exception();
}
finally {
CloseHandle(hdl);
}
}
// P/Invoke:
private const uint CTLCODE = 0xdaf52480;
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CreateFile(string filename, FileAccess access,
FileShare sharing, IntPtr SecurityAttributes, FileMode mode,
FileOptions options, IntPtr template
);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool DeviceIoControl(IntPtr device, uint ctlcode,
ref byte inbuffer, int inbuffersize,
IntPtr outbuffer, int outbufferSize,
IntPtr bytesreturned, IntPtr overlapped
);
[DllImport("kernel32.dll")]
private static extern void CloseHandle(IntPtr hdl);
}
a source to share
You can use Pinvoke ;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
internal static extern bool DeviceIoControl([In] SafeFileHandle hDevice, [In] int dwIoControlCode, [In] IntPtr lpInBuffer, [In] int nInBufferSize, [Out] IntPtr lpOutBuffer, [In] int nOutBufferSize, out int lpBytesReturned, [In] IntPtr lpOverlapped);
This example might help as well.
a source to share
It has a lot of ready-made codes on pinvoke.net with lots of examples.
http://www.pinvoke.net/default.aspx/kernel32.DeviceIoControl
a source to share