⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tvichw32.cpp

📁 《Delphi5企业级解决方案及应用剖析》参考程序 DELPHI 资料集
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* ========================================================================
   =================    TVicHW32  DLL interface        ====================
   ==========            Shareware Version 3.0                  ===========
   ==========     Copyright (c) 1998 Victor I.Ishikeev          ===========
   ========================================================================
   ==========         mail to "victor@ivi.ugatu.ac.ru"          ===========
   ======================================================================== */

//#define DEMOVERSION
// UNDEF THIS TO COMPILE REGISTERED VERSION OF THE DLL
#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


#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

#include "TVicHW32.h"

#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  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


typedef HANDLE (__stdcall* TOpenVxDHandle)(HANDLE); 

BOOL fWin95;

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


//-------------------------------------------------------------
long CtlCode(long Code) 
{
  if (fWin95)	return Code;
  else   		return (0x80000000L | ((0x0800 + Code) << 2));
}
//-------------------------------------------------------------
void
__stdcall IRQProcNT(PVicHW32Descriptor 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,
                    CtlCode(_DRV_SET_POST_EVENT),
                    &HW32->fLocEvent, sizeof(HW32->fLocEvent), 
					NULL, 0, &nByte, NULL);


    dwIRQ = HW32->fIRQNumber;
    DeviceIoControl(HW32->hDrv,
                    CtlCode(_DRV_SET_INT_VEC),
                    &dwIRQ, sizeof(dwIRQ),
					NULL, 0, &nByte, NULL);

    HW32->fMasked   = FALSE;
    DeviceIoControl(HW32->hDrv,
	     	        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);
			};
            if (HW32->OnHWInterrupt)
              HW32->OnHWInterrupt(pl->Drv_IRQ_Counter,
			                      (short)wrd>>8,
			                      (short)(wrd & 0x00ff),
			                      pl->ScanCode);
		}
	    else 
		   break;
	};
	DeviceIoControl(HW32->hDrv,
	            	CtlCode(_DRV_STOP_INT_VEC),
		            NULL, 0, NULL, 0,
		            &nByte, NULL);

    CurrentProcess = GetCurrentProcess();
    SetPriorityClass(CurrentProcess, NORMAL_PRIORITY_CLASS);
}

//-------------------------------------------------------------
void
__stdcall IRQProc95(PVicHW32Descriptor HW32)
{
	DWORD	nByte = 0, 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,
                    CtlCode(_DRV_SET_POST_EVENT),
                    &hDrvEvent, sizeof(hDrvEvent),
					NULL, 0, &nByte, NULL);

	dwIRQ = HW32->fIRQNumber;
    DeviceIoControl(HW32->hDrv,
                    CtlCode(_DRV_SET_INT_VEC),
                    &dwIRQ, sizeof(dwIRQ),
					NULL, 0, &nByte, NULL);
    
    HW32->fMasked   = FALSE;
    DeviceIoControl(HW32->hDrv,
	     	        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);
			  };
              if (HW32->OnHWInterrupt)			
                 HW32->OnHWInterrupt(pl->Drv_IRQ_Counter,
			                         (short)wrd>>8,
			                         (short)(wrd & 0x00ff),
			                         pl->ScanCode);
			  
			};
		} else
			break;
	};

	DeviceIoControl(HW32->hDrv,
	            	CtlCode(_DRV_STOP_INT_VEC),
		            NULL, 0, NULL, 0,
		            &nByte, NULL);

    CurrentProcess = GetCurrentProcess();
    SetPriorityClass(CurrentProcess, NORMAL_PRIORITY_CLASS);

}

BOOL InstallDriver(PVicHW32Descriptor HW32) 
{
    SC_HANDLE	schService;
	char	    ServiceExe[255];
	BOOL        Result;

	GetWindowsDirectory(ServiceExe, 128);
	strcat(ServiceExe, "\\SYSTEM32\\DRIVERS\\");
	strcat(ServiceExe, DriverName);
    strcat(ServiceExe, ".SYS");
	schService = CreateService( 
		                HW32->schSCManager,	    // SCManager database
					    DriverName,			// name of service
					    DriverName,			// name to display
					    SERVICE_ALL_ACCESS,		// desired access
					    SERVICE_KERNEL_DRIVER,	// service type
					    SERVICE_DEMAND_START,	// start type
					    SERVICE_ERROR_NORMAL,	// error control type
					    ServiceExe,				// service's binary
						NULL,                   // no load ordering group
						NULL,                   // no tag identifier
						NULL,                   // no dependencies
						NULL,                   // LocalSystem account
						NULL);                  // no password)

    Result = (schService != 0) || (GetLastError() == ERROR_SERVICE_EXISTS);

    HW32->fDebugCode |= Result?DEB_INST_OK:DEB_INST_ERR;


    CloseServiceHandle(schService);
	return Result;

}

BOOL StartDriver(PVicHW32Descriptor HW32) 
{
	
	SC_HANDLE	schService;
	BOOL		Result = FALSE;
    long        Res;
    
	schService = OpenService (HW32->schSCManager, DriverName, SERVICE_ALL_ACCESS);
	if (schService != 0) {
		
		Result = StartService(schService, 0, NULL);
		Res = GetLastError();

		Result |= (Res == ERROR_SERVICE_ALREADY_RUNNING);

		HW32->fDebugCode |= Result?DEB_START_OK:DEB_START_ERR;

		CloseServiceHandle (schService);

	}
	return Result;
}

BOOL StopDriver(PVicHW32Descriptor HW32) 
{
	
	SC_HANDLE schService;
	SERVICE_STATUS serviceStatus;
	BOOL Result = FALSE;

	schService = OpenService (HW32->schSCManager, DriverName, SERVICE_ALL_ACCESS);
	if (schService != NULL) {
		Result = ControlService (schService, SERVICE_CONTROL_STOP, &serviceStatus);
		CloseServiceHandle (schService);
	}
	return Result;
}

BOOL RemoveDriver(PVicHW32Descriptor HW32) 
{

	SC_HANDLE	schService;
	BOOL		Result = FALSE;

	schService = OpenService (HW32->schSCManager, DriverName, SERVICE_ALL_ACCESS);
	if (schService != 0) {
		Result = DeleteService (schService);
		CloseServiceHandle (schService);
	}
	return Result;
	
}

void CloseStopUnloadDriver(PVicHW32Descriptor HW32) 
{
	CloseHandle(HW32->hDrv); 
   if (fWin95)return;

#ifndef DEMOVERSION
   if (HW32->fStarted) return;
#endif

	HW32->schSCManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
	if  (HW32->schSCManager !=0) {
		StopDriver(HW32);
#ifndef DEMOVERSION
		if (!HW32->fRegistry)
#endif
	    RemoveDriver(HW32);
		CloseServiceHandle(HW32->schSCManager);
    };
}

void InstallStartLoadDriver(PVicHW32Descriptor HW32) 
{
  char buffer[255];


  CloseStopUnloadDriver(HW32);  // Close before start     


  if (fWin95) { // Windows 95/98
     strcpy(buffer,"\\\\.\\");
	 strcat(buffer,DriverName);
	 strcat(buffer,".VXD");
	 HW32->hDrv   = CreateFile(buffer,
						 0,
						 0,
						 NULL,
						 0,
						 FILE_FLAG_DELETE_ON_CLOSE,
						 0
						 );
	 return;
  }; 


  strcpy(buffer,"\\\\.\\");
  strcat(buffer,DriverName);
  
  HW32->fDebugCode = DEB_ENTRY;

#ifndef DEMOVERSION

  HW32->fRegistry= TRUE; // assume driver already installed to th registry
  HW32->fStarted = TRUE; // assume driver already started

  HW32->fDebugCode |= DEB_ENTRY_NOT_DEMO;

  HW32->hDrv     = CreateFile(
     	                buffer, 
	                  	GENERIC_READ | GENERIC_WRITE, 0, NULL,
                        OPEN_EXISTING, 
						FILE_ATTRIBUTE_NORMAL, 
						0
						);

  if (HW32->hDrv != INVALID_HANDLE_VALUE) return;

  HW32->fDebugCode |= DEB_NOT_STARTED;

  HW32->fStarted = FALSE;

#endif

  HW32->schSCManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);

  HW32->fDebugCode |= DEB_SC_NOT_OPEN;

  if (HW32->schSCManager !=0) {

	  HW32->fDebugCode |= DEB_SC_OPEN;

#ifndef DEMOVERSION

	  // Driver already installed but not started? Try to start...
	  if (StartDriver(HW32)) {

	     HW32->hDrv  = CreateFile(
			                buffer, 
		                  	GENERIC_READ | GENERIC_WRITE, 0, NULL,
                            OPEN_EXISTING, 
							FILE_ATTRIBUTE_NORMAL, 
							0
							);
			CloseServiceHandle (HW32->schSCManager);

			HW32->fDebugCode |= DEB_INSTALLED;

			return; // YES!

          }


		 HW32->fRegistry = FALSE; // not installed

		 HW32->fDebugCode |= DEB_NOT_INSTALLED;

#endif

		 
		 if (InstallDriver(HW32)) {

			 HW32->fDebugCode |= DEB_AFTER_INSTALL;

            if (StartDriver(HW32)) {

				HW32->fDebugCode |= DEB_AFTER_START;

	           HW32->hDrv   = CreateFile(
			                buffer, 
		                  	GENERIC_READ | GENERIC_WRITE, 0, NULL,
                            OPEN_EXISTING, 
							FILE_ATTRIBUTE_NORMAL, 
							0
							);
		
			};

		 };

	     CloseServiceHandle (HW32->schSCManager);

	};

	return;

}

void ClearFields(PVicHW32Descriptor HW32) 
{
  	HW32->hDrv                = INVALID_HANDLE_VALUE;
	HW32->fRegistry           = TRUE;
	HW32->fStarted            = TRUE;
    HW32->fTerminated         = FALSE;
    
	HW32->fInterface      = 0;
    HW32->fBus 			  = 0;
    HW32->fPhysLoPart     = 0;
    HW32->fPhysHiPart     = 0;
    HW32->fTypeMem        = 0; 
    HW32->fMemorySize     = 0;

	HW32->fDebugCode      = 0;
    HW32->OnHWInterrupt  = NULL;

    for (int i=0; i<MaxMappedAreas; i++) {
		HW32->fMappedPointers[i]  = NULL;
		HW32->fMappedSizes[i]  = 0;
	};
	HW32->fMappedAreas  = 0;
	
    HW32->fHardAccess     = TRUE;

    HW32->fIsIRQSet       = (HW32->fIRQNumber>0) && (HW32->fIRQNumber<16);
    HW32->fMasked         = TRUE;
    HW32->fOpenDrive      = FALSE;

}

BOOL VICFN GetHardAccess(PVicHW32Descriptor HW32) 
{
  	return HW32->fHardAccess;
}

void  VICFN SetHardAccess(PVicHW32Descriptor HW32, BOOL bNewValue) 
{
  DWORD nByte = 0;
  if (!HW32->fOpenDrive ) return;

  if (!fWin95) {
  
    if (bNewValue)

			DeviceIoControl(HW32->hDrv,
					CtlCode(_DRV_HARD_ACCESS),
					NULL, 0, NULL, 0,
					&nByte, NULL);
	else

        	DeviceIoControl(HW32->hDrv,
					CtlCode(_DRV_SOFT_ACCESS),
					NULL, 0, NULL, 0,
					&nByte, NULL);
  } 

  HW32->fHardAccess = bNewValue;



}

short  VICFN GetLPTNumber(PVicHW32Descriptor HW32) 
{
	return (HW32->fLPTNumber);
}

void  VICFN SetLPTNumber(PVicHW32Descriptor HW32,short nNewValue) 
{
	
	if (HW32->fOpenDrive && (nNewValue<=HW32->fLPTs)) {
		HW32->fLPTBasePort = (short)*(HW32->fLPTAddresses+nNewValue-1);
        ((PLockedBuffer)HW32->fpLockedMemory)->LPT_BASE_PORT = HW32->fLPTBasePort;
		HW32->fDataPorts[0] = HW32->fDataPorts[1] = HW32->fDataPorts[2] =0;
		HW32->fLPTNumber = (BYTE)nNewValue;
	};

	
}

BOOL  VICFN IsIRQMasked(PVicHW32Descriptor HW32) 
{
	return HW32->fMasked;

}

void  VICFN UnmaskIRQ(PVicHW32Descriptor HW32) 
{

	DWORD nByte = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -