Try to use inpout32.dll

For issues with communication ports, protocols, etc.
calatorius
Posts: 18
Joined: Oct 05, 2020 8:39

Re: Try to use inpout32.dll

Post by calatorius »

Try again
if I use this pi file:

Code: Select all

#ifndef __FB_64BIT__
  extern "Windows-MS" lib "inpout32"
#else
  extern "Windows-MS" lib "inpoutx64"
#endif
  declare sub Out32(port as ushort, value as ushort)
  declare function Inp32(port as ushort) as ushort
end extern
and this basic code:

Code: Select all

#include "inpout32.bi"


#inclib "inpoutx64"

? "Hola mundo"
sleep
All is ok, compile perfect

if i use this bas:

Code: Select all

#include "inpout32.bi"


#inclib "inpoutx64"

? Inp32()
sleep
Result:
D:\basic\FreeBASIC-1.07.1-win64\prueba.bas(6) error 1: Argument count mismatch, found ')' in '? Inp32()'

and if I use this bas:

Code: Select all

#include "inpout32.bi"


#inclib "inpoutx64"

? Inp32(&h61)
sleep
then:
D:\basic\FreeBASIC-1.07.1-win64\prueba.o:fake:(.text+0x36): undefined reference to `Inp32'

Somebody could deduce which is the problem, it seems like the problem is in the argument pass in the function on?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Try to use inpout32.dll

Post by jj2007 »

? Inp32() obviously expects an argument. Why do you insist to use a "static" library? What I posted above works fine.
calatorius
Posts: 18
Joined: Oct 05, 2020 8:39

Re: Try to use inpout32.dll

Post by calatorius »

Because I tried too with your code in 32 and 64 bits

Code: Select all

#inclib "InpOut32"
Declare Sub Out32 Alias "Out32" (data As short, port As short)
Dim As short MyData=1, MyPort=2
Out32(MyData, MyPort)
Print "all is fine"
sleep
result:
D:\basic\FreeBASIC>fbc prueba.bas
prueba.o:fake:(.text+0x39): undefined reference to `Out32@8'

Code: Select all

#inclib "InpOutx64"
Declare Sub Out32 Alias "Out32" (data As short, port As short)
Dim As short MyData=1, MyPort=2
Out32(MyData, MyPort)
Print "all is fine"
sleep
Result:
Compiler output:
D:\basic\FreeBASIC-1.07.1-win64\prueba.o:fake:(.text+0x47): undefined reference to `Out32'
Xusinboy Bekchanov
Posts: 782
Joined: Jul 26, 2018 18:28

Re: Try to use inpout32.dll

Post by Xusinboy Bekchanov »

calatorius wrote:Because I tried too with your code in 32 and 64 bits

Code: Select all

#inclib "InpOut32"
Declare Sub Out32 Alias "Out32" (data As short, port As short)
Dim As short MyData=1, MyPort=2
Out32(MyData, MyPort)
Print "all is fine"
sleep
result:
D:\basic\FreeBASIC>fbc prueba.bas
prueba.o:fake:(.text+0x39): undefined reference to `Out32@8'

Code: Select all

