如何在不使用STL或中断的情况下创建文件 [英] How to create a file WITHOUT using STL nor Interrupts

查看:71
本文介绍了如何在不使用STL或中断的情况下创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这看起来很奇怪;但是我想创建一个文件而不使用任何API和库,例如STL或DOS中断.实际上,我将要知道黑匣子内部发生了什么.任何线索对我来说都是非常合作的.我本人认为这应该与处理设备驱动程序或其他东西有关.

更清楚地说,我想知道thouse API(例如CreateFile())如何与设备驱动程序通信.有没有标准化的方法?此外,设备驱动程序如何处理其硬件?是否为他们指定了一部分内存?以及此类问题...

I know this looks strange; but I want to create a file without using any APIs and libraries like STL or DOS interrupts. In fact I''m going to know what''s going on inside that black boxes. ANY CLUE can be quite cooperative for me. I myself guess it should be kind of linked to dealing with Device Driver or something.

To be more clear, I want to know how thouse APIs (e.g. CreateFile()) communicate with device drivers. Is there a standardized way? Furthermore, how device drivers deal with their hardwares? Is a certain portion of memory specified for them? and such questions ...

推荐答案

约瑟夫,

如果我正确理解了您的问题,那么您主要是好奇的并且想了解操作系统如何与硬件交互(使用驱动程序).

我数十年来一直没有使用硬件或直接驱动程序访问,但是如果您使用Windows,我建议从这里开始:

Windows硬件开发人员中心 [
Hi Joseph,

If I understand your question correctly, you''re mainly curious and want insight in how the OS interacts with hardware (using drivers).

I haven''t worked with hardware or direct driver access for decades, but I would suggest starting here, if you''re into Windows:

Windows Hardware Developer Central[^]

There is plenty of information on how drivers should be designed, including how they can interact with hardware (interrupt handlers, direct I/O, adapter memory access and so on), and how the OS interacts with the drivers (driver API).

I very much doubt (though I may be wrong) that you can interact directly with drivers from a normal user-mode program, at least not without going through Windows APIs (I think there are API:s to query drivers for status and so on). I suspect they''re protected so that only the OS (or other kernel-mode software) has access to them - this is the case in all operating systems I have worked with.

I hope this helped.


您提到不想使用任何API,我将建议CreateFile [ [
You mentioned not wanting to use any APIs and I''m going to suggest CreateFile[^] anyway, but because I think it''s used in a way that is acceptable for you :)

CreateFile can be used to create a file, but it can also be used to directly access sectors on a disk. I learned how to do this a while ago from this[^] article.

You can open the floppy disk for example with
hFloppyDisk = CreateFile("\\\\.\\A:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);


然后,您可以像这样访问所需的扇区:


You can then access the required sector like so:

SetFilePointer(hFloppyDisk,512*(18*(cylinder*2+head)+(sector-1)),0,FILE_BEGIN);


然后这样写:


And then write like so:

WriteFile(hFloppyDisk,buffer, 512,&count,NULL);



现在,您可以访问原始磁盘数据,如果知道文件系统如何存储文件,就可以创建文件.
请注意,Windows可能不允许您访问您感兴趣的磁盘.例如,安装Windows的C:\驱动器很可能会导致访问被拒绝错误.



Now you have access to the raw disk data, you can create files if you have the knowledge how the file system stores it.
Do note that Windows might not allow you access to the disk you''re interested in; for example your C:\ drive where Windows is installed is most likely going to result in an access denied error.


您是否有机会编写OS或驱动程序?
Are you by any chance writing a OS or a driver?


这篇关于如何在不使用STL或中断的情况下创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