Can I use winnt.h in Linux? - c

My program is written in C. I want to use library winnt.h, but I don't use Windows anymore.

Seems like a strange question; you should probably clarify which function(s) you actually need from winnt.h so that you can learn the Linux equivalent. winnt.h isn't really a general purpose "library", it's just an interface to built in Windows-specific functions.
With that as a major caveat, you may get some degree of what you want by attempting to run your app with the help of Wine. See http://www.winehq.org/ If you're just trying to run an existing app, that's may be a reasonable solution. If you're trying to make a Linux version of your app, though, that won't help you very much.

No, well you could but it's not going to do any good - the.h file just declares functions that are defined in libs that are only on windows

No. You can't.
winnt.h contains lots of macros that depend on a Windows environment and a lot of function declarations that only exist in Windows-specific libraries. So, it's not really useful (or possible) to use winnt.h on Linux.
That said, you can use Winelib, which includes most of the functionality exposed by those Windows-specific headers, and you can get those features by linking your program with Winelib. In general, this is probably not a good idea, because Winelib is relatively unstable (the functionality of a given API function may be absent, incomplete, buggy, or incompatible compared to the native Windows version). It is a much better idea to look for a Linux-native alternative to what you need.

What parts of winnt.h do you want to use? Of course, if you need some nice macroses or type definitions from it, you can freely copy it to your own header file (of course, with dependencies). But if you include all winnt.h file to your program in linux environment, you will get tons of error messages. One of the reasons for it is pronounced by Martin Beckett in his reply.

Related

How to add a header file to the c project?

Am using a Dev c++ compiler, to compile a c code. (I am a beginner)
When I compile, it says 'some' header files are missing.
How can i include those header files in my system, so as to be utilized by the program??
Thanks
A header such as <sys/sem.h> which is used for the function semget() among other things, is not generally available in Windows. It's a POSIX header, and Windows does not implement the POSIX standard out of the box.
You should maybe look at the Win32 API instead, for instance a function like CreateSemaphore().
The problem is that you are trying to use the Linux API on Windows. Here is what is going on: Every operating system has its own set of libraries for programmers to use to make programs on that platform. In this instance, you are attempting to use Linux libraries on Windows. Windows doesn't have a code location called sys/ipc or sys/sem.
Furthermore, since you said you are a beginner, try finding another tutorial. sys/ipc.h and sys/sem.h are not for beginners, are are libraries typically used for communication between processes. These concepts are way beyond you right now haha :P
Here is a better place to start: http://www.cprogramming.com/tutorial/c-tutorial.html

Hooking in C and windows

