Wednesday, March 20, 2013

System Programming: From Exciting World of Linux to (exciting ??) World of Windows

I was a Linux lover for a long time and used to do everything on Linux in my university academic works and research works back in Sri Lanka. However after moving to my new university, things changed so rapidly. Our research group is working mostly on Windows platform and therefore it is better for me to work on Windows just like others. In this way it's easier to work together.


So, now I'm running on Windows 7. Initially it wasn't easy for me to adapt but now I'm feeling comfortable on Windows. In this very first semester there is a course on System Programming on Windows. Well, I have done so much system programming on Linux but Windows is new to me. So, I decided to take the course and see.

In this course we are using the same C compiler which comes bundled with MS Visual Studio but without using the high-level application frameworks we are writing codes directly using Windows API. This way we get the understanding about the internal architecture and functionality of Windows OS.

Here's how we create a new copy of a file using Windows API. Of course I have used some standard C library functions (i.e- printf() ) inside the code but if we want we can write those functionality also using much lower Windows API.

1:  /* Basic cp file copy program                */  
2:  /* cpW file1 file2: Copy file1 to file2           */  
3:  #include <windows.h> /* Always required for Windows */  
4:  #include <stdio.h>  
5:  #define BUF_SIZE 256 /* Increase for faster copy */  
6:  int main(int argc, LPTSTR argv [])  
7:  {  
8:       HANDLE hIn, hOut; /* Input and output handles */  
9:       DWORD nIn, nOut; /* Number bytes transferred */  
10:       CHAR Buffer[BUF_SIZE];  
11:       if (argc != 3) {  
12:            printf ("Usage: cp file1 file2\n");  
13:            return 1;  
14:  }  
15:  /* Create handles for reading and writing. Many     */  
16:  /*     default values are used                */  
17:  hIn = CreateFile (argv[1], GENERIC_READ, 0, NULL,  
18:       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  
19:  if (hIn == INVALID_HANDLE_VALUE) {  
20:       printf ("Cannot open input file\n");  
21:       return 2;  
22:  }  
23:  hOut = CreateFile (argv[2], GENERIC_WRITE, 0, NULL,  
24:       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);  
25:  if (hOut == INVALID_HANDLE_VALUE) {  
26:       printf ("Cannot open output file\n");  
27:       return 3;  
28:  }  
29:  /*     Input and output file handles are open.     */  
30:  /*     Copy file. Note end-of-file detection      */  
31:  while (ReadFile (hIn, Buffer, BUF_SIZE,     &nIn, NULL) && nIn > 0)  
32:       WriteFile (hOut, Buffer, nIn, &nOut, NULL);  
33:  /*     Deallocate resources, such as open handles */  
34:       CloseHandle (hIn); CloseHandle (hOut);  
35:       return 0;  
36:  }  

Save this file as cpW.c in somewhere in your home directory. Additionally create a text file (eg- file1.txt) in the same place with some content. Open the Visual Studio command prompt and go to the place where these files are saved. Issue the following command to compile the program.

               cl cpW.c

Now there should be a file as cpW.exe in that directory. You can use that executable to create a new copy of our text file. Let's do that by using following way.

               cpW file1.txt file2.txt

Now you will find the new text file file2.txt in the same directory with the same content of the first file. This is the very first program I had to write using Windows API. More interesting things will come ahead of time.

I will write those new things I learn on Windows API in the future.



No comments:

Post a Comment