0

I'm new to Win environment, trying to write code which use win kernel API but on Ubuntu 10.04 ... using mingw32 (more specifically, i586-mingw32msvc)

My code:

#include<ddk/winddk.h>
int main()
{
        return 0;
}

But I'm getting to many errors with ddk/winddk.h header: Compiling as: i586-mingw32msvc-gcc WaitForSingleObj_2.c

All errors are like this:

/usr/lib/gcc/i586-mingw32msvc/4.2.1-sjlj/../../../../i586-mingw32msvc/include/ddk/winddk.h:9208: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'NTSTATUS'

Am I missing something for this? As I tried to compile with headers iostream and windows.h, that works fine.

EDIT:

#include<ntdef.h>
#include<ddk/ntddk.h>
#include<ddk/winddk.h>
#include<ddk/ntddndis.h>

int main()
{
        KTIMER *timer;
        KeInitializeTimer(timer);
        return 0;
}

till KTIMER its fine, now new error: /tmp/cc0epAQA.o:WaitForSingleObj_2.c:(.text+0x1d): undefined reference to `_imp_KeInitializeTimer@4' collect2: ld returned 1 exit status

Can anyone tell me, now which header file to include plz?

Edit: [As others commented already, and I experienced(!) PLEASE try to avoid using other platform for Windonws Kernel development]

I switched to Visual C++ Express, downloaded WDK, given additional include path from Project Property->c/c++ .. ->Additional Include directory (C:\WinDDK\7600.16385.1\inc\ddk;C:\WinDDK\7600.16385.1\inc\api;)

BUT (I hate this but!) I'm getting many compiling error like

    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\crtdefs.h(543): error C2143: syntax error : missing ')' before 'const'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\crtdefs.h(543): error C2143: syntax error : missing '{' before 'const'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\crtdefs.h(543): error C2059: syntax error : ','
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\crtdefs.h(543): error C2059: syntax error : ')'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\crtdefs.h(550): error C2143: syntax error : missing ')' before 'const'

My Code:

#include <wdm.h>
int mian()
{
    /*
     * PKTIMER Timer;
     */
}

If anyone has idea please help!

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
code muncher
  • 1,592
  • 2
  • 27
  • 46
  • Are you sure that's the top level header file? Which functions and types do you need? Is that the right header file to include? – David Heffernan May 04 '12 at 22:49
  • Actually I'm afraid whatever you are trying to do won't work, because your include files are obviously mixing usermode and kernelmode functionality which will simply never work. Even though the kernel has a subset it will for example never provide anything that allows you to use `` ... if you mean you want to use the usermode part of the native API you should correct that in your question as that is a whole different thing and a fragile thing whenever attempting to use some more complex WDK header files. Simple ones will work, more complex ones usually need some extra care. – 0xC0000022L May 04 '12 at 23:42
  • @STATUS_ACCESS_DENIED right, I should not include iostream ... let me edit that ...but do you know why winddk.h is giving me trouble? – code muncher May 05 '12 at 00:08
  • 2
    Well, `Windows.h` does also not make sense in kernelmode code. Besides, even if you don't like to hear it, you should avoid writing drivers with non-WDK compilers. Use WINE if you need. Either way, the top level headers depend on your driver model and are either `ntddk.h` or `wdm.h` ... – 0xC0000022L May 05 '12 at 00:10
  • @DavidHeffernan I checked in dir /usr/i586-mingw32msvc/ with "cscope" ... I need KTIMER, and its there in winddk.h – code muncher May 05 '12 at 00:11
  • @STATUS_ACCESS_DENIED right I'm using WINE ... but I want to work with Linux .. – code muncher May 05 '12 at 00:20
  • @STATUS_ACCESS_DENIED wdm.h is not there and now I tried including ddk/ntddk.h and ddk/winddk.h .. still not working – code muncher May 05 '12 at 00:26
  • Well, you should know your tools. Developing for KM on Windows (as for many other platforms) is no kid's play. If your UM app crashes, the app dies. If your driver crashes, the system dies. **Please** reconsider using unsupported tools for creating Windows KM drivers. Not to mention that your idea of drivers seems to be non-existent from the code snippet above. It **is completely different from UM** and should not be taken lightly. – 0xC0000022L May 05 '12 at 00:29
  • Where do these come from? Let me guess, they are some MinGW-specific headers? In this case you're likely on your own, sorry. I thought we were talking about the official headers here. – 0xC0000022L May 05 '12 at 00:31
  • @STATUS_ACCESS_DENIED you're right, its from MinGW .. official header is Wdm.h as you said .. I saw on Windows development site too .. well actually I wanted to try on Linux m/c only so ... and I agree its completely different from UM ... will see if nothing works then may be I'll switch to VS ... – code muncher May 05 '12 at 00:40
  • 2
    I would use Microsoft's toolchain to do this, there's too many things that can go wrong otherwise in cross-compilation. VmWare Player is free so you can still develop "under" Linux, though you'll proably need licence for Windows itself. VS Express is also free but you'll have to confirm it can do the device driver stuff. – paxdiablo May 05 '12 at 00:43
  • 2
    @Dhara Darji: VS is not needed. What's needed is the WDK build environment, that is completely independent from VS and the VS compilers (used to be dependent until the XP DDK, will probably become dependent again with VS 11). They are all command line tools and should, for all practical purposes, work in WINE. Meaning you can still use your favorite IDE/editor on Linux ... – 0xC0000022L May 05 '12 at 00:43
  • @STATUS_ACCESS_DENIED ohh I didn't know that .. I'm certainly using Wine to 'execute' a.exe, but not sure how to use with this kind of prob, could you please give me some example? Thanks! – code muncher May 05 '12 at 00:58
  • anyone know how to work with wine, to build win-kernel ... install mingw32 (sudo apt-get install mingw32) .. installed wine too .. but no idea how to build this code ... ! – code muncher May 09 '12 at 02:38
  • 3
    Your plan is almost guaranteed to fail for the reasons @STATUS_ACCESS_DENIED and paxdiablo have explained. I would recommend poking yourself in the eye with a sharp stick instead of trying to develop Windows KM drivers with unsupported tools. Oh, and your odds of getting your driver certified by MS? Virtually zero. – Carey Gregory May 15 '12 at 00:25
  • @CareyGregory agree, I edited my question now to make it really clear .. – code muncher May 15 '12 at 00:41
  • @Dhara, it's not usually a good idea to change the basic nature of a question since it invalidates answers to date. Modifying a question to provide further details or making minor mods is usually considered okay but changing it in such a way that it's a totally new question (such as moving from MinGW to VC++) is enough reason for a new question to be asked. Otherwise the whole Q&A concept breaks down. – paxdiablo May 15 '12 at 01:09

2 Answers2

3

Are you sure that ntdef.h is being included, either explicitly or by certain #define values being set? That's the header that defines NTSTATUS and that error message is the one gcc normally spits out when a symbol/type has not been defined.

In any case, developing Windows device drivers using a non-MS compiler will almost certainly have more problems than with Microsoft's own toolchain.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I tried including ... getting same errors, And error messages are not just for NTSTATUS, its for 'void', 'PBOOLEAN', 'ULONG' etc!! – code muncher May 05 '12 at 00:03
  • @DharaDarji: paxdiablo is right. Trying to develop Windows device drivers using tools other than the Windows DDK will almost certainly fail or at least be extremely difficult. It's not even easy, or sometimes even possible, to develop drivers using the Visual Studio compilers (the WDK compilers recognize options VS doesn't). Device drivers are usually operating system specific things. Trying to use tools from another OS is a poor choice on any platform. – Carey Gregory May 15 '12 at 00:22
  • 1
    @CareyGregory right, I agree with you, I modified, and now I believe working with VC++ express, WDK kit on Windows7 should be fine ... let me delete my first try, that's misleading now – code muncher May 15 '12 at 00:34
1

Dhara Darji, you still haven't quite understood what I was writing before. It is not merely the toolchain that is important. As I pointed out you will not be able to get your driver certified (although you might get it - or the .cat - cross-signed). I stand by every word from my comment:

Well, you should know your tools. Developing for KM on Windows (as for many other platforms) is no kid's play. If your UM app crashes, the app dies. If your driver crashes, the system dies. Please reconsider using unsupported tools for creating Windows KM drivers. Not to mention that your idea of drivers seems to be non-existent from the code snippet above. It is completely different from UM and should not be taken lightly.

Even now in your last attempt, except for the misspelled main (as mian), you don't seem to know that a driver requires an entry point called DriverEntry and that this takes parameters completely different from a classic main.

As I pointed out before, you are mixing user mode (UM) and kernel mode (KM) concepts. This will simply not work. There are no ifs and buts. A few years ago people even argued that C++ wasn't the right language to write KM code. Mostly because of the semantics of new and the implications with paged and nonpaged pool. These days C++ is accepted for its type-checking. There we have the next thing "pool", what's that? You have to learn at least the basics of the Windows internals, and I'm afraid that at least the edition of "Windows ???? Internals" I read back in the days didn't quite cut it. You'll want to get a proper book about drivers, such as Oney's "Programming the Microsoft Windows Driver Model" or a number of the books - including old titles - from the guys at OSR. Even very old books from NT4 times still contain a lot of valuable information, even though you will likely want something for at least Windows 2000, because of the overhaul of the PnP system and more in Windows 2000.

OSR is also an excellent online resource, although I have to caution you: they are selling courses and those courses are excellent. Natrually they'll not provide every detail in the online available resources. I thought I knew something about driver programming, until I took two of their courses in 2005. They also host three mailing lists

Other than the difference between paged and nonpaged pool you will have to learn concepts such as IRQLs and what you can do at which IRQL. Also, most people think of drivers as "programs", which is another fallacy. One can more compare them with DLLs, even though this comparison also has its shortcomings. It is hard for drivers to maintain global state because many threads from various processes in UM and KM may be hammering it with requests. And all of this still has to be running at a reasonable speed. You may also want to learn tools such as WinDbg, even though with the new VS 11 WDK integration you may not need that anymore (VisualKD is an older option for the same problem).

If you think you can get away with Windows UM programming skills in KM, think again! The best scenario I can imagine is that your driver will be badly written and will work only because you used one of the more recent driver models from the WDF, which simplify many of the things that otherwise require in-depth knowledge. However, will it run reliably? You must consider that a crashing program just crashes the program, but your crashing driver will inevitably crash the OS, the repercussions of which may become clear only later. Especially when considering that PCs are also used in hospitals or nuclear power plants.

Also one last note: as I pointed out before you do not need Visual Studio at all. Be it the Express edition or another. The reason being that the WDK is reportedly a subset of the environment used to build the OS (i.e. Windows) itself. This means you can use Vim or whatever your favorite editor and still build your driver. After all, the build environment of the WDK is nmake-based, with an application named build on top and the sources file in your folder being the central file in your make process. Look at any one sample in the WDK, there are plenty. But most of all RTFM (read the friendly manual). The WDK documentation is excellent, especially if you have the comparison from Windows NT4, 2000 or even XP times.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152