No announcement yet.

Native Hardware access to a single Byte from XP?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Native Hardware access to a single Byte from XP?

    Reading here in the forum, some say that access to hardware (ring 0) could only be done via using a "device driver".

    So I was reading a bit around in the internet and there seem to be another possibility:

    Another possible alternative is to modify the I/O permission bitmap to allow a particular task, access to certain I/O ports. This grants your usermode program running in ring 3 to do unrestricted I/O operations on selected ports, per the I/O permission bitmap.

    ( See ).

    Did anyonde do something like this before?
    I would like to see a source code, which just writes to a single hardware-byte.

    Then I saw another intresting thing, this looks like a "universal Hardware-Access-Driver".

    However, all examples are in C-Code. If someone is intrested to take a look on it, here is the adress: ( ). I just took a rough look on the doku.

    As well, I would like to know if it could be used from a PB-Program.
    If someone is also intrested in these things or has experience,
    I would appreciate some "start code" which this forum is full of, just not for these things.

    Maybe people do most of these things in C, which I try to make a circle around.

    As a result I only want to make a very simple operation on a hardware register from PB.

    --Theo Gottwald

  • #2
    i've used winio from it gives an application direct access to all i/o ports.
    i don't know how much use to you the following code is but it shows how to install winio, call it from a pb program running in winxp and it shows the lines where it originally used to call a device driver which are now commented out and replaced with direct access of the i/o port.

    !mov dx,&h201               'new way, direct acces of i/o port &h201
    !in al,dx
    !mov joy%,ax
    'getportval(&h201, joy&,1)  'original way, call a device driver
    'joy%=joy& and 255
    i hope it's of some use,
    rem pb7.03 program
    rem check the joystick port and use the buttons to simulate key presses on the keyboard.
    rem this one uses winio to access the joystick port under winxp
    $include ""
    '####### winio declares
    'must also include winio.vxd, winio.dll and winio.sys is the same directory as the comilped exe
    declare function mapphystolin lib "winio.dll" alias "mapphystolin" (byval physaddr as long, byval physsize as long, byref physmemhandle as long) as long
    declare function unmapphysicalmemory lib "winio.dll" alias "unmapphysicalmemory" (byval physmemhandle as long, byval linaddr as long) as long
    declare function getphyslong lib "winio.dll" alias "getphyslong" (byval physaddr as long, byref physval as long) as long
    declare function setphyslong lib "winio.dll" alias "setphyslong" (byval physaddr as long, byval physval as long) as long
    declare function getportval lib "winio.dll" alias "getportval" (byval portaddr as integer, byref portval as long, byval bsize as byte) as long
    declare function setportval lib "winio.dll" alias "setportval" (byval portaddr as integer, byval portval as long, byval bsize as byte) as long
    declare function initializewinio lib "winio.dll" alias "initializewinio"  () as long
    declare function shutdownwinio lib "winio.dll" alias "shutdownwinio" () as long
    declare function installwiniodriver lib "winio.dll" alias "installwiniodriver" (byval driverpath as string, byval mode as integer) as long
    declare function removewiniodriver lib "winio.dll" alias "removewiniodriver" () as long
    function apppath() as string
    'get the path to the current running exe file
    ' [url=""][/url] 
    local szpath as asciiz * %max_path
    getmodulefilename getmodulehandle(byval %null), szpath, %max_path
    if instr(-1, szpath, "\") > 0 then
       apppath = left$(szpath, instr(-1, szpath, "\"))
    end if
    end function
    function pbmain() as long
    local path as asciiz * %max_path
    path = apppath
    joy%=0 :rem the value of the joystick port byte
    ctlq$=chr$(17): rem quit whren ctrl-q is pressed
    rem the button locations within the joystick byte
    rem button flags
    rem the previous button values
    oldfa1&=0  :oldfa2&=0  :oldfb1&=0  :oldfb2&=0
    rem the keys to stuff for each button
    keya1&=13:rem enter
    keya2&=32:rem space
    keyb1&=67:rem c
    keyb2&=68:rem d
    if keya1&<33 then a1$="chr$("+str$(keya1&)+")" else a1$=chr$(keya1&)
    if keya2&<33 then a2$="chr$("+str$(keya2&)+")" else a2$=chr$(keya2&)
    if keyb1&<33 then b1$="chr$("+str$(keyb1&)+")" else b1$=chr$(keyb1&)
    if keyb2&<33 then b2$="chr$("+str$(keyb2&)+")" else b2$=chr$(keyb2&)
    if initializewinio=0 then
        'can't initialise it, try to instal it..
        winioflag& =installwiniodriver(path, 0)
        if winioflag& = 0 then
            msgbox "could not install winio"  :exit function
            'installed, now initialise it
            if  initializewinio=0 then msgbox "could not initialise winio": exit function
        end if
    end if
    rem &h201 is the joystick port
    !mov dx,&h201
    !in al,dx
    !mov joy%,ax
    'getportval(&h201, joy&,1)
    'joy%=joy& and 255
    rem separate the individual bits out for the buttons
    if (joy% and a1&) then fa1&=1 else fa1&=0
    if (joy% and a2&) then fa2&=1 else fa2&=0
    if (joy% and b1&) then fb1&=1 else fb1&=0
    if (joy% and b2&) then fb2&=1 else fb2&=0
    rem if it was a key press then stuff the corresponding character into the keyboard buffer
    rem key press
    if oldfa1&=1 and fa1&=0 then keybd_event keya1&,mapvirtualkey(keya1&,0), 0, 0
    if oldfa2&=1 and fa2&=0 then keybd_event keya2&,mapvirtualkey(keya2&,0), 0, 0
    if oldfb1&=1 and fb1&=0 then keybd_event keyb1&,mapvirtualkey(keyb1&,0), 0, 0
    if oldfb2&=1 and fb2&=0 then keybd_event keyb2&,mapvirtualkey(keyb2&,0), 0, 0
    rem key release
    if oldfa1&=0 and fa1&=1 then keybd_event keya1&,mapvirtualkey(keya1&,0), %keyeventf_keyup, 0
    if oldfa2&=0 and fa2&=1 then keybd_event keya2&,mapvirtualkey(keya2&,0), %keyeventf_keyup, 0
    if oldfb1&=0 and fb1&=1 then keybd_event keyb1&,mapvirtualkey(keyb1&,0), %keyeventf_keyup, 0
    if oldfb2&=0 and fb2&=1 then keybd_event keyb2&,mapvirtualkey(keyb2&,0), %keyeventf_keyup, 0
    rem copy current values to old values
    oldfa1&=fa1&  :oldfa2&=fa2&  :oldfb1&=fb1&  :oldfb2&=fb2&
    sleep 10 :rem give up the rest of the current time slice
    loop 'until inkey$=ctlq$
    end function

    [this message has been edited by paul dixon (edited january 06, 2005).]


    • #3
      Thanks for this excellent code, Paul!

      I'll immediately take a look at it and try to learn whats going on inside.

      This is the sort of code, which makes this Forum one of the richest
      code-sources in the I-net (at least for me!).

      --Theo Gottwald

      [This message has been edited by Theo Gottwald (edited January 06, 2005).]


      • #4
        a few notes about the program.
        You can safely ignore most of it, the important parts are the DECLARES and the initialising/installing of WinIO. Once installed you can access I/O ports as you would in Win98 or DOS using the IN and OUT instructions.

        I've included all the DECLARES that the WinIO package uses but some are not needed for accessing I/O, they're used for accessing physical memory.

        I just noticed I don't uninstall the program before it quits, that's because this program was intended to run all of the time, it never exits. Most programs should call ShutDownWinIO before exiting to free up any resources it uses.

        The WinIO program sets the I/O permission bitmap to allow access to ALL I/O by the program rather than just certain ports, that's why I don't need to specify which port (&h201 in this case) that I need access to.

        Although WinIO will work from a non-admin account in WinXP it can only do this if it was installed first and installing it can only be done from a admin account.

        In case you're curious about the purpose of the program, it was to allow external switches to be used to control software for handicapped children. A keyboard with switch interface costs $150. The local school had no money so I plugged the switches into the joystick port for the cost of a connector ($5?) and wrote this program saving over $700 on 5 computers that needed these switches.




        • #5
          Thanks for posting that stuff, Paul! I will find it very useful,
          even though I'm not the topic starter.

          mailto:[email protected][email protected]</A>


          • #6
            Thanks again, Paul.

            After doing some tests, my problem is solved with your help!

            --Theo Gottwald