当前位置:编程学习 > C/C++ >>

ioperm - Windows下设置端口权限

Windows下设置端口权限的系统调用有两个:ioperm和iopl函数
1.ioperm函数
     功能描述:
为调用进程设置I/O端口访问权能。ioperm的使用需要具有超级用户的权限,只有低端的[0-0x3ff] I/O端口可被设置,要想指定更多端口的权能,可使用iopl函数。这一调用只可用于i386平台。

用法:
[cpp]
#include  <ioperm.h>/* for libc5 */  
#include  <sys/io.h>/* for glibc */  
int ioperm(unsigned long from, unsigned long num, int turn_on); 

#include  <ioperm.h>/* for libc5 */
#include  <sys/io.h>/* for glibc */
int ioperm(unsigned long from, unsigned long num, int turn_on);参数:
from:起始端口地址。
num:需要修改权能的端口数。
turn_on:端口的新权能位。1为开启,0为关闭。
返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EINVAL:参数无效
EIO:这一调用不被支持
EPERM:调用进程权能不足。
  
2.iopl函数

功能描述:该调用用于修改当前进程的操作端口的权限。可以用于所有65536个端口的权限。因此,ioperm相当于该调用的子集。和ioperm一样,这一调用仅适用于i386平台。

用法:
[cpp]
#include <ioperm.h>  
int iopl(int level); 

#include <ioperm.h>
int iopl(int level);参数:
level: 端口的权限级别。为3时可以读写端口。默认权能级别为0,用户空间不可读写。
返回说明:成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EINVAL:level值大于3
ENOSYS:未实现该调用
EPERM:调用进程权能不足。

目前已经将主要的部分更改成支持GCC编译的内容。支持GCC内联汇编。
代码如下(默认用Windows的C编译器):
[cpp]
/*
* function: read cmos data using rtc port(0x70 -- port, 0x71 -- data)
*/ 
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <windows.h>  
 
extern int install( int  ); 
extern int uninstall(int ); 
extern ioperm( unsigned long from, unsigned long num, int turn_on ); 
 
#ifdef _GCC  
void outp(unsigned short addr, unsigned short data){ 
    __asm__ ( 
        "mov %1,   %%al;" 
        "mov %0,   %%dx;" 
        "out %%al, %%dx;" 
        : /* no output */ 
            :"m" (addr), "m" (data) /* input addr and data */ 
            :"ax", "dx" 
    ); 

unsigned char inp( unsigned short addr) 

    unsigned char cha; 
    __asm__ ( 
            "mov %1,   %%dx;" 
        "in  %%dx, %%al;" 
        "mov %%al, %0;" 
        :"=r" (cha) /* output to var(cha) */ 
        :"m" (addr) /* input addr */ 
        : "ax", "dx" 
    ); 
    return cha; 

#else /* using windows asm */  
void outp(unsigned short addr , unsigned short data){ 
    asm { 
        mov al,data 
        mov dx,addr 
        out dx,al 
    } 

unsigned char inp( unsigned short addr) 

    unsigned char cha; 
    asm{ 
        mov dx,addr 
        in al,dx 
        mov cha ,al 
    } 
    return cha; 

 
#endif  
 
int main( void ) 

    int i; 
    unsigned char c; 
    install(0); 
    Sleep(500); 
    if (ioperm(0x70, 2, 1 )) { 
        fprintf( stderr, "Error: ioperm() failed. Please install ioperm.sys driver.\n" ); 
    }else{ 
            for (i=0; i<260; i++) 
            { 
        //  Sleep(100);  
                    outp(0x70, i); 
            c = inp(0x71); 
                    printf("%02x ", c); 
            if ((i+1)%20 == 0) 
                printf("\n");     
            } 
    } 
    uninstall(0); 
    return 0; 

/*
* function: read cmos data using rtc port(0x70 -- port, 0x71 -- data)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

extern int install( int  );
extern int uninstall(int );
extern ioperm( unsigned long from, unsigned long num, int turn_on );

#ifdef _GCC
void outp(unsigned short addr, unsigned short data){
 __asm__ (
  "mov %1,   %%al;"
  "mov %0,   %%dx;"
  "out %%al, %%dx;"
  : /* no output */
         :"m" (addr), "m" (data) /* input addr and data */
         :"ax", "dx"
 );
}
unsigned char inp( unsigned short addr)
{
 unsigned char cha;
 __asm__ (
      "mov %1,   %%dx;"
  "in  %%dx, %%al;"
  "mov %%al, %0;"
  :"=r" (cha) /* output to var(cha) */
  :"m" (addr) /* input addr */
  : "ax", "dx"
 );
 return cha;
}
#else /* using windows asm */
void outp(unsigned short addr , unsigned short data){
 asm {
  mov al,data
  mov dx,addr
  out dx,al
 }
}
unsigned char inp( unsigned short addr)
{
 unsigned char cha;
 asm{
 &nbs

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,