Windows, Unix and ANSI C API Comparison
The "Windows System Programming"(3E)
has a great appendix about Windows, Unix, C Library API comparison.
It's obvious that this appendix lacks of many APIs in Memory, DLL and
Security related areas, but it is still very helpful for referencing
when doing system programming on windows platform.
Original content can be found here: https://my.safaribooksonline.com/0321256190/app02
1. I/O
1. 1 File & Directory
Subject | Windows | UNIX | C Library | Comments |
---|---|---|---|---|
Console I/O | AllocConsole |
terminal I/O | N/A | |
Console I/O | FreeConsole |
terminal I/O | N/A | |
Console I/O | ReadConsole |
read |
getc, scanf, gets |
|
Console I/O | SetConsoleMode |
ioctl |
N/A | |
Console I/O | WriteConsole |
write |
putc, printf, puts |
|
Directory Mgt | CreateDirectory |
mkdir * |
N/A | Make a new directory |
Directory Mgt | FindClose |
closedir * |
N/A | Close a directory search handle |
Directory Mgt | FindFirstFile |
opendir *, readdir * |
N/A | Find first file matching a pattern |
Directory Mgt | FindNextFile |
readdir * |
N/A | Find subsequent files |
Directory Mgt | GetCurrentDirectory |
getcwd * |
N/A | |
Directory Mgt | GetFullPathName |
N/A | N/A | |
Directory Mgt | GetSystemDirectory |
Well-known pathnames | N/A | |
Directory Mgt | RemoveDirectory |
rmdir, unlink * |
remove |
|
Directory Mgt | SearchPath |
Use opendir, readdir |
N/A | Search for a file on a specified path |
Directory Mgt | SetCurrentDirectory |
chdir *, fchdir |
N/A | Change the working directory |
Error Handling | FormatMessage |
strerror |
perror |
|
Error Handling | GetLastError |
errno |
errno |
Global variable |
Error Handling | SetLastError |
errno |
errno |
Global variable |
File Locking | LockFile |
fcntl (cmd=F_GETLK, ..) |
N/A | |
File Locking | LockFileEx |
fcntl (cmd=F_GETLK, ..) |
N/A | |
File Locking | UnlockFile |
fcntl (cmd=F_GETLK, ..) |
N/A | |
File Locking | UnlockFileEx |
fcntl (cmd=F_GETLK, ..) |
N/A | |
File System | CloseHandle (file handle) |
close * |
fclose |
CloseHandle is not limited to files |
File System | CopyFile |
open; read; write; close |
fopen; fread; fwrite; fclose |
Duplicate a file |
File System | CreateFile |
open *, creat * |
fopen |
Open/create a file |
File System | DeleteFile |
unlink * |
remove |
Delete a file |
File System | FlushFileBuffers |
fsynch |
fflush |
Write file buffers |
File System | GetFileAttributes |
stat *, fstat *, lstat |
N/A | |
File System | GetFileInformationByHandle |
stat *, fstat *, lstat |
N/A | Fill structure with file info |
File System | GetFileSize |
stat *, fstat *, lstat |
ftell, fseek |
Get length of file in bytes |
File System | GetFileTime |
stat *, fstat *, lstat |
N/A | |
File System | GetFileType |
stat *, fstat *, lstat |
N/A | Check for character stream device or file |
File System | GetStdHandle |
Use file desc 0, 1, or 2 | Use stdin , stdout , stderr |
|
File System | GetTempFileName |
Use C library | tmpnam |
Create a unique file name |
File System | GetTempFileName, CreateFile |
Use C library | tmpfile |
Create a temporary file |
File System | GetTempPath |
/temp path |
N/A | Directory for temp files |
File System | MoveFile, MoveFileEx |
Use C library | rename |
Rename a file or directory |
File System | CreateHardLink |
link, unlink * |
N/A | Windows does not support links |
File System | N/A | symlink |
N/A | Create a symbolic link |
File System | N/A | readlink |
N/A | Read name in a symbolic link |
File System | N/A, ReadFile returns 0 bytes |
N/A, read returns 0 bytes |
feof |
Rest for end of file |
File System | N/A, use multiple ReadFile s |
readv |
N/A, use multiple freads |
Scatter read |
File System | N/A, use multiple WriteFile s |
writev |
N/A, use multiple fwrites |
Gather write |
File System | ReadFile |
read |
fread |
Read data from a file |
File System | SetEndOfFile |
chsize * |
N/A | |
File System | SetFileAttributes |
fcntl |
N/A | |
File System | SetFilePointer |
lseek |
fseek |
Set file pointer |
FileSystem | SetFilePointer (to 0) |
lseek (0) |
rewind |
|
File System | SetFileTime |
utime * |
N/A | |
File System | SetStdHandle |
close, dup *, dup2 *, or fcntl |
freopen |
dup2 or fcntl |
File System | WriteFile |
write |
fwrite |
Write data to a file |
1.2 Async I/O
Subject | Windows | UNIX | C Library | Comments |
---|---|---|---|---|
Asynch I/O | GetOverlappedResult |
N/A | N/A | |
Asynch I/O | ReadFileEx |
N/A | N/A | Extended I/O with completion routine |
Asynch I/O | SleepEx |
N/A | N/A | Alertable wait |
Asynch I/O | WaitForMultipleObjects (file handles) |
poll, select |
N/A | |
Asynch I/O | WaitForMultipleObjectsEx |
N/A | N/A | Alertable wait |
Asynch I/O | WriteFileEx |
N/A | N/A | Extended I/O with completion routine |
Asynch I/O | WaitForSingleObjectEx |
waitpid |
N/A | Alertable wait |
2. Memory & DLL
Subject | Windows | UNIX | C Library |
---|---|---|---|
Mapped Files | CreateFileMapping |
shmget |
N/A |
Mapped Files | MapViewOfFile |
mmap, shmat |
N/A |
Mapped Files | MapViewOfFileEx |
mmap, shmat |
N/A |
Mapped Files | OpenFileMapping |
shmget |
N/A |
Mapped Files | UnmapViewOfFile |
munmap, shmdt, shmctl |
N/A |
Memory Mgt | GetProcessHeap |
N/A | N/A |
Memory Mgt | GetSystemInfo |
N/A | N/A |
Memory Mgt | HeapAlloc |
sbrk , brk , or C library |
malloc, calloc |
Memory Mgt | HeapCreate |
N/A | N/A |
Memory Mgt | HeapDestroy |
N/A | N/A |
Memory Mgt | HeapFree |
Use C library | free |
Memory Mgt | HeapReAlloc |
Use C library | realloc |
Memory Mgt | HeapSize |
N/A | N/A |
Shared Memory | CloseHandle (map handle) |
shmctl |
N/A |
Shared Memory | CreateFileMapping, OpenFileMapping |
shmget |
N/A |
Shared Memory | MapViewOfFile |
shmat |
N/A |
Shared Memory | UnmapViewOfFile |
shmdt |
N/A |
DLLs | LoadLibrary |
dlopen |
N/A |
DLLs | FreeLibrary |
dlclose |
N/A |
DLLs | GetProcAddress |
dlsyn |
N/A |
DLLs | DllMain |
pthread_once |
N/A |
3. Process & Thread
3.1 Process
Subject | Windows | UNIX | C Library | Comments |
---|---|---|---|---|
Process Mgt | CreateProcess |
fork (); execl () *, system() |
N/A | There are 6 execxx functions |
Process Mgt | ExitProcess |
_exit |
exit |
|
Process Mgt | GetCommandLine |
argv [] |
argv [] |
|
Process Mgt | GetCurrentProcess |
getpid * |
N/A | |
Process Mgt | GetCurrentProcessId |
getpid * |
N/A | |
Process Mgt | GetEnvironmentStrings |
N/A | getenv |
|
Process Mgt | GetEnvironmentVariable |
N/A | getenv |
|
Process Mgt | GetExitCodeProcess |
wait, waitpid |
N/A | |
Process Mgt | GetProcessTimes |
times, wait3, wait4 |
N/A | |
Process Mgt | GetProcessWorkingSetSize |
wait3, wait4 |
N/A | |
Process Mgt | N/A | execl * , execv * , execle * , execve * , execlp * , execvp * |
N/A | Windows does not have a direct equivalent |
Process Mgt | N/A | fork, vfork |
N/A | Windows does not have a direct equivalent |
Process Mgt | N/A | getppid |
N/A | No parent/child relationships in Windows |
Process Mgt | N/A | getgid, getegid |
N/A | No process groups in Windows |
Process Mgt | N/A | getpgrp |
N/A | |
Process Mgt | N/A | setpgid |
N/A | |
Process Mgt | N/A | setsid |
N/A | |
Process Mgt | N/A | tcgetpgrp |
N/A | |
Process Mgt | N/A | tcsetpgrp |
N/A | |
Process Mgt | OpenProcess |
N/A | N/A | |
Process Mgt | SetEnvironmentVariable |
putenv |
N/A | putenv is not part of the Standard C library |
Process Mgt | TerminateProcess |
kill |
N/A | |
Synch: Process | WaitForMultipleObjects (process handles) |
waitpid |
N/A | |
Synch: Process | WaitForSingleObject (process handle) |
wait, waitpid |
N/A | |
Timers | KillTimer |
alarm (0) |
N/A | |
Timers | SetTimer |
alarm |
N/A | |
Timers | Sleep |
sleep |
N/A | |
Timers | Sleep |
poll or select , no file descriptor |
N/A |
3.2 Thread
Subject | Windows | UNIX/Pthreads | Comments |
---|---|---|---|
Thread Mgt | N/A | ||
TLS | |||
TLS | |||
TLS | |||
TLS | |||
Thread Mgt | |||
Thread Mgt | |||
Thread Mgt | |||
Thread Mgt | N/A | ||
Thread Mgt | |||
Thread Mgt | N/A | ||
Thread Mgt | N/A | ||
Thread Mgt | is safer | ||
Thread Mgt | (thread handle) | ||
Thread Priority | |||
Thread Priority | |||
Thread Priority | |||
Thread Priority | |||
Note: Pthreads, while a part of all modern UNIX offerings, are available on non-UNIX systems as well. |
3.3 Synchronization
Subject | Windows | UNIX/Pthreads | Comments |
---|---|---|---|
Synch: CritSec | Use mutexes to emulate critical sections. Some systems provide proprietary equivalents. | C library is not applicable | |
Synch: CritSec | C library is not applicable | ||
Synch: CritSec | |||
Synch: CritSec | |||
Synch: Event | (event handle) | ||
Synch: Event | |||
Synch: Event | Manual-reset event | ||
Synch: Event | N/A | ||
Synch: Event | Auto-reset event | ||
Synch: Event | (event handle) | ||
Synch: Event | (event handle) | ||
Synch: Mutex | (mutex handle) | ||
Synch: Mutex | |||
Synch: Mutex | |||
Synch: Mutex | (mutex handle) | ||
Synch: Sem | |||
Synch: Sem | N/A | Windows does not directly support all these options | |
Synch: Sem | |||
Synch: Sem | |||
Synch: Sem | (semaphore handle) | Windows can wait for only one count |
3.4 IPC
Subject | Windows | UNIX | C Library | Comments |
---|---|---|---|---|
IPC | CallNamedPipe |
N/A | N/A | CreateFile, WriteFile, ReadFile, CloseHandle |
IPC | CloseHandle (pipe handle) |
close, msgctl |
pclose |
Not part of the Standard C library—see Stevens |
IPC | ConnectNamedPipe |
N/A | N/A | |
IPC | CreateMailslot |
N/A | N/A | |
IPC | CreateNamedPipe |
mkfifo, msgget |
N/A | |
IPC | CreatePipe |
pipe |
popen |
Not part of the Standard C library—see Stevens |
IPC | DuplicateHandle |
dup, dup2 , or fcntl |
N/A | Or use file names CONIN$ , CONOUT$ |
IPC | GetNamedPipeHandleState |
stat, fstat , lstat64 |
N/A | |
IPC | GetNamedPipeInfo |
stat, fstat , lstat |
N/A | |
IPC | ImpersonateNamedPipeClient |
N/A | N/A | |
IPC | PeekNamedPipe |
N/A | N/A | |
IPC | ReadFile (named pipe handle) |
read (fifo), msgsnd |
N/A | |
IPC | RevertToSelf |
N/A | N/A | |
IPC | SetNamedPipeHandleState |
N/A | N/A | |
IPC | TransactNamedPipe |
N/A | N/A | WriteFile; ReadFile |
IPC | WriteFile (named pipe handle) |
write (fifo), msgrcv |
N/A | |
Misc. | GetComputerName |
uname |
N/A | |
Misc. | SetComputerName |
N/A | N/A | |
Security | SetNamedPipeIdentity |
Use directory sticky bit | N/A |
4. Misc
4.1 Security
Subject | Windows | UNIX | Comments |
---|---|---|---|
Security | C library does not support security | ||
Security | |||
Security | N/A | ||
Security | N/A | ||
Security | |||
Security | N/A | ||
Security | |||
Security | |||
Security | |||
Security | N/A | ||
Security | |||
Security | |||
Security | N/A | ||
Security | |||
Security | |||
Security | |||
Security | N/A | ||
Security | N/A | ||
Security | N/A | ||
Security | N/A | ||
Security | |||
Security | |||
Security | N/A | ||
Security | |||
Security | |||
Security | |||
Security | N/A |
4.2 Exception Handling
Subject | Windows | UNIX | C Library |
---|---|---|---|
SEH | Use C library signals | Use C library signals | |
SEH | Use C library signals | Use C library signals | |
SEH | Use C library signals | Use C library signals | |
SEH | Use C library signals | Use C library signals | |
SEH | Use C library signals | ||
Signals | Use block | Use C library | |
Signals | Use C library or terminate process | ||
Signals | Use C library | Use C library | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH | N/A | |
Signals | Use SEH, VEH, or C library | Use C library | |
Note: Many UNIX vendors provide proprietary exception handling capabilities. |
4.3 System Information & Time
Comments
- Anonymous
August 30, 2010
Some correction:symlink > CreateSymbolicLinklstat > GetFullPathName (say, for symbolic links as lstat is the link version of stat)