FreeBasic Compilation Driver Instructions

General FreeBASIC programming questions.
Post Reply
MOODSKY2002
Posts: 10
Joined: May 06, 2022 12:43

FreeBasic Compilation Driver Instructions

Post by MOODSKY2002 »

The article is automatically translated by the machine, there may be some grammar problems, I hope everyone can understand the meaning correctly
Moodsky2002 from China

When you use FreeBasic, you should have noticed that there is a driver.bas in the example, but how this thing is compiled into .sys is rarely mentioned on the Internet. Running that make.bat directly will report a bunch of errors and it will not work at all.
After the author spent more than N hours stepping on numerous pits, consulting various materials, and finding ways, finally successfully compiled the example driver with FreeBasic and ran it normally. The experience is now organized as follows:
[1]: missing files excpt.h sdkddkver.h, install MSYS2 and translate these two files into .bi with fbfrog-master

[2]: ntdef.bi winddk.bi a bunch of errors, the most is the declaration and winnt.bi duplicate, add comments to solve. A small number of original bi file type declarations have problems, please fix them yourself, and there are some types that are not defined, check MSDN and make them up.

[3]: At this time, the .o file can be successfully compiled with FBC! The next step is to compile it into a driver. When the ld.exe of FB is compiled, it will always report that DriverEntry cannot be found. At this time, copy a ld.exe from MSYS2 and modify the file name to avoid repetition (the following example is ld64.exe).
ld64.exe DRIVER.o --subsystem=native --image-base=0x10000 --entry=DriverEntry@8 --nostdlib -shared -L "change the path to \lib\win64" -l ntoskrnl -o "DRIVER.sys "
The fourth level: Our driver has been successfully generated at this time, and it can also start, stop and output debugging information to DebugView normally.

renderings: img-blog.csdnimg.cn/c9dd9164ff7041cfb547fbb22ed4a3ce.png
Chinese original: blog.csdn.net/MOODSKY2002/article/details/124444903

The BI file used to compile the driver, but Winddk.bi needs to be supplemented and updated continuously, and now it can only be used to verify the driver generation!

excpt.bi/ntdef.bi/winnt.bi/winddk.bi
https://1drv.ms/u/s!AsWHQO8XUbldhmQVJML ... U?e=RMOIbf

I need some simple lightweight driver work due to some work needs, so I want to use FB to complete the driver. After all, most people don't need very professional driver development. I hope that friends can participate, and everyone can supplement and update BI. In the future, the drivers in small and medium-sized applications can also be completed by FB, which is great!
Last edited by MOODSKY2002 on May 08, 2022 3:37, edited 3 times in total.
MOODSKY2002
Posts: 10
Joined: May 06, 2022 12:43

Re: FreeBasic Compilation Driver Instructions

Post by MOODSKY2002 »

In addition, the ld.exe of FB-1.09-GCC9.3 can directly generate .sys
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: FreeBasic Compilation Driver Instructions

Post by TeeEmCee »

Very nice!
MOODSKY2002 wrote: May 07, 2022 2:47 [2]: ntdef.bi winddk.bi a bunch of errors, the most is the declaration and winnt.bi duplicate, add comments to solve. A small number of original bi file type declarations have problems, please fix them yourself, and there are some types that are not defined, check MSDN and make them up.
Wow, win/ntdef.bi seems to be completely broken, including non-existent files and clashing with winnt.bi. So noone uses it? If it's so broken maybe we can throw it away and retranslate it from scratch with fbfrog (likely to be incompatible with existing version due to different name mangling).

On GitHub_IRP Statement bugfix (Issue #366)
adeyblue
Posts: 300
Joined: Nov 07, 2019 20:08

Re: FreeBasic Compilation Driver Instructions

Post by adeyblue »

I don't doubt the neat technical feat of making this work but...
TeeEmCee wrote: May 07, 2022 10:27 On GitHub_IRP Statement bugfix (Issue #366)
github wrote: Regardless, I used fbfrog to translate the C++ definition I linked to. It only took me a couple minutes (I had to make a couple small edits like removing __volatile),
This is why this isn't the greatest idea. Having to remove things that the C compilers use to generate the correct code because FB doesn't understand or have an equivalent just means you've punted a compile time problem to a runtime one. Especially for things like spinlocks. And since you won't be able to easily debug it when it doesn't work right since WinDbg can't read FB's debug info... Plus the fact it seems half of the fundamental Types in the headers are outdated and wrong or broken, doing anything useful seems like a job of hassle and admin.

If you're just messing around for fun then there's no real problem other the amount of time investment doing all that requires. If you're doing something serious that other people will use, using the normal driver tools will give you a much better experience all around.
github wrote: There is no excpt.bi file. Does anyone know why it's missing?
Probably because it's part of the compiler/CRT header set, not the SDK header set. If someone only translated the w32api mingw package (or whateever it was called) then it would be missing, MinGW's (and MSVC's) lives in the same directory as stdlib.h not windows.h
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: FreeBasic Compilation Driver Instructions

Post by TeeEmCee »

I agree that it's not very practical.

Maybe FB will gain an equivalent to "volatile" some day, but I believe that when using the gas emitter all variables are effectively already treated as volatile due to lack of persistent register allocation.
Probably because it's part of the compiler/CRT header set, not the SDK header set.
Thanks.
Post Reply