📄 sacwd32.c
字号:
/* (CCW)
SACWD32.c
Windows 95, NT, 32s Access System (06/97 Release V4.3)
//---------------------------------------------------------------------------
// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Dallas Semiconductor
// shall not be used except as stated in the Dallas Semiconductor
// Branding Policy.
//---------------------------------------------------------------------------
12/10/96 Added a new CheckOverdrive Function to test overdrive state
12/13/96 Fixed Sleep problem. Replaced Sleep() with FastSleep()
6/09/97 Fixed grounded printer problem by putting EC on port before dropping 14
6/09/97 Wait for semaphore before opening handle to driver. Driver is now exclusive access
Modifications to standard SACWD32 denoted with (OWPD)
*/
#include <dos.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <windows.h>
#include <windef.h>
#include <winbase.h>
#include <winnt.h>
#define W32SUT_32
#include "w32sut.h"
#include "utdll.h"
#include "sacwd32.h"
#define DO_RESET 0
#define DO_BIT 1
#define DO_BYTE 2
#define TOGGLE_OVERDRIVE 3
#define TOGGLE_PASSTHRU 4
#define CHECK_BRICK 5
#define SET_PORT 6
#define SAUTH95_SEM "SAuth95AccessSem"
#define SA16COMPAT_SEM "SAuth95Win16BitCompatSem"
#define SAUTH95_PASSTHRU_SEM "SAuth95PassThruSem"
#define SEM_TIMEOUT 10000 //milliseconds
#define PASSTRHU_TIMEOUT 10 //milliseconds
#define ROMSEARCH 0xF0
//nt variables
HANDLE DOWHandle; // Handle to file representing our driver
HANDLE sHandle = NULL; // Handle to our semaphore
DWORD NumTrans;
UCHAR gpn = 1;
UCHAR Passthru = TRUE;
UCHAR TimeOut;
UCHAR mch[256]; // (OWPD) was 3, enlarged for block function
BOOL WinNT = FALSE;
BOOL Win95 = FALSE;
BOOL Win32s = FALSE;
BOOL loaded = FALSE;
uchar ToggleOverdrive(void); // (OWPD) made not static
static uchar CheckDriver(void);
uchar CheckOverdrive(void); // (OWPD) mad not static
static void TogglePassthru(void);
static uchar EnterPassthru (void);
static uchar ExitPassthru (void);
static uchar CheckBusy (void);
static uchar DOWBit (uchar);
static uchar DOWByte (uchar);
uchar DOWReset (void); // (OWPD) made not static
static uchar RomSearch (sa_struct *gb);
static void SetPortNumber(void);
static void FastSleep(WORD Delay);
static void MyInt64Add(PLARGE_INTEGER pA, PLARGE_INTEGER pB);
static BOOL MyInt64LessThanOrEqualTo(PLARGE_INTEGER pA, PLARGE_INTEGER pB);
//-----------------
//Win32s typedefs
//-----------------
typedef BOOL (WINAPI * PUTREGISTER) ( HANDLE hModule,
LPCSTR lpsz16BitDLL,
LPCSTR lpszInitName,
LPCSTR lpszProcName,
UT32PROC * ppfn32Thunk,
FARPROC pfnUT32Callback,
LPVOID lpBuff
);
typedef VOID (WINAPI * PUTUNREGISTER) (HANDLE hModule);
typedef DWORD (APIENTRY * PUT32CBPROC) (LPVOID lpBuff, DWORD dwUserDefined );
//-----------------
//Win32s variables
//-----------------
UT32PROC pfnUTProc = NULL;
PUTREGISTER pUTRegister = NULL;
PUTUNREGISTER pUTUnRegister = NULL;
int cProcessesAttached = 0;
HANDLE hKernel32 = 0;
//------------------
//Win95/NT variables
//------------------
HANDLE hSemaphore = NULL, // Handle of Semaphore
hSA16CompatSem= NULL; // Semaphore for Win16 compatibility
HANDLE hDriver = NULL; // Handle of Driver
HANDLE hThread = NULL; // Handle of Thread
HANDLE hPassThruSem = NULL; // Handle of PassThru Semaphore
uchar RomDta[8] = {0,0,0,0,0,0,0,0}; // Current ROM Id
uchar DriverBuff[4]; // Buffer for exchanging data with driver
void *lpDriverBuff = DriverBuff; // Pointer to data buffer
BOOL AuthFlag = FALSE; // Thread has driver control?
BOOL SemCreated = FALSE; // Semaphore created yet?
//////////////////////////////////////////////////////////
/////////////// software auth vars ///////////////////////
static uchar Compatl0 = 0; // for SACWD300.dll compatability
static ushort pa_vals[3] = { 0x378, 0x278, 0x3BC };
ushort bpa; // (OWPD) made non-static
////////////////////////////////////////////////////////////////////////////////
//==============================================================================
// DLLInit
//==============================================================================
BOOL WINAPI DllMain(HANDLE hInst, DWORD fdwReason, LPVOID lpReserved)
{
DWORD dwVersion;
DWORD dwMajorVersion, dwMinorVersion;
BOOL retVal;
if ( fdwReason == DLL_PROCESS_ATTACH )
{
// Registration of UT need to be done only once for first
// attaching process. At that time set the Win32s flag
// to indicate if the DLL is executing under Win32s or not.
if( cProcessesAttached++ )
{
return(TRUE); // Not the first initialization.
}
// Find out if we're running on Win32s
dwVersion = GetVersion();
dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
if(dwVersion < 0x80000000)// NT
{
WinNT = TRUE;
Win32s = Win95 = FALSE;
return TRUE;
}
else if (dwMajorVersion < 4) // Win32s
{
Win32s = TRUE;
Win95 = WinNT = FALSE;
}
else // Windows 95 -- No build numbers provided
{
Win95 = TRUE;
WinNT = Win32s = FALSE;
return TRUE;
}
hKernel32 = LoadLibrary( "Kernel32.Dll" ); // Get Handle to Kernel32.Dll
pUTRegister = (PUTREGISTER) GetProcAddress( hKernel32, "UTRegister" );
if( !pUTRegister )
return(FALSE); // Error - On Win32s, but can't find UTRegister
pUTUnRegister = (PUTUNREGISTER) GetProcAddress( hKernel32, "UTUnRegister" );
if( !pUTUnRegister )
return(FALSE); // Error - On Win32s, but can't find UTUnRegister
retVal = (*pUTRegister)( hInst, // UTSamp32.DLL module handle
"SWA16UT.DLL", // name of 16-bit thunk dll
NULL, // name of init routine
"UTProc", // name of 16-bit dispatch routine (in utsamp16)
&pfnUTProc, // Global variable to receive thunk address
NULL, // callback function
NULL ); // no shared memroy
}
if( (fdwReason == DLL_PROCESS_DETACH) && (0 == --cProcessesAttached) && Win32s )
{
(*pUTUnRegister)( hInst );
FreeLibrary( hKernel32 );
}
return 1;
}
////////////////////////////////////////////////////////////////////////////////
static uchar CallDriver(DWORD FuncNum, uchar Param)
{
uchar *pByte = DriverBuff; // first byte is data
ushort *pBPA; // next two are the port address
DWORD BuffSize = 0; // driver returns number of data bytes to us
(uchar *)pBPA = &DriverBuff[1];
memset(DriverBuff,0,sizeof(DriverBuff)); // clear buffer,
*pByte = Param; // set parameter value
*pBPA = bpa; // and port address
DeviceIoControl(hDriver, // communicate with driver
FuncNum,
lpDriverBuff,sizeof(DriverBuff),
lpDriverBuff,sizeof(DriverBuff),(LPDWORD)(&BuffSize),
NULL);
return (*pByte);
}
////////////////////////////////////////////////////////////////////////////////
void CleanUp(void)
{
if(hSemaphore)
CloseHandle(hSemaphore);// release semaphore handle to system
}
////////////////////////////////////////////////////////////////////////////////
uchar APIENTRY garbagebag(uchar *ld)// Added for Win16 compatibility
{ // returns lastone for SACWD300.dll
*ld = Compatl0;
return Compatl0;
}
////////////////////////////////////////////////////////////////////////////////
uchar APIENTRY OverdriveOn(void)
{
if(!Win32s)
{
// (OWPD) if (!DOWReset())
// (OWPD) return FALSE;
// (OWPD) // Put all overdrive parts into overdrive mode.
// (OWPD) DOWByte(0x3C);
return ToggleOverdrive() ? TRUE : ToggleOverdrive();
}
return FALSE;
}
////////////////////////////////////////////////////////////////////////////////
void APIENTRY OverdriveOff(void)
{
if(!Win32s)
{
// Turn overdrive off
if(ToggleOverdrive())
ToggleOverdrive();
}
return;
}
////////////////////////////////////////////////////////////////////////////////
static uchar EnterPassthru(void)
{
if(Win95 || WinNT)
{
TogglePassthru();
Passthru = TRUE;
return TRUE;
}
return FALSE;
}
////////////////////////////////////////////////////////////////////////////////
static uchar ExitPassthru(void)
{
uchar i = 0;
if(!Win32s)
{
Passthru = TRUE;
while(Passthru && (i++ < 3))
{
TogglePassthru();
Passthru = !CheckBusy();// if busy lines don't drop we're in passthru mode
}
return !Passthru;
}
return FALSE;
}
////////////////////////////////////////////////////////////////////////////////
BOOL LoadDriver(void)
{
SC_HANDLE hSCManager, hDS1410D;
char Service[] = "DS1410D";
DWORD LastError;
/* Get a handle to the Service control manager database */
hSCManager = OpenSCManager(
NULL,
NULL,
SC_MANAGER_ALL_ACCESS);
if(hSCManager)
{
/* get a handle to our driver */
hDS1410D = OpenService(hSCManager,
Service,
SERVICE_ALL_ACCESS);
if(hDS1410D)
{
/* attempt to start DS1410D.SYS */
if(StartService(hDS1410D, 0, NULL))
return TRUE;
else
{
LastError = GetLastError();
return FALSE;
}
}
else
{
hDS1410D= CreateService(
hSCManager, // handle to service control manager database
Service, // pointer to name of service to start
Service, // pointer to display name
SERVICE_ALL_ACCESS, // type of access to service
SERVICE_KERNEL_DRIVER, // type of service
SERVICE_AUTO_START, // when to start service
SERVICE_ERROR_NORMAL, // severity if service fails to start
"SYSTEM32\\drivers\\DS1410D.SYS", // pointer to name of binary file
"Extended Base", // pointer to name of load ordering group
NULL, // pointer to variable to get tag identifier
NULL, // pointer to array of dependency names
NULL, // pointer to account name of service
NULL // pointer to password for service account
);
if(hDS1410D)
{
/* attempt to start DS1410D.SYS */
if(StartService(hDS1410D, 0, NULL))
return TRUE;
else
return FALSE;
}
}
}
else
return FALSE;
return FALSE; // (OWPD) get all control paths
}
// This function is called on NT to determine if the driver is loaded on the System
// CheckDriver should only be called by dowcheck()
static uchar CheckDriver(void)
{
SC_HANDLE hSCManager, hDS1410D;
char Service[] = "DS1410D";
/* Get a handle to the Service control manager database */
hSCManager = OpenSCManager(
NULL,
NULL,
SC_MANAGER_CONNECT);//(3.11a fix)
if(hSCManager)
{
/* get a handle to our driver */
hDS1410D = OpenService(hSCManager,
Service,
SERVICE_QUERY_STATUS);//(3.11a fix)
if(hDS1410D)
{
CloseServiceHandle(hDS1410D);
CloseServiceHandle(hSCManager);
return TRUE;
}
else
{
CloseServiceHandle(hSCManager);
return FALSE;
}
}
return FALSE;
}
////////////////////////////////////////////////////////////////////////////////
uchar APIENTRY dowcheck(void) // Must be used in All Apps!!!!!
{
DWORD dwVersion;
DWORD dwMajorVersion, dwMinorVersion;
dwVersion = GetVersion();
dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
if(dwVersion < 0x80000000) // NT
{
WinNT = TRUE;
Win32s = Win95 = FALSE;
}
else if (dwMajorVersion < 4) // Win32s
{
Win32s = TRUE;
WinNT = Win95 = FALSE;
}
else // Windows 95 -- No build numbers provided
{
Win32s = WinNT = FALSE;
Win95 = TRUE;
}
if(Win95)
{
if((!hDriver) || (hDriver == INVALID_HANDLE_VALUE))
hDriver=CreateFile("\\\\.\\VSAUTHD.VXD",0,0,NULL,OPEN_ALWAYS,
FILE_FLAG_DELETE_ON_CLOSE,NULL);
if(hDriver==INVALID_HANDLE_VALUE)
return FALSE; // could not get handle to our driver
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -