34
Appendix A. Sample C++ Cash Drawer Code for Windows
NOTE:
Requires installation of System Driver. Refer to the System Driver Installation
section for instructions.
1.
Open Cash Drawer
// IOCTL Codes
#define GPD_TYPE 56053
#define ADV_OPEN_CTL_CODE CTL_CODE(GPD_TYPE, 0x900, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define ADV_STATUS_CTL_CODE CTL_CODE(GPD_TYPE, 0x901, 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("\\\\.\\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);
}
2.
Get Cash Drawer Status
void GetDrawerState()
{
HANDLE hFile;
BOOL bRet;
// 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;
}
// Read the CD status
bRet = DeviceIoControl(hFile, ADV_CD_STATUS_CTL_CODE,
NULL, 0