📄 tvichw32ctl.cpp
字号:
// TVicHW32Ctl.cpp : Implementation of the CTVicHW32Ctrl ActiveX Control class.
#include "stdafx.h"
#include "TVicHW32.h"
#include "TVicHW32Ctl.h"
#include "TVicHW32Ppg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define _DRV_MAP_MEMORY 2
#define _DRV_UNMAP_MEMORY 3
#define _DRV_SET_POST_EVENT 5
#define _DRV_SET_INT_VEC 6
#define _DRV_MASK_INT_VEC 7
#define _DRV_UNMASK_INT_VEC 8
#define _DRV_STOP_INT_VEC 9
#define _DRV_PORT_CONTROL 10
#define _DRV_HARD_READ_PORT 11
#define _DRV_HARD_WRITE_PORT 12
#define _DRV_HARD_READ_PORTW 13
#define _DRV_HARD_WRITE_PORTW 14
#define _DRV_HARD_READ_PORTL 15
#define _DRV_HARD_WRITE_PORTL 16
#define _DRV_READ_PORT_FIFO 18
#define _DRV_WRITE_PORT_FIFO 19
#define _DRV_READ_PORT_FIFO_WORD 20
#define _DRV_WRITE_PORT_FIFO_WORD 21
#define _DRV_READ_PORT_FIFO_LONG 22
#define _DRV_WRITE_PORT_FIFO_LONG 23
#define _DRV_LOCK_MEMORY 24
#define _DRV_UNLOCK_MEMORY 25
#define _DRV_MAP_LPT_AREA 26
#define _DRV_UNMAP_LPT_AREA 27
#define _DRV_SOFT_ACCESS 31
#define _DRV_HARD_ACCESS 32
#define MaxPins 17
#define MAX_RING_BUFFER 511
#define DEB_INST_OK 0x0001
#define DEB_INST_ERR 0x0002
#define DEB_START_OK 0x0004
#define DEB_START_ERR 0x0008
#define DEB_ENTRY 0x0010
#define DEB_ENTRY_NOT_DEMO 0x0020
#define DEB_NOT_STARTED 0x0040
#define DEB_RESERVED 0x0080
#define DEB_SC_OPEN 0x0100
#define DEB_SC_NOT_OPEN 0x0200
#define DEB_INSTALLED 0x0400
#define DEB_NOT_INSTALLED 0x0800
#define DEB_AFTER_INSTALL 0x1000
#define DEB_AFTER_START 0x2000
//#define DEMOVERSION
#ifdef DEMOVERSION
#ifdef PORT_ONLY
const char DriverName[] = "VICPRT00";
#else
const char DriverName[] = "VICHW00";
#endif
#else
#ifdef PORT_ONLY
const char DriverName[] = "VICPRT11";
#else
const char DriverName[] = "VICHW11";
#endif
#endif
const BOOL PinsPort[] = {2,0,0,0,0,0,0,0,0,1,1,1,1,2,1,2,2};
const BOOL Negative[] =
{TRUE, // -STROBE, pin 1, Base+2,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, FALSE,// DATA, pins 2..9, Base+0
FALSE, // ACKWL, pin 10, Base+1
TRUE, // BUSY, pin 11, Base+1
FALSE, // PE, pin 12, Base+1
FALSE, // SLCT, pin 13, Base+1
TRUE, // -AUTOFD, pin 14, Base+2
FALSE, // -ERROR, pin 15, Base+1
FALSE, // INIT, pin 16, Base+2
TRUE}; // -SLCTIN pin 17, Base+2
const BYTE MaskPins[] =
{0x01, // -STROBE, pin 1, Base+2,
0x01, // DATA0, pin 2, Base+0
0x02, // DATA1, pin 3, Base+0
0x04, // DATA2, pin 4, Base+0
0x08, // DATA3, pin 5, Base+0
0x10, // DATA4, pin 6, Base+0
0x20, // DATA5, pin 7, Base+0
0x40, // DATA6, pin 8, Base+0
0x80, // DATA7, pin 9, Base+0
0x40, // -ACKWL, pin 10, Base+1
0x80, // BUSY, pin 11, Base+1
0x20, // PE, pin 12, Base+1
0x10, // SLCT, pin 13, Base+1
0x02, // -AUTOFD, pin 14, Base+2
0x08, // -ERROR, pin 15, Base+1
0x04, // INIT, pin 16, Base+2
0x08}; // -SLCTIN, pin 17, Base+2
IMPLEMENT_DYNCREATE(CTVicHW32Ctrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CTVicHW32Ctrl, COleControl)
//{{AFX_MSG_MAP(CTVicHW32Ctrl)
// NOTE - ClassWizard will add and remove message map entries
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit)
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CTVicHW32Ctrl, COleControl)
//{{AFX_DISPATCH_MAP(CTVicHW32Ctrl)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "HardAccess", GetHardAccess, SetHardAccess, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTNumber", GetLPTNumber, SetLPTNumber, VT_I2)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "IRQMasked", GetIRQMasked, SetIRQMasked, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "IRQNumber", GetIRQNumber, SetIRQNumber, VT_I2)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTNumPorts", GetLPTNumPorts, SetNotSupported, VT_I2)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTBasePort", GetLPTBasePort, SetNotSupported, VT_I2)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTAckwl", GetLPTAckwl, SetNotSupported, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTBusy", GetLPTBusy, SetNotSupported, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTPaperEnd", GetLPTPaperEnd, SetNotSupported, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTSlct", GetLPTSlct, SetNotSupported, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "LPTError", GetLPTError, SetNotSupported, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "ActiveHW", GetActiveHW, SetNotSupported, VT_BOOL)
DISP_PROPERTY_EX(CTVicHW32Ctrl, "MappedAreas", GetMappedAreas, SetNotSupported, VT_I2)
DISP_FUNCTION(CTVicHW32Ctrl, "CloseDriver", CloseDriver, VT_EMPTY, VTS_NONE)
DISP_FUNCTION(CTVicHW32Ctrl, "OpenDriver", OpenDriver, VT_BOOL, VTS_NONE)
DISP_FUNCTION(CTVicHW32Ctrl, "MapPhysToLinear", MapPhysToLinear, VT_I4, VTS_I4 VTS_I4)
DISP_FUNCTION(CTVicHW32Ctrl, "UnmapMemory", UnmapMemory, VT_EMPTY, VTS_I4 VTS_I4)
DISP_FUNCTION(CTVicHW32Ctrl, "LPTStrobe", LPTStrobe, VT_EMPTY, VTS_NONE)
DISP_FUNCTION(CTVicHW32Ctrl, "LPTAutofd", LPTAutofd, VT_EMPTY, VTS_BOOL)
DISP_FUNCTION(CTVicHW32Ctrl, "LPTInit", LPTInit, VT_EMPTY, VTS_NONE)
DISP_FUNCTION(CTVicHW32Ctrl, "LPTSlctIn", LPTSlctIn, VT_EMPTY, VTS_NONE)
DISP_FUNCTION(CTVicHW32Ctrl, "LPTPrintChar", LPTPrintChar, VT_BOOL, VTS_I2)
DISP_FUNCTION(CTVicHW32Ctrl, "DebugCode", DebugCode, VT_I4, VTS_NONE)
DISP_PROPERTY_PARAM(CTVicHW32Ctrl, "Port", GetPort, SetPort, VT_I2, VTS_I2)
DISP_PROPERTY_PARAM(CTVicHW32Ctrl, "PortW", GetPortW, SetPortW, VT_I2, VTS_I2)
DISP_PROPERTY_PARAM(CTVicHW32Ctrl, "PortL", GetPortL, SetPortL, VT_I4, VTS_I2)
DISP_PROPERTY_PARAM(CTVicHW32Ctrl, "Pin", GetPin, SetPin, VT_BOOL, VTS_I2)
DISP_PROPERTY_PARAM(CTVicHW32Ctrl, "Mem", GetMem, SetMem, VT_I2, VTS_I4 VTS_I4)
DISP_PROPERTY_PARAM(CTVicHW32Ctrl, "MemW", GetMemW, SetMemW, VT_I2, VTS_I4 VTS_I4)
DISP_PROPERTY_PARAM(CTVicHW32Ctrl, "MemL", GetMemL, SetMemL, VT_I4, VTS_I4 VTS_I4)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CTVicHW32Ctrl, COleControl)
//{{AFX_EVENT_MAP(CTVicHW32Ctrl)
EVENT_CUSTOM("OnHwInterrupt", FireOnHwInterrupt, VTS_I4 VTS_I2 VTS_I2 VTS_I2)
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CTVicHW32Ctrl, 1)
PROPPAGEID(CTVicHW32PropPage::guid)
END_PROPPAGEIDS(CTVicHW32Ctrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CTVicHW32Ctrl, "TVICHW32.TVicHW32Ctrl.1",
0x647ef20d, 0x1fd2, 0x11d2, 0xb0, 0x35, 0, 0xa0, 0xc9, 0x18, 0x4f, 0xf9)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CTVicHW32Ctrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DTVicHW32 =
{ 0xeac1564, 0x1feb, 0x11d2, { 0xb0, 0x35, 0, 0xa0, 0xc9, 0x18, 0x4f, 0xf9 } };
const IID BASED_CODE IID_DTVicHW32Events =
{ 0xeac1565, 0x1feb, 0x11d2, { 0xb0, 0x35, 0, 0xa0, 0xc9, 0x18, 0x4f, 0xf9 } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwTVicHW32OleMisc =
OLEMISC_INVISIBLEATRUNTIME |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CTVicHW32Ctrl, IDS_TVICHW32, _dwTVicHW32OleMisc)
/////////////////////////////////////////////////////////////////////////////
// CTVicHW32Ctrl::CTVicHW32CtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CTVicHW32Ctrl
BOOL CTVicHW32Ctrl::CTVicHW32CtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: Verify that your control follows apartment-model threading rules.
// Refer to MFC TechNote 64 for more information.
// If your control does not conform to the apartment-model rules, then
// you must modify the code below, changing the 6th parameter from
// afxRegInsertable | afxRegApartmentThreading to afxRegInsertable.
if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_TVICHW32,
IDB_TVICHW32,
afxRegInsertable | afxRegApartmentThreading,
_dwTVicHW32OleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
//-------------------------------------------------------------
void
__stdcall IRQProcNT(CTVicHW32Ctrl& HW32)
{
DWORD nByte = 0;
DWORD dwIRQ;
HANDLE CurrentProcess;
PLockedBuffer pl;
WORD wrd,sel;
pl = (PLockedBuffer)(HW32.fpLockedMemory);
wrd = 0;
CurrentProcess = GetCurrentProcess();
SetPriorityClass(CurrentProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
HW32.fLocEvent = CreateSemaphore(NULL, 0, 1000, NULL);
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_SET_POST_EVENT),
&HW32.fLocEvent, sizeof(HW32.fLocEvent),
NULL, 0, &nByte, NULL);
dwIRQ = HW32.fIRQNumber;
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_SET_INT_VEC),
&dwIRQ, sizeof(dwIRQ),
NULL, 0, &nByte, NULL);
HW32.fMasked = FALSE;
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_UNMASK_INT_VEC),
NULL, 0, NULL, 0,
&nByte, NULL);
while (TRUE) {
WaitForSingleObject(HW32.fLocEvent, INFINITE);
if (!HW32.fTerminated) {
if ((HW32.fIRQNumber == 7) && (pl->L_BUF_LPT>0)) {
sel = pl->N_SEL_LPT;
wrd = pl->BUF_LPT[sel];
++sel;
sel = sel & (pl->MAX_BUF_LPT);
pl->N_SEL_LPT = sel;
--(pl->L_BUF_LPT);
};
HW32.FireOnHwInterrupt(pl->Drv_IRQ_Counter,
(short)wrd>>8,
(short)(wrd & 0x00ff),
pl->ScanCode);
}
else
break;
};
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_STOP_INT_VEC),
NULL, 0, NULL, 0,
&nByte, NULL);
CurrentProcess = GetCurrentProcess();
SetPriorityClass(CurrentProcess, NORMAL_PRIORITY_CLASS);
}
typedef HANDLE (__stdcall* TOpenVxDHandle)(HANDLE);
//-------------------------------------------------------------
void
__stdcall IRQProc95(CTVicHW32Ctrl& HW32)
{
DWORD nByte = 0;
DWORD dwIRQ;
DWORD fHandled;
HANDLE CurrentProcess;
DWORD Count;
HANDLE hDrvEvent;
TOpenVxDHandle OpenVxDHandle;
PLockedBuffer pl;
WORD wrd, sel;
pl = (PLockedBuffer)(HW32.fpLockedMemory);
wrd = 0;
fHandled = 0;
CurrentProcess = GetCurrentProcess();
SetPriorityClass(CurrentProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
HW32.fLocEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ResetEvent(HW32.fLocEvent);
HW32.fMasked = TRUE;
HW32.hDll = GetModuleHandle("kernel32");
OpenVxDHandle = (TOpenVxDHandle)GetProcAddress(HW32.hDll, "OpenVxDHandle");
hDrvEvent = OpenVxDHandle(HW32.fLocEvent);
CloseHandle(HW32.hDll);
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_SET_POST_EVENT),
&hDrvEvent, sizeof(hDrvEvent),
NULL, 0, &nByte, NULL);
dwIRQ = HW32.fIRQNumber;
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_SET_INT_VEC),
&dwIRQ, sizeof(dwIRQ),
NULL, 0, &nByte, NULL);
HW32.fMasked = FALSE;
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_UNMASK_INT_VEC),
NULL, 0, NULL, 0,
&nByte, NULL);
while (TRUE) {
WaitForSingleObject(HW32.fLocEvent, INFINITE);
ResetEvent(HW32.fLocEvent);
if (!HW32.fTerminated) {
Count = pl->Drv_IRQ_Counter;
while (fHandled < Count) {
fHandled++;
if ((HW32.fIRQNumber == 7) && (pl->L_BUF_LPT>0)) {
sel = pl->N_SEL_LPT;
wrd = pl->BUF_LPT[sel];
++sel;
sel = sel & (pl->MAX_BUF_LPT);
pl->N_SEL_LPT = sel;
--(pl->L_BUF_LPT);
};
HW32.FireOnHwInterrupt(pl->Drv_IRQ_Counter,
(short)wrd>>8,
(short)(wrd & 0x00ff),
pl->ScanCode);
};
} else
break;
};
DeviceIoControl(HW32.hDrv,
HW32.CtlCode(_DRV_STOP_INT_VEC),
NULL, 0, NULL, 0,
&nByte, NULL);
CurrentProcess = GetCurrentProcess();
SetPriorityClass(CurrentProcess, NORMAL_PRIORITY_CLASS);
}
/////////////////////////////////////////////////////////////////////////////
// CTVicHW32Ctrl::CTVicHW32Ctrl - Constructor
CTVicHW32Ctrl::CTVicHW32Ctrl()
{
InitializeIIDs(&IID_DTVicHW32, &IID_DTVicHW32Events);
COleControl::SetInitialSize(28,28);
fWin95 = ((GetVersion() & 0x80000000) != 0);
ClearFields();
}
/////////////////////////////////////////////////////////////////////////////
// CTVicHW32Ctrl::~CTVicHW32Ctrl - Destructor
CTVicHW32Ctrl::~CTVicHW32Ctrl()
{
if (fOpenDrive) CloseDriver();
ClearFields();
}
/////////////////////////////////////////////////////////////////////////////
// CTVicHW32Ctrl::OnDraw - Drawing function
void CTVicHW32Ctrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
#define ssize 28
BITMAP bm;
POINT ptSize,ptOrg;
/*
if (
(rcBounds.right - rcBounds.left)!= ssize ||
(rcBounds.bottom - rcBounds.top) != ssize )
*/
{
// COleControl::SetControlSize(ssize,ssize);
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
// pdc->Ellipse(rcBounds);
HBITMAP hbitmap= ::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1));
HDC hMemDC = ::CreateCompatibleDC(NULL);
SelectObject(hMemDC,hbitmap);
SetMapMode(hMemDC,GetMapMode(pdc->m_hDC));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -