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

📄 tdlportio.cpp

📁 这里介绍的一款多功能编程器
💻 CPP
📖 第 1 页 / 共 4 页
字号:
      // Stop the driver, then close the service
      SERVICE_STATUS sStatus;

      if (!ControlService(hService, SERVICE_CONTROL_STOP, &sStatus))
         dwStatus = GetLastError();

      // Close the service
      CloseServiceHandle(hService);
   }
   else
      dwStatus = GetLastError();

   if (dwStatus!=0)
      sprintf(FLastError, "DriverStop: Error #%d", dwStatus);

   return dwStatus==0; // Success == 0
}


//---------------------------------------------------------------------------
// DriverRemove()
//    Removes a driver from the Windows NT system (service manager)
//---------------------------------------------------------------------------
bool __fastcall DriverRemove()
{
   SC_HANDLE hService; // Handle to the service to start
   DWORD dwStatus = 0; // Assume success, until we prove otherwise

   // If we didn't install the driver, then don't remove it.
   // Pretend we removed it, by indicating success.
   if (FDrvPrevInst) return true;

   // Get a handle to the service to remove
   hService = OpenService(
                 hSCMan,
                 DRIVER_NAME,
                 DELETE);

   if (hService!=NULL)
   {
      // Remove the driver then close the service again
      if (!DeleteService(hService))
         dwStatus = GetLastError();

      // Close the service
      CloseServiceHandle(hService);
   }
   else
      dwStatus = GetLastError();

   if (dwStatus!=0)
      sprintf(FLastError, "DriverRemove: Error #%d", dwStatus);

   return dwStatus==0; // Success == 0
}


//---------------------------------------------------------------------------
// OpenDriver()
//    Opens the DLL dynamically
//---------------------------------------------------------------------------
void _export __stdcall OpenDriver(void)
{
   // If the DLL/driver is already open, then forget it!
   if (FActiveHW) return;

   // If we're running Windows NT, install the driver then start it
   if (FRunningWinNT)
   {
      // Connect to the Service Control Manager
      if (!ConnectSCM()) return;

      // Install the driver
      if (!DriverInstall())
      {
         // Driver install failed, so disconnect from the SCM
         DisconnectSCM();
         return;
      }

      // Start the driver
      if (!DriverStart())
      {
         // Driver start failed, so remove it then disconnect from SCM
         DriverRemove();
         DisconnectSCM();
         return;
      }
   }

   // Load DLL library
   char LibraryFileName[MAX_PATH];
   strcpy(LibraryFileName, LIBRARY_FILENAME);

   if (strlen(FDLLPath)>0)
   {
      strcpy(LibraryFileName, FDLLPath);
      strcpy(LibraryFileName, "\\");
      strcpy(LibraryFileName, LIBRARY_FILENAME);
   }

   FDLLInst=LoadLibrary(LibraryFileName);
   if (FDLLInst!=NULL)
   {
      DlReadByte=(TDlPortReadPortUchar)
                  GetProcAddress(FDLLInst,"DlPortReadPortUchar");
      DlReadWord=(TDlPortReadPortUshort)
                  GetProcAddress(FDLLInst,"DlPortReadPortUshort");
      DlReadDWord=(TDlPortReadPortUlong)
                   GetProcAddress(FDLLInst,"DlPortReadPortUlong");

      DlWriteByte=(TDlPortWritePortUchar)
                   GetProcAddress(FDLLInst,"DlPortWritePortUchar");
      DlWriteWord=(TDlPortWritePortUshort)
                   GetProcAddress(FDLLInst,"DlPortWritePortUshort");
      DlWriteDWord=(TDlPortWritePortUlong)
                    GetProcAddress(FDLLInst,"DlPortWritePortUlong");

      DlReadBufferByte=(TDlPortReadPortBufferUchar)
                        GetProcAddress(FDLLInst,"DlPortReadPortBufferUchar");
      DlReadBufferWord=(TDlPortReadPortBufferUshort)
                        GetProcAddress(FDLLInst,"DlPortReadPortBufferUshort");
      DlReadBufferDWord=(TDlPortReadPortBufferUlong)
                         GetProcAddress(FDLLInst,"DlPortReadPortBufferUlong");

      DlWriteBufferByte=(TDlPortWritePortBufferUchar)
                         GetProcAddress(FDLLInst,"DlPortWritePortBufferUchar");
      DlWriteBufferWord=(TDlPortWritePortBufferUshort)
                         GetProcAddress(FDLLInst,"DlPortWritePortBufferUshort");
      DlWriteBufferDWord=(TDlPortWritePortBufferUlong)
                          GetProcAddress(FDLLInst,"DlPortWritePortBufferUlong");

      // Make sure all our functions are there
      if (DlReadByte!=NULL && DlReadWord!=NULL && DlReadDWord!=NULL &&
          DlWriteByte!=NULL && DlWriteWord!=NULL && DlWriteDWord!=NULL &&
          DlReadBufferByte!=NULL && DlReadBufferWord!=NULL &&
          DlReadBufferDWord!=NULL && DlWriteBufferByte!=NULL &&
          DlWriteBufferWord!=NULL && DlWriteBufferDWord!=NULL)
         FActiveHW=true; // Success
   }

   // Did we fail?
   if (!FActiveHW)
   {
      // If we're running Windows NT, stop the driver then remove it
      // Forget about any return (error) values we might get...
      if (FRunningWinNT)
      {
         DriverStop();
         DriverRemove();
         DisconnectSCM();
      }

      // Free the library
      if (FDLLInst!=NULL)
      {
         FreeLibrary(FDLLInst);
         FDLLInst=NULL;
      }
   }
}


