Converting Visual Basic parallel port app using inpout32.dll in to Delphi

此生再无相见时 提交于 2019-12-02 05:32:01

问题


I've been given this simple VB application and library which I'm told can open a door/turnstyle attached to the printer port at 0x378 base address.

   '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

However I need to re-write it in Delphi 5 to integrate in to my application.

  1. Is it possible to access the same library through D5?
  2. Am I in the right direction with the following code?

//Inp and Out declarations for port I/O using library

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;

I'm not sure exactly how to declare the library functions and what to declare the variables as (&H378 is obviously not an integer)

thanks


回答1:


PortAddress is declared as an Integer, so don't use strings. Your code should look something like this:

//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;



回答2:


You should completely drop using inpout32.dll since it's only used to directly access printer port and complicates your code conversion. You can do the same much more efficient with ZLOPRTIO Delphi library.



来源:https://stackoverflow.com/questions/8240520/converting-visual-basic-parallel-port-app-using-inpout32-dll-in-to-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!