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!