//---------------------------------------------------------------------------
// CloseDriver()
//    Closes the dynamically opened DLL
//---------------------------------------------------------------------------
void _export __stdcall CloseDriver(void)
{
   // Don't close anything if it wasn't opened previously
   if (!FActiveHW) return;

   // If we're running Windows NT, stop the driver then remove it
   if (FRunningWinNT)
   {
      if (!DriverStop()) return;
      if (!DriverRemove()) return;
      DisconnectSCM();
   }

   // Free the library
   if (FreeLibrary(FDLLInst)==0) return;
   FDLLInst=NULL;

   FActiveHW=false; // Success
}


//---------------------------------------------------------------------------
// PortControl()
//    Reads/writes a TVicPort/TVicHW32 compatible port record array
//---------------------------------------------------------------------------
void _export __stdcall PortControl(TPortRec *Ports, WORD NumPorts)
{
#ifndef FAST
   if (!FActiveHW) return;
#endif

   for (int Index=0; Index<NumPorts; Index++)
      if (Ports[Index].fWrite)
         DlWriteByte(Ports[Index].PortAddr, Ports[Index].PortData);
      else
         Ports[Index].PortData=DlReadByte(Ports[Index].PortAddr);
}


//---------------------------------------------------------------------------
// PortCommand()
//    Reads/writes a port command array
//---------------------------------------------------------------------------
void _export __stdcall PortCommand(TPortCommand *Ports, WORD NumPorts)
{
#ifndef FAST
   if (!FActiveHW) return;
#endif

   for (int Index=0; Index<NumPorts; Index++)
      switch (Ports[Index].PortMode)
      {
         case tmReadByte:
            Ports[Index].PortData.Byte=DlReadByte(Ports[Index].PortAddr);
            break;

         case tmReadWord:
            Ports[Index].PortData.Word=DlReadWord(Ports[Index].PortAddr);
            break;

         case tmReadDWord:
            Ports[Index].PortData.DWord=DlReadDWord(Ports[Index].PortAddr);
            break;

         case tmWriteByte:
            DlWriteByte(Ports[Index].PortAddr, Ports[Index].PortData.Byte);
            break;

         case tmWriteWord:
            DlWriteWord(Ports[Index].PortAddr, Ports[Index].PortData.Word);
            break;

         case tmWriteDWord:
            DlWriteDWord(Ports[Index].PortAddr, Ports[Index].PortData.DWord);
            break;

         default:
            break; // Ignore it
      }
}


//---------------------------------------------------------------------------
// ReadPortFIFO()
//    Burt read of a single port
//---------------------------------------------------------------------------
void _export __stdcall ReadPortFIFO(WORD PortAddr,
                                    WORD NumPorts,
                                    BYTE *Buffer)
{
#ifndef FAST
   if (FActiveHW)
#endif
      DlReadBufferByte(PortAddr, Buffer, NumPorts);
}


//---------------------------------------------------------------------------
// WritePortFIFO()
//    Burt write of a single port
//---------------------------------------------------------------------------
void _export __stdcall WritePortFIFO(WORD PortAddr,
                                     WORD NumPorts,
                                     BYTE *Buffer)
{
#ifndef FAST
   if (FActiveHW)
#endif
      DlWriteBufferByte(PortAddr, Buffer, NumPorts);
}


//---------------------------------------------------------------------------
// ReadWPortFIFO
//    Extended block word read
//---------------------------------------------------------------------------
void _export __stdcall ReadWPortFIFO(WORD PortAddr,
                                     WORD NumPorts,
                                     WORD *Buffer)
{
#ifndef FAST
   if (FActiveHW)
#endif
      DlReadBufferWord(PortAddr, Buffer, NumPorts);
}


//---------------------------------------------------------------------------
// WriteWPortFIFO
//    Extended block word write
//---------------------------------------------------------------------------
void _export __stdcall WriteWPortFIFO(WORD PortAddr,
                                      WORD NumPorts,
                                      WORD *Buffer)
{
#ifndef FAST
   if (FActiveHW)
#endif
      DlWriteBufferWord(PortAddr, Buffer, NumPorts);
}


//---------------------------------------------------------------------------
// ReadLPortFIFO
//    Extended block double word read
//---------------------------------------------------------------------------
void _export __stdcall ReadLPortFIFO(WORD PortAddr,
                                     WORD NumPorts,
                                     DWORD *Buffer)
{
#ifndef FAST
   if (FActiveHW)
#endif
      DlReadBufferDWord(PortAddr, Buffer, NumPorts);
}


//---------------------------------------------------------------------------
// WriteLPortFIFO
//    Extended block double word write
//---------------------------------------------------------------------------
void _export __stdcall WriteLPortFIFO(WORD PortAddr,
                                      WORD NumPorts,
                                      DWORD *Buffer)
{
#ifndef FAST
   if (FActiveHW)
#endif
      DlWriteBufferDWord(PortAddr, Buffer, NumPorts);
}


//---------------------------------------------------------------------------
// GetDriverPath()
//---------------------------------------------------------------------------
char* _export __stdcall GetDriverPath(void)
{
   return FDriverPath;
}


//---------------------------------------------------------------------------
// SetDriverPath()
//---------------------------------------------------------------------------
void _export __stdcall SetDriverPath(char *Path)
{
   strcpy(FDriverPath, Path);
}


//---------------------------------------------------------------------------
// GetDLLPath()
//---------------------------------------------------------------------------
char* _export __stdcall GetDLLPath(void)
{
   return FDLLPath;
}


//---------------------------------------------------------------------------
// SetDLLPath()
//---------------------------------------------------------------------------
void _export __stdcall SetDLLPath(char *Path)
{
   strcpy(FDLLPath, Path);
}


//---------------------------------------------------------------------------
// ActiveHW()
//---------------------------------------------------------------------------
bool _export __stdcall ActiveHW(void)

⌨️ 快捷键说明

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