tags:

views:

496

answers:

6

What is the relationship between the Windows API and the C run time library?

+2  A: 

If you mean the standard C library (msvcrt.dll I assume). Then not much at all. The majority of the windows API is implemented in separate dlls (very much of it is in user32.dll or kernel32.dll). In fact, some of these functions in the Windows API are just thin wrappers around system calls where the actual work is done in the kernel itself.

Also, as ocdecio said, it is entirely reasonable to assume that certain parts of the C standard library are implemented using windows APIs. And for certain cases like string manipulations, vice versa.

EDIT: since which dlls are implemented in terms of others has come into question, i've checked with dependancy walker and here is my findings:

kernel32.dll depends on:
  ntdll.dll

user32.dll depends on:
  gdi32.dll
  kernel32
  ntdll.dll
  advapi.dll
  msimg32.dll
  powerprof.dll (this dll references msvcrt.dll for some string functions)
  winsta.dll

msvcrt.dll depends on:
  kernel32.dll (yes it does have imports for CreateFileA)
  ntdll.dll

based off of this, I believe that msvcrt is build on top of the win32 API.

Evan Teran
another downvote? could the downvoters at least explain. I don't believe my answer to be wrong.
Evan Teran
I think the answer is good, so +1... I can't stand it when people downvote a reasonable answer without leaving a comment.
Jason Coco
Is there any difference between C standard library and C runtime libraray?
MainID
Rich, why "did" that for you? i think it's right. fopen could very well be implemented using OpenFile/CreateFile
Johannes Schaub - litb
Dazza, that really deserves its own separate question, not a follow-up in a comment.
Rob Kennedy
dazza, people mean the part of the c standard library that your program uses at runtime - as opposed stuff that it uses at compile time (i.e macro definitions). other ppl mean a specific c standard library implementation when they talk about "c runtime library"
Johannes Schaub - litb
other people say "runtime library" means a library that is bound to your program at loading time, as opposed at linking time. yet another group says runtime libs are those that do not entirely consist only of header files.
Johannes Schaub - litb
I would probably define the "runtime" as the contents of what is usually called crt0 (which stands for c runtime). Which really only *needs* to do some basic pre/post main work (allocate stdio streams and such, call exit with the return value of main, etc).
Evan Teran
@Rich B: what do you think fopen does if it doesn't eventually call CreateFile? I suppose it could directly call the system call "NtOpenFile" like CreateFile eventually does, but that would just be duplicated code. It makes more sense for it to be built ontop of the existing API.
Evan Teran
Also, (I dunno if MS cared enough to avoid it, but) if it does call the system call directly, that would make the dll itself not not always transferable between different versions of the OS. Occasionally MS has be known to change them (see http://www.metasploit.com/users/opcode/syscalls.html)
Evan Teran
Implementation really doesn't matter... fopen is part of the C Standard Library (which MS calls C Runtime Library, but whatever), CreateFile is part of the Win32 API. They could all be rolled up in one giant static archive, and it still wouldn't matter. The diff is they're 2 sets of APIs
Jason Coco
No need to assume anything. The CRT source is included with Visual C++, and yes, it uses Win32 API calls.
bk1e
+4  A: 

In a nutshell: The Windows API contains all the functions defined specifically for Windows. The C run-time library contains all the functions that are required by standard C.

The physical libraries that implement these functions may be a single file (library), split across two separate libraries or split into many libraries, depending on the operating system and the actual API/service you are using.

For example, when creating files, the C standard includes the function:

fopen

to open and create files, etc., while the Win32 API (for example) defines functions like:

CreateFile

to create and manipulate files. The first one will be available wherever a standard C run-time library is available while the second one will only be available on a Windows machine that supports the Win32 API.

Jason Coco
+2  A: 

Win32 is a completely different beast to the CRT.

CRT is something that needs to be linked into your project when you use C or C++ functions/features (such as printf or cout).

Win32 is a set of libraries that need to be linked into your project when you use Windows features (like GetWindowText).

OJ
A: 

What they are:

  • The Windows API is the API exported by the Microsoft Windows[TM] Operating System
  • The C run time library is the "standard library" which is shipped with the C compiler by the compiler vendor, and which is available on whichever/any operating system (for example, Unix) is targetted by the compiler

What their relationship is:

  • They are distinct, but both equally available to C++ applications running on Windows
  • On Windows, the C standard library is implemented by invoking the underlying Windows API (to allocate memory, open files, etc.).
ChrisW
A: 

Unix System calls are analogy with Windows API.

What? That doesn't make any sense.
Geoffrey Chetwood
Rich, i think it does make limited sense: unix syscalls <=> windows API, msvcrt <=> glibc
Johannes Schaub - litb
@litb: I disagree.
Geoffrey Chetwood
A: 

C run time library is based on the Windows API

dmityugov