|
|
发表于 2008-4-7 23:12:35
|
显示全部楼层
贴一个WINIO自带的例子:6 w$ s/ w4 M, I7 f3 J8 v
#include <windows.h>
/ L& \, Z* L' S5 M' b#include <stdio.h>
: M2 A6 ~: }, k& N#include "winio.h"
, G5 {. w/ `% |% m, J0 W3 e b9 r
% d& p) ^( w$ s: |, `( D# n4 j+ Mvoid main()
# C$ E' U4 Y" e; o6 T{
: a9 M! U. ?! t0 J7 |( Y DWORD dwPortVal;
/ H* O5 e) \3 R- P9 z2 Y5 u3 O DWORD dwMemVal;
) U7 g; ^7 V2 \; F- d# f, D0 [ bool bResult;
4 ?" |9 p8 o# h4 d( ]* S- {4 | HANDLE hPhysicalMemory;" k, p6 V( a4 F, X- u. j# C! Z
PBYTE pbLinAddr;
8 J8 `! J! ^1 b, z: p+ F7 _- b/ ?3 D( C" S* h7 n$ o, s
// Call InitializeWinIo to initialize the WinIo library.0 O, [5 N! W" }& T5 A
4 j V/ v, d* S1 o1 | bResult = InitializeWinIo();7 a) ~+ o0 j5 O& B$ m) q: B6 H
/ O- H, b3 e W5 u' H% | F) g if (bResult): x" s$ z% s3 C
{
# E) d9 l" z1 y, V) N // Under Windows NT/2000/XP, after calling InitializeWinIo,
0 j: E& ~; e; h" d$ Z% W# m // you can call _inp/_outp instead of using GetPortVal/SetPortVal0 V! J, @; W' D1 K
; |4 i H) f" ^: l# b) P
GetPortVal(0x378, &dwPortVal, 4);, n5 c4 ?2 V0 x* g" \
( m& |+ m9 C. O
SetPortVal(0x378, 10, 4);$ Y) m8 a& [' g7 ] Q2 W. S; a
3 w3 t8 l% A/ i // Map physical addresses 0xA0000 - 0xAFFFF into the linear address space2 n: |: B% e" P* }2 R/ ~% B/ T# t
// of the application. The value returned from the call to MapPhysToLin is8 [+ J- x# T1 v1 T \* D
// a linear address corresponding to physical address 0xA0000. In case of
6 q; } F$ A7 I- O J2 ^% B // an error, the return value is NULL.: G' F( V, P, v/ C' |% Z) ~2 O% N
' x& h- t2 }3 y: ?% f0 ]4 H8 y pbLinAddr = MapPhysToLin((PBYTE)0xA0000, 65536, &hPhysicalMemory);, w& @( T3 ^7 @" h/ o
: m8 `( T9 g. x1 H1 m, B if (pbLinAddr)8 {% h" z N4 S+ f( d- V8 [8 o; a
{
/ z8 X# V/ S9 N } ^: n // Now we can use pbLinAddr to access physical address 0xA00005 l* f. [* Q5 |# |: z3 ~- o
, S. M5 M5 H: k6 B [! Q, N" x *pbLinAddr = 10;
$ i3 U, l+ Z/ Z; J9 H4 c) l. g: h/ K/ C9 o. M
// When you're done with pbLinAddr, call UnmapPhysicalMemory
* y7 @5 }# W$ @/ |, u
; }! A# ~2 M2 D9 {# d% Y" E: J/ t UnmapPhysicalMemory(hPhysicalMemory, pbLinAddr);
3 R% W' k% |$ y Q6 i9 H/ y }) v8 c& K+ W! L. E" R
1 I/ g. S# N8 A" ^
// Instead of using MapPhysToLin, we can use GetPhysLong/SetPhysLong7 i: M: a' {$ c I6 r2 a
1 L& x( d" f: O" | M7 F: S! {. y GetPhysLong((PBYTE)0xA0000, &dwMemVal);
" t8 i% f0 L7 Y2 I8 S& v/ g( b9 v
, ^3 f# t+ a7 J4 |* ^ k( J# E SetPhysLong((PBYTE)0xA0000, 10);
0 v! c! j; B: L9 l
2 l4 V: d) U u2 @5 w, @2 R$ f/ v1 R // When you're done using WinIo, call ShutdownWinIo8 `' ^2 e/ o' N4 B3 D
, b' T, Z$ P$ j1 g6 p2 k' U. t
ShutdownWinIo();1 Y" _* P6 l5 [% z$ F% K5 ?
}
' b+ {* l u: X" _, H else3 m' e2 L% @# b& p: ?+ [
{
7 Q# S/ |" Z1 q2 ~, R: h3 s) y printf("Error during initialization of WinIo.\n");, v) J2 ^( I, ?1 M4 x
exit(1);2 R$ T) v$ N3 \: \
}5 X+ q4 p, m9 d. ?
} |
|