#inclib "InpOutx64"
Declare Sub Out32 Alias "Out32" (data As short, port As short)
Dim As short MyData=1, MyPort=2
Out32(MyData, MyPort)
Print "all is fine"
sleep
Result:
Compiler output:
D:\basic\FreeBASIC-1.07.1-win64\prueba.o:fake:(.text+0x47): undefined reference to `Out32'
Have you tried it:

Code: Select all

Declare Sub Out32 Lib "InpOut32" (data As short, port As short)
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Try to use inpout32.dll

Post by dodicat »

This is OK with 32 bit fbc, -gen gcc or -gen gas.
Note, gcc 5.2.0 with fbide.
If I use option -exx I get error 10 ("illegal instruction" signal) fault, but no problems calling the function.

Code: Select all

#inclib "InpOut32"
Declare Sub Out32 cdecl Alias "Out32" (data_ As short, port As short)

Dim As short MyData=1, MyPort=2

Out32(MyData, MyPort)
Print "all is fine"
sleep 
I had to use cdecl to avoid a warning.
I didn't have to make an import library, but here is my .def file anyway

Code: Select all

;
; Definition file of inpout32.dll
; Automatic generated by gendef
; written by Kai Tietz 2008
;
LIBRARY "inpout32.dll"
EXPORTS
Out32@8
Inp32@4
 
I keep the .dll file in the same folder as the test.bas file (as paul doe mentioned earlier)
There is no point in putting a .dll into any of your system folders unless you really really like it and want to use it.
You will probably have to make an import library (InpOut32.dll.a) with dlltool.exe and the .def file for this if you want to do this, and put the .a file into your freebasic lib folder along with all the other .a files.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Try to use inpout32.dll

Post by jj2007 »

calatorius wrote:Because I tried too with your code in 32 and 64 bits

Code: Select all

#inclib "InpOut32"
Declare Sub Out32 Alias "Out32" (data As short, port As short)
Dim As short MyData=1, MyPort=2
Out32(MyData, MyPort)
Print "all is fine"
sleep
Strange. That snippet works fine for me with both GAS and Gcc32.
dodicat wrote:I had to use cdecl to avoid a warning
That does indeed suppress some warnings but I am astonished that it works. Under the hood this is clearly a stdcall function.
marcov
Posts: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Try to use inpout32.dll

Post by marcov »

Maybe the importlib stub changes calling convention?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Try to use inpout32.dll

Post by dodicat »

Here is a relevant page
http://www.highrez.co.uk/downloads/inpout32/
Probably just as well I got an illegal instruction call, I really don't need any drivers installed here on Win 10.
That was a close shave!
I am usually careful running unknown dll's.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Try to use inpout32.dll

Post by jj2007 »

marcov wrote:Maybe the importlib stub changes calling convention?
It certainly won't poke a retn (or retn 8) inside the DLL. There is a retn 8 at the end of out32, so it's stdcall.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Try to use inpout32.dll

Post by dodicat »

The sample C++ code in the link uses stdcall, but stdcall gives a warning here.

extern "c"
bla bla
end extern
is OK also.
calatorius
Posts: 18
Joined: Oct 05, 2020 8:39

Re: Try to use inpout32.dll

Post by calatorius »

EUREKA!

I got it, it's incredible but the only thing to run was to have the .def file create with pexports in inpoutx64.dll.
with this code:

Code: Select all

#inclib "InpOutx64"
Declare Sub Out32 Alias "Out32" (port As short, data As short)
Declare Function Inp32 Alias "Inp32" (port as short) as short
Dim As short MyData=2, MyPort=&h61
print inp32(&H3da)
Print "all is fine"
sleep
And the dll with its .def in the same directory compile and run fine.
The fact is that I don't know why inclib needs a def file :|
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Try to use inpout32.dll

Post by dodicat »

Glad you got it working.
FreeBASIC has inbuilt inp and out, so you wouldn't be stuck.
calatorius
Posts: 18
Joined: Oct 05, 2020 8:39

Re: Try to use inpout32.dll

Post by calatorius »

yes
but inbuild inp and out don´t work because are protected instructions in windows.
It´s necesary to use external libraries to do it.
Thanks very much to everyone for your help
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Try to use inpout32.dll

Post by MrSwiss »

calatorius wrote:It´s necesary to use external libraries to do it.
Well, that isn't exactly correct as stated.

The driver (elevated privileges in OS) that the ext. lib. uses 'does the trick' in this context.
danube7
Posts: 10
Joined: Nov 16, 2018 20:29

Re: Try to use inpout32.dll

Post by danube7 »

Hi Folks,

I've been following this thread, trying to reproduce some of the results, and have not succeeded at getting INP or OUT (or INP32 or OUT32) to work.

Can someone familiar with the code, both native to FB and the external libraries write up a concise summary of what works and what doesn't?

I would also like to point out that the FB help information is a bit vague. It reads:
"Using true port access in the Windows version requires the program to install a device driver for the present session. For that reason, Windows executables using hardware port access should be run with administrator permits each time the computer is restarted. Further runs don't require admin rights as they just use the already installed driver. The driver is only 3K in size and is embedded in the executable."

Does that mean that the compiled program (i.e. "Myfile.exe") needs to be run with admin privileges on first run? Or does it mean it needs to be compiled with admin privileges by elevating fbc when compiling?

To be clear, I am working with both FB 1.06 and 1.07.1 on a Windows 7 machine. I would also like to make sure this works on a windows 10 machine. My end goal is to try to manipulate the COM1 UART registers directly to open the com port (rather than just using OPEN COM) but so far I have had no luck. I don't have a PC speaker on this machine to test the functionality of OUT(). Is there another port that would work - say for example, one of the indicator LEDs on a typical PC?

I'm glad to see the FB community is actively working on the INP and OUT functions and the posts are current. Thanks!

Dan
Post Reply