
COMPUTERENGINEERING
1999ꡡ257ڡVol.25No.51999



VBDLLӲ˿ڿƷ
ˮ 
ժҪ Ҫ16λ32λWindowsϵͳ,û̬ӿĿüʵVBӲ˿ڵĿƷ
ؼ ̬ӿDLL Visual Basic Visual C++ ISA PCI
Computer Inport & Outport Control Method Based on DLL
Xu Jishui Li YI
Wuhan University of Hydraulic & Electric Engineering Wuhan 430072
AbstractThis article introduces the methods of progamming DLL under the 16-Bit and 32-Bit Windows environmentand clarifies the realization of VB controling computer Inport&Outport by Using DLL
Key wordsDynamic Link LibraryDLLVisual BasicVisual C++ISA busPCI bus
Visual Basic (VB)һֿӻıԣܹWindowsԱϲκһVBеıҪûṩдӲ˿ڡжϵӲƺһЩҵƳϣҪӲ忨ждǲVBдģͺбҪй䣬ʹвӲʵVBķһǱдDLLVBXȡ
1û̬ӿ(DLL)Ŀ
̬ӿDLL(Dynamic Link Library)Windowsص֮һWindowsУ̬ӿṩִвִд벿ֺĵ÷úִλڶ̬ӿУһʹǵĹ̷ֿ롢Ӻʹ洢ĺ봫ͳľ̬ӿȣŵҪУִʱӣͬӦóܹ˽Լڴռ䣻ڿɷعԵĳá
һDLLһ⣬һҪĳʱWindowsʹDLLж֮DLLWindowsӦóDLLWindowsڴ沢ִеġ
WindowsṩܶDLL,"User.Exe""Kernel.Exe""Gdi.Exe"ȡЩṩӲGUIƹߵȵȵĳǧWindowsӦóӿ(API)һ鶯̬ӿ⣬κʹWindows APIĹ̶ʹö̬ӡԱԸҪдԼӲ豸DLL豸⣬ʹWindowsܷ֧ǵӦ뿪
Visual C++1.52Visual C++2.0ṩDLL;ҪDLLҪһԴļ(.C)һģ鶨ļ(.DEF)ԴļDLLеĺ.DEFļгЩDLLԡʹ.CļĿļ(.OBJ).DEFļһļ(.EXP)һļ(.LIB)ӳʹ.OBJ.EXP.LIBļDLLĿļ.OBJ.EXPļṩӳҪ.DLLļϢ.LIBļӳҪDLLⲿõַϢʱҵָDLL
2VBDLL
VBִͨDLLԵWindowsĸֺԼԱԼӲ豸DLL豸Լĺһ㡣VBDLLУVisual BasicDeclareҪʹõDLLִ̻DLLӳ
ĿǰPCʽҪISA(16bits)PCI(32bits)VESA(32bits)ISAһοԶд16bits(a word)PCIVESAһοԶд32bits(double word)ֱ16bits32bits WindowsϵͳУVCԱдDLLԼVBDLLĵá
316λϵͳû̬ӿĿ
3.1ʹVisual C++1.52д16λDLL
(1)DLLCԴļ(DLLʵֲ)
//PORT16.CPP
#include <windows.h>
#include <conio.h>
//ʹDLLɱCԵãز趨Ϊȫֱ
//ЩISA PCI VESA
extern"C"
{ int _export WINAPI Inportb(int port);//input a byte 
void _export WINAPI Outportb(int port,int data);//output a byte
int _export WINAPI Inportw(int port);//input a word
void _export WINAPI Outportw(int port,int data);//output a word
}
//DLL׼ڵ
int FAR PASCAL LibMain(HINSTANCE hInstance,WORD wDataSeg,WORD wHeapsize,LPSTR lpszCmdLine)
{ if(wHeapsize!=0)UnlockData(0);return 1;}
//DLL׼ڵ
int FAR PASCAL _export WEP(int nParam)
{ return 1;}
//˿뺯(byte)
int _export WINAPI Inportb(int port)
{ return _inp(port);}
//˿(byte)
void _export WINAPI Outportb(int port,int data)
{ _outp(port,data);}
int _export WINAPI Inportw(int port)//input a word
{ return _inpw(port);}
void _export WINAPI Outportw(int port,int data)//output a word
{ _outpw(port,data);}
(2)DLLģ鶨ļ(DLL)
//PORT16.DEF
LIBRARYPORT16
EXETYPEWINDOWS
CODEPRELOADMOVEABLEDISCARDABLE
DATAPRELOADMOVEABLE
HEAPSIZE4096
EXPORTS
(3)ɶ̬ӿport16.dll
VC++1.52DLLͨWindowsӦóƣProject Type(ļ)ѡ Windows Dynamic-Link Library(.DLL)͡ProjectļΪPORT16PORT16.CPPPORT16.DEFļ
3.2VB4.0-16BitDLL
ͨԴӡ˿378Hжд(дͶӦͬ),˵VBDLL̺ͷнڴвʾӶ˿ڶ0-255
 '(General)DLL̻
Private Declare Sub Outportb Lib "port16.dll"(ByVal port As Integer,ByVal data As Integer)
Private Declare Function Inportb Lib"port16.dll"(ByVal port As Integer)As Byte
Private Declare  Sub Outportw Lib"port16.dll"(ByVal port As Integer,ByVal data As Integer)
Private Declare Function Inportw Lib"port16.dll"(ByVal port As Integer)As Integer
Dim data As Integer
 'Formгʼdata
Private Sub Form_Load()
data=0
End Sub
 'һʱؼLabelؼ
Private Sub Timer1_Timer()
Call Outportb(&H378,data) 'port16.dllеOutportb( ),
Label1.Caption=Inportb(&H378) 'port16.dllеInportb( ),ݲʾ
data=data+1
If data>255 Then data=0
End Sub
432λϵͳû̬ӿĿ
4.1ʹVisual C++2.0д32λDLL
(1) DLLCԴļ
32λϵͳбдDLL16λйͬWIN32DLLҪLibMainWEP,֮DLLMain,رDLLݹн̹,Ҳ˵һе̶߳ɷЩݣʹñдDLLˡΪһǵӦóļһ̣Ҷ˿ڶд漰ݹҪдDLLMain
//PORT32.CPP
#include <windows.h>
#include <conio.h>
//Inportd()Outportd()PCIVESA
extern"C"
{LONG FAR PASCAL Inportb(LONG port);//input a byte
LONG FAR PASCAL Inportw(LONG port);//input a word
LONG FAR PASCAL Inportd(LONG port);//input a double word
VOID FAR PASCAL Outportb(LONG port,LONG data);//output a byte 
VOID FAR PASCAL Outportw(LONG port,LONG data);//output a word
VOID FAR PASCAL Outportd(LONG port,LONG data);//output a double word
}
LONG FAR PASCAL Inportb(LONG port)//input a byte
{return _inp((int)port);}
LONG FAR PASCAL  Inportw(LONG port)//input a word
{return _inpw((int)port);}
LONG FAR PASCAL Inportd(LONG port)//input a double word
{return_inpd((int)port);}
VOID FAR PASCAL Outportb(LONG port,LONG data)//output a byte
{_outp((int)port,data);}
VOID FAR PASCAL Outportw(LONG port,LONG data)//output a word
{_outpw((int)port,(int)data);}
VOID FAR PASCAL Outportd(LONG port,LONG data)//output a double word
{_outpd((int)port,data);}
(2) DLLģ鶨ļ 
//PORT32.DEF
LIBRARY PORT32
EXETYPE WINDOWS
CODEPRELOADMOVEABLE DISCARDABLE
DATAPRELOADMOVEABLE
EXPORTS
Inportb
Outportb
Inportw
Outportw
Inportd
Outportd
(3)ɶ̬ӿport32.dll(16λϵͳɷͬ)
4.2VB4.0-32BitVB5.0DLL
'(General)DLL̻
Private Declare Sub Outportb Lib"port32.dll"(ByVal port As Integer,ByVal data As Integer)
Private Declare Function Inportb Lib"port32.dll"(ByVal port As Integer)As Byte
Private Declare Sub Outportw Lib"port32.dll"(ByVal port As Integer,ByVal data As Integer)
Private Declare Function Inportw Lib"port32.dll"(ByVal port As Integer)As Integer
Private Declare Sub Outportd Lib"port32.dll"(ByVal port As Integer,ByVal data As Long)
Private Declare Function Inportd Lib"port32.dll"(ByVal port As Integer)As Long
Ժʹ÷ͬ16Bitһ()
߼飺ˮ У36꣬ʦз򣺵ϵͳԶӲ
ߵλ人ˮѧ 人 430072
ο
1֣컪.Visual Basic 3.0 for Windowsָ. ӿƼѧ, 1995
2ŵƳ.VFPDLLʵӲ˿ڿ.΢,1997-03
ոڣ1998-09-07
