📄 tdlportio.cpp
字号:
{
return FActiveHW;
}
//---------------------------------------------------------------------------
// IsDriverOpened()
//---------------------------------------------------------------------------
bool _export __stdcall IsDriverOpened(void)
{
return FActiveHW;
}
//---------------------------------------------------------------------------
// TestHardAccess()
// SetHardAccess()
// This doesn't really do anything; provided for compatibility only
//---------------------------------------------------------------------------
bool _export __stdcall TestHardAccess(void) { return false; }
void _export __stdcall SetHardAccess(bool Access) {}
//---------------------------------------------------------------------------
// LastError()
// Returns the last error which occurred in Open/CloseDriver()
//---------------------------------------------------------------------------
char* _export __stdcall LastError(void)
{
return FLastError;
}
//---------------------------------------------------------------------------
// ReadPort()
//---------------------------------------------------------------------------
BYTE _export __stdcall ReadPort(WORD Address)
{
#ifndef FAST
return FActiveHW?DlReadByte(Address):(BYTE)0x00;
#else
return DlReadByte(Address);
#endif
}
//---------------------------------------------------------------------------
// ReadPortW()
//---------------------------------------------------------------------------
WORD _export __stdcall ReadPortW(WORD Address)
{
#ifndef FAST
return FActiveHW?DlReadWord(Address):(WORD)0x0000;
#else
return DlReadWord(Address);
#endif
}
//---------------------------------------------------------------------------
// ReadPortL()
//---------------------------------------------------------------------------
DWORD _export __stdcall ReadPortL(WORD Address)
{
#ifndef FAST
return FActiveHW?DlReadDWord(Address):(DWORD)0x00000000;
#else
return DlReadDWord(Address);
#endif
}
//---------------------------------------------------------------------------
// WritePort()
//---------------------------------------------------------------------------
void _export __stdcall WritePort(WORD Address, BYTE Data)
{
#ifndef FAST
if (FActiveHW) DlWriteByte(Address, Data);
#else
DlWriteByte(Address, Data);
#endif
}
//---------------------------------------------------------------------------
// WritePortW()
//---------------------------------------------------------------------------
void _export __stdcall WritePortW(WORD Address, WORD Data)
{
#ifndef FAST
if (FActiveHW) DlWriteWord(Address, Data);
#else
DlWriteWord(Address, Data);
#endif
}
//---------------------------------------------------------------------------
// WritePortL()
//---------------------------------------------------------------------------
void _export __stdcall WritePortL(WORD Address, DWORD Data)
{
#ifndef FAST
if (FActiveHW) DlWriteDWord(Address, Data);
#else
DlWriteDWord(Address, Data);
#endif
}
//---------------------------------------------------------------------------
// DetectPorts9x()
//---------------------------------------------------------------------------
void _export __stdcall DetectPorts9x()
{
const char *BASE_KEY = "Config Manager\\Enum";
const char *PROBLEM = "Problem";
const char *ALLOCATION = "Allocation";
const char *PORTNAME = "PortName";
const char *HARDWARE_KEY = "HardwareKey";
const REGSAM KEY_PERMISSIONS = KEY_ENUMERATE_SUB_KEYS |
KEY_QUERY_VALUE;
HKEY CurKey; // Current key when using the registry
char KeyName[MAX_PATH]; // A key name when using the registry
char **KeyList; // List of keys
DWORD KeyCount; // Count of the number of keys in KeyList
// Clear the port count
FLPTCount = 0;
// Clear the port array
for (int index=0; index<=MAX_LPT_PORTS; index++)
FLPTAddress[index] = 0;
// Open the registry
RegOpenKeyEx(HKEY_DYN_DATA, BASE_KEY, 0, KEY_PERMISSIONS, &CurKey);
// Grab all the key names under HKEY_DYN_DATA
//
// Do this by first counting the number of keys,
// then creating an array big enough to hold them
// using the KeyList pointer.
FILETIME DummyFileTime;
DWORD DummyLength = MAX_PATH;
KeyCount = 0;
while (RegEnumKeyEx(
CurKey, KeyCount++, KeyName, &DummyLength,
NULL, NULL, NULL, &DummyFileTime
) != ERROR_NO_MORE_ITEMS)
{
DummyLength = MAX_PATH;
}
KeyList = new char*[KeyCount];
KeyCount = 0;
DummyLength = MAX_PATH;
while (RegEnumKeyEx(
CurKey, KeyCount, KeyName, &DummyLength,
NULL, NULL, NULL, &DummyFileTime
) != ERROR_NO_MORE_ITEMS)
{
KeyList[KeyCount] = new char[DummyLength+1];
strcpy(KeyList[KeyCount], KeyName);
DummyLength = MAX_PATH;
KeyCount++;
}
// Close the key
RegCloseKey(CurKey);
// Cycle through all keys; looking for a string valued subkey called
// 'HardWareKey' which is not NULL, and another subkey called 'Problem'
// whose fields are all valued 0.
for (DWORD KeyIndex=0; KeyIndex<KeyCount; KeyIndex++)
{
bool HasProblem = false; // Is 'Problem' non-zero? Assume it is Ok
// Open the key
strcpy(KeyName, BASE_KEY);
strcat(KeyName, "\\");
strcat(KeyName, KeyList[KeyIndex]);
if (RegOpenKeyEx(
HKEY_DYN_DATA, KeyName, 0, KEY_PERMISSIONS, &CurKey
) != ERROR_SUCCESS)
continue;
// Test for a 0 valued Problem sub-key,
// which must only consist of raw data
DWORD DataType, DataSize;
RegQueryValueEx(CurKey, PROBLEM, NULL, &DataType, NULL, &DataSize);
if (DataType == REG_BINARY)
{
// We have a valid, binary "Problem" sub-key
// Test to see if the fields are zero
char HardwareSubKey[MAX_PATH];
// Data from the "Hardware" sub-key
BYTE *Data = new BYTE[DataSize];
// Data from "Problem" sub-key
// Read the data from the "Problem" sub-key
if (RegQueryValueEx(
CurKey, PROBLEM, NULL,
NULL, Data, &DataSize
) == ERROR_SUCCESS)
{
// See if it has any problems
for (DWORD index=0; index<DataSize; index++)
HasProblem |= Data[index];
}
else
HasProblem = true; // No good
delete[] Data;
// Now try and read the Hardware sub-key
DataSize = MAX_PATH;
RegQueryValueEx(
CurKey, HARDWARE_KEY, NULL, &DataType, HardwareSubKey, &DataSize
);
if (DataType != REG_SZ)
HasProblem = true; // No good
// Do we have no problem, and a non-null Hardware sub-key?
if (!HasProblem && strlen(HardwareSubKey) > 0)
{
// Now open the key which is "pointed at" by HardwareSubKey
RegCloseKey(CurKey);
strcpy(KeyName, "Enum\\");
strcat(KeyName, HardwareSubKey);
if (RegOpenKeyEx(
HKEY_LOCAL_MACHINE, KeyName, 0, KEY_PERMISSIONS, &CurKey
) != ERROR_SUCCESS)
continue;
// Now read in the PortName and obtain the LPT number from it
char PortName[MAX_PATH];
DataSize = MAX_PATH;
RegQueryValueEx(
CurKey, PORTNAME, NULL, &DataType, PortName, &DataSize
);
if (DataType != REG_SZ)
strcpy(PortName, ""); // No good
// Make sure it has LPT in it
if (strstr(PortName, "LPT") != NULL)
{
int PortNumber;
// The nubmer of the port
char PortNumberStr[MAX_PATH];
// Holds the number of the port as a string
WORD Allocation[64];
// Holds the registry data for the port address allocation
memset(PortNumberStr, '\0', MAX_PATH);
strncpy(PortNumberStr,
strstr(PortName, "LPT")+3,
strlen(PortName)-(strstr(PortName, "LPT")-PortName)-2);
// Find the port number
PortNumber = atoi(PortNumberStr);
// Find the address
RegCloseKey(CurKey);
strcpy(KeyName, BASE_KEY);
strcat(KeyName, "\\");
strcat(KeyName, KeyList[KeyIndex]);
RegOpenKeyEx(HKEY_DYN_DATA, KeyName, 0, KEY_PERMISSIONS, &CurKey);
DataSize = sizeof(Allocation);
RegQueryValueEx(
CurKey, ALLOCATION, NULL, &DataType,
(unsigned char*)Allocation, &DataSize
);
if (DataType == REG_BINARY)
{
// Decode the Allocation data: the port address is present
// directly after a 0x000C entry (which doesn't have 0x0000
// after it).
for (int pos=0; pos<63; pos++)
if (Allocation[pos] == 0x000C &&
Allocation[pos+1] != 0x0000 &&
PortNumber<=MAX_LPT_PORTS)
{
// Found a port; add it to the list
FLPTAddress[PortNumber] = Allocation[pos+1];
FLPTCount++;
break;
}
}
}
}
}
RegCloseKey(CurKey);
}
// Destroy our key list
for (DWORD index=0; index<=KeyCount; index++)
delete[] KeyList[index];
delete KeyList;
}
//---------------------------------------------------------------------------
// DetectPortsNT()
//---------------------------------------------------------------------------
void _export __stdcall DetectPortsNT()
{
const char *BASE_KEY = "HARDWARE\\DEVICEMAP\\PARALLEL PORTS";
const char *LOADED_KEY = "HARDWARE\\RESOURCEMAP\\LOADED PARALLEL DRIVER RESOURCES\\Parport";
const char *DOS_DEVICES = "\\DosDevices\\LPT";
const char *DEVICE_PARALLEL = "\\Device\\Parallel";
const REGSAM KEY_PERMISSIONS = KEY_ENUMERATE_SUB_KEYS |
KEY_QUERY_VALUE;
HKEY CurKey; // Current key when using the registry
char KeyName[MAX_PATH]; // A key name when using the registry
char **ValueList; // List of value names
DWORD ValueCount; // Count of the number of value names in ValueList
// Clear the port count
FLPTCount = 0;
// Clear the port array
for (int index=0; index<=MAX_LPT_PORTS; index++)
FLPTAddress[index] = 0;
// Open the registry
if (RegOpenKeyEx(
HKEY_LOCAL_MACHINE, BASE_KEY, 0, KEY_PERMISSIONS, &CurKey
) != ERROR_SUCCESS)
return; // Can't do anything without this BASE_KEY
// Grab all the value names under HKEY_LOCAL_MACHINE
//
// Do this by first counting the number of value names,
// then creating an array big enough to hold them
// using the ValueList pointer.
DWORD DummyLength = MAX_PATH;
DWORD ValueType;
ValueCount = 0;
while (RegEnumValue(
CurKey, ValueCount++, KeyName, &DummyLength,
NULL, &ValueType, NULL, NULL
) != ERROR_NO_MORE_ITEMS)
{
DummyLength = MAX_PATH;
}
ValueList = new char*[ValueCount];
ValueCount = 0;
DummyLength = MAX_PATH;
while (RegEnumValue(
CurKey, ValueCount, KeyName, &DummyLength,
NULL, &ValueType, NULL, NULL
) != ERROR_NO_MORE_ITEMS)
{
ValueList[ValueCount] = new char[DummyLength+1];
strcpy(ValueList[ValueCount], KeyName);
DummyLength = MAX_PATH;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -