使用inpout32.dll将Visual Basic并行端口应用程序转换为Delphi

时间:2011-11-23 10:20:39

标签: vb.net delphi parallel-port

我已经获得了这个简单的VB应用程序和库,我被告知可以在0x378基地址打开一个连接到打印机端口的门/转动。

   'Inp and Out declarations for port I/O using inpout32.dll.

Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" _
    (ByVal PortAddress As Integer) _
    As Integer

Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" _
    (ByVal PortAddress As Integer, _
    ByVal Value As Integer)

------------------------------------------------------------------------------------
Option Explicit
Dim Value As Integer
Dim PortAddress As Integer

Private Sub cmdWriteToPort_Click()
'Write to a port.
Out PortAddress, Value
'Read back and display the result.
Text1.Text = Inp(PortAddress)
Value = Value + 1
If Value = 255 Then Value = 0
End Sub

Private Sub Form_Load()
'Test program for inpout32.dll
Value = 0
'Change PortAddress to match the port address to write to:
'(Usual parallel-port addresses are &h378, &h278, &h3BC)
PortAddress = &H378
End Sub

但是我需要在Delphi 5中重新编写它以集成到我的应用程序中。

  1. 是否可以通过D5访问同一个库?
  2. 我是否正确使用以下代码?
  3. //使用库

    的端口I / O的Inp和Out声明
    function Inp(PortAddress:String); external 'inpout32.dll.dll'
    begin
     return ??
    end;
    
    procedure Output(PortAddress:String;Value:Integer); external 'inpout32.dll.dll'
    
    procedure TForm1.FormActivate(Sender: TObject);
    begin
    //Test program for inpout32.dll
    
    Value := 0;
    
    //Change PortAddress to match the port address to write to:
    //(Usual parallel-port addresses are &h378, &h278, &h3BC)
    
    PortAddress := '&H378';
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    
    //Write to a port.
    Output(PortAddress, Value);
    
    //Read back and display the result.
    Edit1.Text := Inp(PortAddress);
    Value := Value + 1;
    
    if Value = 255 then
    Value := 0;
    
    end;
    

    我不确定如何声明库函数以及声明变量的内容(& H378显然不是整数)

    感谢

2 个答案:

答案 0 :(得分:3)

PortAddress声明为Integer,因此不要使用字符串。您的代码应如下所示:

//Inp and Out declarations for port I/O using inpout32.dll.
function Inp(PortAddress: Integer): Integer; stdcall; external 'inpout32.dll' name 'Inp32';

procedure Output(PortAddress, Value: Integer); stdcall; external 'inpout32.dll' name 'Out32';

procedure TForm1.FormActivate(Sender: TObject);
begin
  //Test program for inpout32.dll
  Value := 0;

  //Change PortAddress to match the port address to write to:
  //(Usual parallel-port addresses are $378, $278, $3BC)
  PortAddress := $378;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //Write to a port.
  Output(PortAddress, Value);

  //Read back and display the result.
  Edit1.Text := IntToStr(Inp(PortAddress));
  Value := Value + 1;

  if Value = 255 then
    Value := 0;
end;

答案 1 :(得分:1)

你应该完全放弃使用 inpout32.dll ,因为它仅用于直接访问打印机端口并使代码转换复杂化。您可以使用ZLOPRTIO Delphi库来提高效率。