操作系统的完整代码——YCOS驱动程序

YCOS驱动程序

    YCOS是微内核的,它的大部分功能可以由外部驱动程序提供,只有少数几个功能由内核文件ycker.cpp提供。
    下面介绍如何编写、安装、调用驱动程序。

1. 设计驱动程序
#define YCORG -1          //该设置使编译器生成头部为YCEXE结构的可执行文件
#include "ycio.cpp"       //提供前缀为ycio_的函数和C/C++标准函数

struct drvCLASS     //本驱动程序的接口结构及其实现
{
        virtual char *stdcall nice_day(int aa)
          {
              printf("%d\n",aa);
              return "It is a nice day!";
          }
        virtual char *stdcall hello()
          {
              return "Hello wordl!";
          }
};

drvCLASS DRV;   //声明接口对象
void main(void **pObj)
{
    *pObj = &DRV;   //将接口对象DRV的地址传给调用者
}

                        C/C++代码文件 drv.cpp

2. 安装驱动程序
#define YCORG -1          //该设置使编译器生成头部为YCEXE结构的可执行文件
#include "ycio.cpp"

void main()
{
    ycfsCLASS *YCFS;
    ycio_get_driver(&YCFS,"ycfs");  //获取文件管理接口指针
    byte *codeptr = YCFS->get_file_code("drv.sys");  //获得drv.sys文件的代码
    int rv = ycio_set_driver(codeptr, "nice");  //安装驱动程序(取名为nice)到内核
    if(rv == 0)   printf("\ndrv.sys installed.\n");   //安装成功
    else          printf("\ndrv.sys: has been installed before! - %d\n",rv);
}

                        C/C++代码文件 dra.cpp

    在YCOS提示符下,键入dra后,驱动程序便被安装到内核了,应用程序就可以通过名字
“nice”获得驱动程序drv.cpp的函数接口指针。

3. 调用驱动程序
#define YCORG -1          //该设置使编译器生成头部为YCEXE结构的可执行文件
#include "ycio.cpp"

struct drvCLASS   //drv.cpp的接口结构
{
        virtual char *stdcall nice_day(int aa);
        virtual char *stdcall hello();
};

void main()
{
    drvCLASS  *DRV;
    ycio_get_driver(&DRV,"nice");  //通过名字nice获得驱动程序接口指针
    if(!DRV)
      {
        printf("\nError: drv.sys not installed! Run dra.exe first.\n");
        return;
      }
    printf("\nDriver: drv.sys\n");
    printf("%s\n",DRV->nice_day(2019));  //调用驱动程序drv.cpp的nice_day()函数
    printf("%s\n",DRV->hello());         //调用驱动程序drv.cpp的hello()函数
}

                        C/C++代码文件 drb.cpp

在YCOS提示符下,键入drb,将显示下列文字:
    Error: drv.sys not installed! Run dra.exe first.

    2019
    It is a nice day!
    Hello wordl!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值