Win32 API vs. Linux - Round One
Before we start today's fight, we shall introduce you to the contestants, and a bit of background on each of them.
In the Red Corner, we have code written to work well with the Win32 API. The Win32 API is known for being part of Windows, and being able to kill its opponents easily, with the sheer size and complexity of itself. In the Blue Corner, we have code written to work well on Linux. Linux code being known to be quite easy to deal with, and not having heaps of levels of abstraction when it isn't neccesary.
These two come together today, to see who can write the better program that can: * Open a file, specified on the command line, for exclusive read only access. * Report if there was a problem opening the file, with an appropriate error message. * Close the file.
We would like to thank today's sponsor for this event, namely the Systems Programming 2 class that I am currently undertaking at University, and is making me learn how to code with the Windows API.
So that is enough, talk, lets see how the fight goes.
Windows starts off first by allowing a bit more complexity to its CreateFile() routine, but this is unfortunately useless, and not able to withstand the sheer simplicity of Linux's open() call, which beats it hands down.
In an attempt to reclaim, the match, Windows tries to claim that it can give a more superior error message handling routine, for when that file open does not work. Unfortunately, while Windows was still trying to type the function name for the routine to get the error message, Linux had already finished writing code to get and display the error message, thanks to perror(). In fact, Linux was so efficient, it decided to go one step better, and customise the error message all together.
In the end, it came down to closing the handle/file descriptor, to see if Windows would get a foot in, in this round, and it seems that it was a tie. Both Windows and Linux successfully made closing the open file nice and easy.
However, it the clear winner this round is the Linux code. Score: 2.5 - 0.5 -- Linux code wins.
To see the code used in this contest, and to agree with me that Linux definately wins this round in terms of better code/less code for this task, see the code below.
Windows
include <windows.h>
include <stdio.h>
int main(int argc, char *argv[]) { HANDLE hFileToRead; LPTSTR pszErrorMessageBuf; SECURITY_ATTRIBUTES lpSecurityAtrribs;
lpSecurityAtrribs.nLength = sizeof(lpSecurityAtrribs); lpSecurityAtrribs.lpSecurityDescriptor = NULL; lpSecurityAtrribs.bInheritHandle = TRUE; if (argc < 2) { fprintf(stderr, "Syntax: %s <filename>", argv[0]); return -1; } hFileToRead = CreateFile( argv[1], GENERIC_READ, 0, lpSecurityAtrribs, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFileToRead == INVALID_HANDLE_VALUE) { FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, (LPTSTR)&pstrErrorMessageBuf, 0, NULL); fprintf(stderr, "Could not open file '%s': [%d] %s", argv[1], GetLastError(), pstrErrorMessageBuf); LocalFree(pstrErrorMessageBuf); return -1; } else printf("File successfully opened.\n"); if (!CloseHandle(hFileToRead)) fprintf(stderr, "Error closing file...\n"); return 0;}
Linux code
include <stdio.h>
include <sys/types.h>
include <sys/stat.h>
include <fcntl.h>
include <errno.h>
include <string.h>
include <unistd.h>
int main(int argc, char *argv[]) { int fd;
if (argc < 2) { fprintf(stderr, "Syntax: %s <filename>\n", argv[0]); return -1; } fd = open(argv[1], O_EXCL|O_RDONLY); if (fd == -1) { fprintf(stderr, "Could not open file '%s': [%d] %s\n", argv[1], errno, strerror(errno)); return -1; } else printf("File successfully opened.\n"); if (close(fd)) fprintf(stderr, "Error closing file...\n"); return 0;}
Comments
(#2) — praetorian
I never proclaimed that I was an expert at the Win32 API. Infact, the first time I had to deal with it, was during this subject at university this semester gone.
Personally, I use a Linux setup on all my desktop's, whether it be a Desktop machine or a Laptop. Most of the code I write is either in C or Python.
While the CreateFile() function does give you lots of options for the chance that you do need certain things. It is things like to simply make a file have exclusive access, requires quite a bit of code, when it is trival and should not.
I did not look into how such options provided would be added in C programs on for a Linux platform, so I cannot argue the point of it would be hundreds of lines. But this does seem like a fair amount larger than I believe it would take.
Also, you say their is no guarantee that the Same API will be supported by all versions of Windows. Well I can state that this is mostly incorrect, as Microsoft does maintain that they will keep the function call names, the same, and only change internal code.
As for waiting for Longhorn, I have better things to do, like to play Duke Nukem Forever, which will most likely be out before Longhorn is ;-)
(#3) — Huge
If you think an api gives you too many options, write a function to simplify life. eg "file = OpenForReadOrPrintError(argv[1])". Then the code looks the same on both versions - it may even compile on both systems. A small program is not the test of an API - you need a real-world application, and you should assume people put code in libraries and the like.
(#4) — Kieren Johnstone
Isn't the "open" method a POSIX or standard C function - it is available on Windows after all..
Are you comparing different things?

7th March, 2006
Tuesday, 11 July 2006 @ 8:12 a.m.
(#1) — Kapil Suri ( Tata Consultancy Services )
Prae ,
By Looking at your thoughts in Comparing Win32API with Linux Code,it seems either you are novice in Linux or Win API's ,Did you make out the flexibility given by API's..or you are just counting the number of parameters passing in API's above in the code.If you closely look at the parameters above
GENERIC_READ, 0,lpSecurityAtrribs, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
It is giving you the enough options while Creating files,The same if you try to do it through Linux Code..you have to end up in writing hundreds of lines code..
But yep if you write appropriate Linux Code ,it will never crash not hang..these are the benefits you get while writing code with Linux ..The same is unmanageable always with Windows ,I hope Longhorn will take care of it..
Another disadvantages of working with API's ,there is no guarantee that Same API's will be supported by all windows OS..and the exceptions they give is difficult to trace..
But in anyways there will no longer API's funda in newer Operating Systems like Longhornn
Lets wait for it frd...
Kapil Suri
Tata Consultancy Services..