C# - 可以使用 IOCTL
我正在尝试为销售点系统编写代码,该系统允许“现金抽屉”附件。手册中提供了用于打开现金抽屉的代码(使用 IOCTL 在 C++ 中)。由于我使用 C# .NET 进行编码,是否可以在 C# 中执行类似的操作,或者我必须编写一些非托管代码?
我可以从 C# 中获取“\\.\ADVANSYS”的句柄吗?我需要使用 DLLImport 吗?
如果有人能指出我正确的方向,我将不胜感激。
// 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);
}
I'm trying to code for a Point Of Sale system which allows for a "Cash Drawer" attachment. Code is provided in the manual for opening the cash drawer (in C++ using IOCTL). Since I am coding in C# .NET, is it possible to perform something similar from within C# or will I have to write some unmanaged code?
Am I able to get a handle to "\\.\ADVANSYS" from within C#? Do I need to use DLLImport?
Would appreciate it 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 充满了错误,不确定我是否正确。最好的办法是使用更改的参数类型来声明 DeviceIoControl(),以便于调用。您还必须 P/Invoke CreateFile,因为 FileStream 无法打开设备。它应该看起来与此类似:
The C++ is riddled with mistakes, not sure if I got it right. The best thing to do is to declare DeviceIoControl() with altered argument types so that it is easy to call. You also have to P/Invoke CreateFile because FileStream cannot open devices. It ought to look similar to this:
您可以使用 Pinvoke;
此示例可能也会有所帮助。
You can use Pinvoke;
This example might also help.
pinvoke.net 上有大量为此准备的代码,还有大量示例。
http://www.pinvoke.net/default.aspx/kernel32.DeviceIoControl
There's loads of code ready for this on pinvoke.net with plenty of examples too.
http://www.pinvoke.net/default.aspx/kernel32.DeviceIoControl
你写道:
您可以发布 GPD_TYPE 的正确值吗?
最好的问候!
you wrote:
Can you post proper value of GPD_TYPE ?
Best regadrs!