I'm looking for a quick guide to basic dll hooking in windows with C, but all the guides I can find are either not C, or not windows.
(The DLL is not part of windows, but a third party program)
I understand the principle, but I don't know how to go about it.
I have pre-existing source code in C++ that shows what I need to hook into, but I don't have any libraries for C, or know how to hook from scratch.
The detours license terms are quite restrictive.
If you merely want to hook certain functions of a DLL it is often cheaper to use a DLL-placement attack on the application whose DLL you want to hook. In order to do this, provide a DLL with the same set of exports and forward those that you don't care about and intercept the rest. Whether that's C or C++ doesn't really matter. This is often technically feasible even with a large number of exports but has its limitations with exported data and if you don't know or can't discern the calling convention used.
If you must use hooking there are numerous ways including to write a launcher and rewrite the prepopulated (by the loader) IAT to point to your code while the main thread of the launched application is still suspended (see the respective CreateProcess flag). Otherwise you are likely going to need at least a little assembly knowledge to get the jumps correct. There are plenty of liberally licensed disassembler engines out there that will allow you to calculate the proper offsets for patching (because you don't want to patch the middle of a multi-byte opcode, for example).
You may want to edit your question again to include what you wrote in the comments (keyword: "DLL hooking").
loading DLLs by LoadLibrary()
This is well known bad practice.
You might want to look up "witch" or "hctiw", the infamous malware dev. there's a reason he's so infamous - he loaded DLLs with LoadLibrary(). try to refrain from bad practice like that.

Emacs Auto-include in C

When using java and eclipse, there is a button to automatically include whatever standard libraries need to be included. Is there any analogous option for C and emacs to save me the effort of writing out includes from standard library?
Unfortunately, I don't think so. In general a feature like that requires the ability to fully parse C code and the only part of Emacs that can do that is CEDET. CEDET has a lot of infrastructure for adding features, so this would be the place to look.
However, C's lack of namespaces would make building something like that very error prone. Often most includes are application specific anyway, so I can't see something restricted to the stdlib be incredibly useful.
You can save yourself some typing with yas-snippets, but that is still a long way from auto-magically figuring out your includes.

How can I best check for C library dependencies?

I'm building something that installs a high-level stack, and to do that, I need to install the lower-level stuff.
The simplest way to look for whether, say, Java is installed, is to just shell out a which java in a shell script and check if it can find it. I'm now to the point where I need to do some libraries without an obvious binary- basically stuff that is an include from within C. libxml, for example.
I'm woefully green to C in general, so this makes things a little tricky for me. :) Ideally I could just make a shell script that calls a little C applicaiton that calls #include <xxxx>, where xxxx is the library that I'm checking the existence of. If it can't find it, it errors out. Unfortunately, of course, all that happens prior to compilation, so it's not as dynamic as I'd like.
I'm doing this on a system that probably doesn't have anything installed on it (be it high-level language or package managers or what have you), so I'm looking more for a basic shell script way of doing things (or maybe some clever C or command-line gcc options). Or maybe just manually search the include paths that gcc would look for anyway /usr/local/include, /usr/include, etc.). Any thoughts?
Autotools is really what you need. Its a huge (and bizarre) framework for dealing with this very problem:
http://www.gnu.org/software/autoconf/
You can also use pkg-config, which will work with newer software making use of that mechanism:
http://pkg-config.freedesktop.org/wiki/
this is the purpose of configure (part of automake and autoconf)

Tool to determine symbol origin in C

I'm looking for a tool that, given a bit of C, will tell you what symbols (types, precompiler definitions, functions, etc) are used from a given header file. I'm doing a port of a large driver from Solaris to Windows and figuring out where things are coming from is getting to be difficult, so this would be a huge help. Any ideas?
Edit: Not an absolute requirement, but tools that work on Windows would be a plus.
Edit #2: To clarify what I'm trying to do, I have a codebase I'm trying to port, which brings in a large number of headers. What I'd like is a tool that, given foo.c, will tell me which symbols it uses from bar.h.
I like KScope, which copes with very large projects.
KScope http://img110.imageshack.us/img110/4605/99101zd3.png
I use on both Linux and Windows :
gvim + ctags + cscope.
Same environment will work on solaris as well, but this is of course force you to use vim as editor, i pretty sure that emacs can work with both ctags and cscope as well.
You might want give a try to vim, it's a bit hard at first, but soon you can't work another way. The most efficient editor (IMHO).
Comment replay:
Look into the cscope man:
...
Find functions called by this function:
Find functions calling this function:
...
I think it's exactly what are you looking for ... Please clarify if not.
Comment replay 2:
ok, now i understand you. The tools i suggested can help you understand code flow, and find there certain symbol is defined, but not what are you looking for.
Not what you asking for but since we are talking i have some experience with porting and drivers (feel free to ignore)
It seems like compiler is good enough for your task. You just starting with original file and let compiler find what missing part, it will be a lot of empty stubs and you will get you code compiled.
At least for beginning i suggest you to create a lot of stubs and modifying original code as less as possible, later on once you get it working you can optimize.
It's might be more complex depending on the type of driver your are porting (I'm assuming kernel driver), the Windows and Solaris subsystems are not so alike. We do have a driver working on both solaris and windows, but it was designed to be multi platform from the beginning.
emacs and etags.
And I leverage make to run the tag indexing for me---that way I can index a large project with one command. I've been thinking about building a master index and separate module indecies, but haven't gotten around to implementing this yet...
#Ilya: Would pistols at dawn be acceptable?
Try doxygen, it can produce graphs and/or HTML and highly customizable

Resources