The winio library allows 32-bit Windows applications to directly access the I/O port and physical memory. By using a kernel-mode device drive and several other underlying programming skills, it bypasses the protection mechanism of the Windows system.
In winnt/2000/XP, The winio function library can only be called by applications with administrative permissions. If the user does not enter as a manager, winio. dll cannot be installed or activated. This restriction can be overcome by installing the drive software under the Administrator's permission. However, in this case, the shutdownwinio function cannot be called before the application ends because it deletes the winio driver from the system registry.
The function library provides eight function calls:
Bool _ stdcall initializewinio ();
This function initializes the wioio function library.
You must call this function before calling all other functions.
If the function is successfully called, the returned value is a non-zero value.
If the call fails, the return value is 0.
Void _ stdcall shutdownwinio ();
This function clears the winio library in the memory.
This function must be called before suspending the application function or when the winio library is no longer needed,
Bool _ stdcall getportval (word wportaddr, pdword pdwportval, byte bsize );
Use this function to read one byte/word/double-word data from an input or output port.
Parameters:
Wportaddr-input/output port address
Pdwportval-pointer to a two-character variable to receive data from the port.
Bsize-number of bytes to be read, which can be 1 (byte), 2 (Word) or 4 (DWORD ).
If the call is successful, a non-zero value is returned.
If the function call fails, the return value is zero.
Bool _ stdcall setportval (word wportaddr, DWORD dwportval, byte bsize );
Use this function to write data of one byte/word/double character to the input or output interface.
Parameters:
Wportaddr-input/output port address
Dwportval-data to be written to the portal
Bsize-number of data to be written, which can be 1 (byte), 2 (Word) or 4 (DWORD ).
If the call is successful, a non-zero value is returned.
If the function call fails, the return value is zero.
Pbyte _ stdcall mapphystolin (pbyte pbphysaddr, DWORD dwphyssize, handle * pphysicalmemoryhandle)
Use this function to map a portion of the physical memory to the linear address space of a 32-bit application.
The following is an example:
Pbyte pblinaddr;
Handle physicalmemoryhandle;
Pblinaddr = mapphystolin (0xa0000, 65536, & physicalmemoryhandle );
This function maps the address space with the physical address range of 0xa0000-0xaffff to the linear address space corresponding to the application. The return value is a linear address related to the physical address 0xa0000. If an error occurs, the return value is null.
Parameters:
Pbphysaddr-pointer to physical address
Dwphyssize-number of bytes to be mapped
Pphysicalmemoryhandle-variable pointer. If the call is successful, it receives the physical memory handle. Then, this handle serves as its first parameter when calling the unmapphysicalmemory function.
Bool _ stdcall unmapphysicalmemory (handle physicalmemoryhandle, pbyte
Pblinaddr)
This function is used to remove a linear physical memory area mapped using the mapphystolin function. This area is mapped to the linear address space of the application.
Windows 9x applications do not need to call this function.
Parameters:
Physicalmemoryhandle-handle of the physical memory area. This parameter is returned by calling the mapphystolin function.
The linear address returned by the pblinaddr-mapphystolin function call.
Bool _ stdcall getphyslong (pbyte pbphysaddr, pdword pdwphysval );
Reads a dual-word data from a specified physical address.
Parameters:
Pbphysaddr-pointer to the physical address.
Pdwphysval-a pointer to a dual variable that receives data from the physical memory.
If this function is successfully called, a non-zero value is returned.
If the function call fails, a zero value is returned.
Bool _ stdcall setphyslong (pbyte pbphysaddr, DWORD dwphysval );
Write a double-font data to the specified physical address.
Parameters:
Pbphysaddr-pointer to the physical address.
Pdwphysval-specify the dual-font data to be written to the physical memory address.
If this function is successfully called, a non-zero value is returned.
If the function call fails, a zero value is returned.
The Code implements the winio virtual implementation button.
# Include "stdafx. H "# include" winio analog buttons. H "# include" winio analog button DLG. H "# include" winio. H "# ifdef _ debug # define new debug_new # UNDEF this_filestatic char this_file [] = _ file __; # endif ////////////////////////////////////// //////////////////////////////////////// /caboutdlg Dialog used for app aboutclass caboutdlg: public cdialog {public: caboutdlg (); // dialog data // {afx_data (caboutdlg) Enum {IDD = idd_about Box}; //} afx_data // classwizard generated virtual function overrides // {afx_virtual (caboutdlg) protected: Virtual void dodataexchange (cdataexchange * PDX ); // DDX/DDV support //} afx_virtual // implementationprotected: // {afx_msg (caboutdlg) //} afx_msgdeclare_message_map ()}; caboutdlg: caboutdlg (): cdialog (caboutdlg: IDD) {// {afx_data_init (caboutdlg) //} afx_data_init} void caboutdlg: dodataexchange (cdat Aexchange * PDX) {cdialog: dodataexchange (PDX); // {afx_data_map (caboutdlg) //} afx_data_map} begin_message_map (caboutdlg, cdialog) // {afx_msg_map (caboutdlg) // No message handlers //} afx_msg_mapend_message_map () //////////////////////////////////////// /// // cwiniodlg dialogcwiniodlg:: cwiniodlg (cwnd * pparent/* = NULL */): cdialog (cwiniodlg: IDD, pparent) {// {afx_data_init (Cwiniodlg) // Note: The classwizard will add member initialization here //} afx_data_init // note that loadicon does not require a subsequent destroyicon in win32m_hicon = afxgetapp () -> loadicon (idr_mainframe);} void cwiniodlg: dodataexchange (cdataexchange * PDX) {cdialog: dodataexchange (PDX); // {afx_data_map (cwiniodlg) // note: the classwizard will add DDX and DDV cballs here //} afx_data_map} begin_message _ Map (outputs, cdialog) // {afx_msg_map (outputs) values () on_wm_paint () values (idc_button1, onbutton1) values (idc_button2, onbutton2) values (idc_button3, onbutton3) on_bn_clicked (idc_button4, onbutton4) on_wm_destroy () on_wm_close () //} afx_msg_mapend_message_map () //////////////////////////////////////// /// // cwiniodlg Message handlersbool cwiniodlg: oninitdialog () {cdialog: oninitdialog (); // Add "about... "menu item to system menu. // idm_aboutbox must be in the system command range. assert (idm_aboutbox & 0xfff0) = idm_aboutbox); Assert (idm_aboutbox <0xf000); cmenu * psysmenu = getsystemmenu (false); If (psysmenu! = NULL) {cstring straboutmenu; straboutmenu. loadstring (ids_aboutbox); If (! Straboutmenu. isempty () {psysmenu-> appendmenu (mf_separator); psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu); }}// set the icon for this dialog. the framework does this automatically // when the application's main window is not a dialogseticon (m_hicon, true); // set big iconseticon (m_hicon, false ); // set small icon // todo: add extra initialization here // initialize the winio library initializewinio (); Return true; // return true unless you set the focus to a control} void cwiniodlg: onsyscommand (uint NID, lparam) {If (NID & 0xfff0) = idm_aboutbox) {caboutdlg dlgabout; dlgabout. domodal ();} else {cdialog: onsyscommand (NID, lparam) ;}// if you add a Minimize button to your dialog, you will need the code below // to draw the icon. for MFC applications using the document/view model, // This is automatically done for you by the framework. void cwiniodlg: onpaint () {If (isiconic () {cpaintdc DC (this); // device context for paintingsendmessage (wm_iconerasebkgnd, (wparam) DC. getsafehdc (), 0); // center icon in client rectangleint cxicon = getsystemmetrics (sm_cxicon); int cyicon = getsystemmetrics (sm_cyicon); crect rect; getclientrect (& rect ); int x = (rect. width ()-cxicon + 1)/2; int y = (rect. height ()-cyicon + 1)/2; // draw the icondc. drawicon (X, Y, m_hicon);} else {cdialog: onpaint ();}} // The system callthis to obtain the cursor to display while the user drags // The minimized window. hcursor cwiniodlg: onquerydragicon () {return (hcursor) m_hicon;} void cwiniodlg: onok () {// todo: add extra validation herecdialog: onok ();} void kbcwait4ibe () {DWORD dwregval = 0; do {getportval (0x64, & dwregval, 1) ;}while (dwregval & 0x2 );} void mykeydown (DWORD kcode) {sleep (2000); // rest for 2 seconds kbcwait4ibe (); // wait for the keyboard buffer to be empty setportval (0x64, 0xd2, 1 ); // send the keyboard write command sleep (10); kbcwait4ibe (); setportval (0x60, mapvirtualkey (kcode, 0), 1); // write the key information, press key} void mykeyup (DWORD kcode) {sleep (10); kbcwait4ibe (); // wait for the keyboard buffer to be empty setportval (0x64, 0xd2, 1 ); // send the keyboard write command sleep (10); kbcwait4ibe (); setportval (0x60, (mapvirtualkey (kcode, 0) | 0x80), 1 ); // write key information, release key} void cwiniodlg: onbutton1 () {// todo: add your control notification handler code heremykeydown (vk_f1); // press f1mykeyup (vk_f1 ); // release F1} void cwiniodlg: onbutton2 () {// todo: add your control notification handler code heremykeydown (vk_back); // press the backend mykeyup (vk_back ); // release backspace} void cwiniodlg: onbutton3 () {// todo: add your control notification handler code heremykeydown (65); // press the key mykeyup (65 ); // release key a} void cwiniodlg: onbutton4 () {// todo: add your control notification handler code heremykeydown (66); // press Key B mykeyup (66 ); // release key B} void cwiniodlg: onclose () {// todo: add your message handler code here and/or call defaultshutdownwinio (); cdialog: onclose ();}