是否有可能使用Inpout32.dll来解决高端口?
本文关键字:解决 高端口 dll Inpout32 有可能 是否 | 更新日期: 2023-09-27 18:12:58
我想解决端口0xE020 (Hex ->12月= 57.376),但在短时间内是越界的。现在,我需要这个来测试PCI-E扩展并行端口。
由于这个问题的答案(https://stackoverflow.com/a/4618383/3391678),我使用以下32位环境的工作代码:
using System;
using System.Runtime.InteropServices;
namespace LPTx86
{
public class LPT
{
//inpout.dll
[DllImport("inpout32.dll")]
private static extern void Out32(short PortAddress, short Data);
[DllImport("inpout32.dll")]
private static extern char Inp32(short PortAddress);
private short _PortAddress;
public LPT(short PortAddress)
{
_PortAddress = PortAddress;
}
public void Write(short Data)
{
Out32(_PortAddress, Data);
}
public byte Read()
{
return (byte)Inp32(_PortAddress);
}
}
}
对于64位环境:
using System;
using System.Runtime.InteropServices;
namespace LPTx64
{
class LPT
{
[DllImport("inpoutx64.dll", EntryPoint = "Out32")]
private static extern void Out32_x64(short PortAddress, short Data);
[DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
private static extern char Inp32_x64(short PortAddress);
private short _PortAddress;
public LPT64(short PortAddress)
{
_PortAddress = PortAddress;
}
public void Write(short Data)
{
Out32_x64(_PortAddress, Data);
}
public byte Read()
{
return (byte)Inp32_x64(_PortAddress);
}
}
}
(是的,我去掉了所有的安全检查和健全检查,我的第二个名字是" dangerous ";)
是可能的,我怎么能解决端口不适合短?
我的页眉是这样的
void _stdcall Out32(short PortAddress, short data);
short _stdcall Inp32(short PortAddress);
被接受的答案有效,但我认为使用更长的地址的更习惯的方法是保持签名一致并在unchecked
上下文中转换为short
[DllImport("inpout32.dll")]
private static extern void Out32(short PortAddress, short data);
[DllImport("inpout32.dll")]
private static extern short Inp32(short PortAddress);
public void Write(int address, short data) {
short portAddress;
unchecked {
portAddress = (short)address;
}
Out32(portAddress, data);
}
public short Read(int address) {
short portAddress;
unchecked {
portAddress = (short)address;
}
return Inp32(portAddress);
}
这是可能的:
我尝试并改变端口变量的类型从短到int,我测试了它,它似乎工作:
编辑:重构代码,将数据/控制/状态端口合并到一个包中using System;
using System.Runtime.InteropServices;
namespace LPTtestdrive
{
class LPT_X
{
//inpout.dll
[DllImport("inpout32.dll")]
private static extern UInt32 IsInpOutDriverOpen();
[DllImport("inpout32.dll")]
private static extern void Out32(int PortAddress, short Data);
[DllImport("inpout32.dll")]
private static extern char Inp32(int PortAddress);
//inpoutx64.dll
[DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
private static extern UInt32 IsInpOutDriverOpen_x64();
[DllImport("inpoutx64.dll", EntryPoint = "Out32")]
private static extern void Out32_x64(int PortAddress, short Data);
[DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
private static extern char Inp32_x64(int PortAddress);
private bool _X64;
private int DataAddress;
private int StatusAddress;
private int ControlAddress;
public LPT_X(int PortAddress)
{
DataAddress = PortAddress;
StatusAddress = (int)(DataAddress + 1);
ControlAddress = (int)(DataAddress + 2);
//now the code tries to determine if it should use inpout32.dll or inpoutx64.dll
uint nResult = 0;
try
{
Console.WriteLine("trying 32 bits");
nResult = IsInpOutDriverOpen();
if (nResult != 0)
{
Console.WriteLine("using 32 bits");
return;
}
}
catch (BadImageFormatException)
{
Console.WriteLine("32 bits failed");
}
catch (DllNotFoundException)
{
throw new ArgumentException("Unable to find InpOut32.dll");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
try
{
Console.WriteLine("trying 64 bits");
nResult = IsInpOutDriverOpen_x64();
if (nResult != 0)
{
Console.WriteLine("using 64 bits");
_X64 = true;
return;
}
}
catch (BadImageFormatException)
{
Console.WriteLine("64 bits failed");
}
catch (DllNotFoundException)
{
throw new ArgumentException("Unable to find InpOutx64.dll");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
throw new ArgumentException("Unable to open either inpout32 and inpoutx64");
}
public void WriteData(short Data)
{
if (_X64)
{
Out32_x64(DataAddress, Data);
}
else
{
Out32(DataAddress, Data);
}
}
public void WriteControl(short Data)
{
if (_X64)
{
Out32_x64(ControlAddress, Data);
}
else
{
Out32(ControlAddress, Data);
}
}
public byte ReadData()
{
if (_X64)
{
return (byte)Inp32_x64(DataAddress);
}
else
{
return (byte)Inp32(DataAddress);
}
}
public byte ReadControl()
{
if (_X64)
{
return (byte)Inp32_x64(ControlAddress);
}
else
{
return (byte)Inp32(ControlAddress);
}
}
public byte ReadStatus()
{
if (_X64)
{
return (byte)Inp32_x64(StatusAddress);
}
else
{
return (byte)Inp32(StatusAddress);
}
}
}
}
像这样使用:
LPT_X LPT;
int read;
int Adress = 0xE020;
try
{
LPT = new LPT_X(Address);
LPT.WriteData(251);
LPT.WriteControl(0);
read = LPT.ReadStatus();
...
}