| 
                        
         ...............By
        Adarsh Hoizal  | 
                       
                     
                   
                  
                    
                      
                        | 
                           To
                          Create a file on the local drive. Assume the path to
                          be  C:\  and create a text file called
                          file1.txt. Use Windows API function CreateFile or
                          OpenFile,  i.e  Do not use C function fopen.  | 
                       
                     
                   
                  #include<windows.h> 
                  #include<stdio.h> 
                   
                  #define FILEPATH "c:\\file1.txt"  
                  /* This is the path */ 
                   
                  main() 
                  { 
                   
	HANDLE filehandle; 
	DWORD h; 
                   
	filehandle=CreateFile( FILEPATH,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);  
                  /*This creates the file, if file
                  already exists, it overwrites it. */ 
                   
                  if(INVALID_HANDLE_VALUE==filehandle) 
                  { 
                     printf("File could not be created as
                  function returned an invalid handle \n\n "); 
                     return 0; 
                  } 
                  printf("File
                  successfully created"); 
                   
                  CloseHandle(filehandle);       
                  /* Close the open file handle */ 
                   
                  printf("\n\n"); 
                  return 0; 
                  } 
                   
                  
                    
                      
                        | To
                          read the contents of a text file and count the number
                          of lines and words in it. Assume the path to be C:\file1.txt.
                          Use Windows API function ReadFile, i.e Do not use C
                          function  fread. | 
                       
                     
                   
                  #include<windows.h> 
                  #include<stdio.h> 
                  #define MAX 100 
                  #define FILEPATH "c:\\file1.txt"    
                  /* This is the File Path */ 
                   
                  main() 
                  { 
                   
	HANDLE filehandle; 
	DWORD h; 
	int readerror=0;      
                  /* readerror returns the status of read operation, it could
                  also be a boolean type since it returns only two values*/ 
                   
	int linecount=0,wordcount=0; 
	bool wordstarted=false; 
	char c; 
	DWORD chars; 
                   
                   
	filehandle=CreateFile( FILEPATH,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); 
                   
                  /* The file would open only if it
                  exists. In case the file does not exist at the specified path,
                  the above function fails */ 
                  if(INVALID_HANDLE_VALUE==filehandle) 
                  { 
                     printf("File could not be opened as function
                  returned an invalid handle "); 
                     return 0; 
                  } 
                  /*
                  The below code executes only if CreateFile returns a valid
                  file handle  */ 
	chars=0; 
                   
	while(1) 
                   { 
                     readerror=ReadFile(filehandle,&c,1, &h,NULL);   
                    /* ReadFile returns
                  a  bool type TRUE or FALSE into readerror. If readerror=0
                  or FALSE, then file read operation was not successful. In case
                  it returns a 1 or TRUE, read operation was successful. The
                  number of bytes read from the file is returned as a DWORD and
                  is stored into h. Hence to check whether  the read
                  operation was successful or not, we have to check the return
                  values of readerror and h.  */ 
                    
                      if (readerror==0 || h<=0 )      
                  /*    An alternate method of checking this
                  would be to check if (readerror && h)==0 
                  */  
                      { 
                              
                  if(wordstarted) wordcount++; 
                        
                              
                  if(chars>0 && linecount==0)  linecount++; 
                              
                  break; 
                     } 
                   
                     chars++; 
                   
                     if( c==' ')                                          
                  /* Check for blank spaces 
                  */ 
                     { 
                             
                  if(wordstarted) 
                             
                  { 
                                    
                  wordcount++; 
                                    
                  wordstarted=FALSE; 
                             } 
                     } 
                    else 
                     { 
                             
                  if(c!='\n')                                
                  /* Check for newline escape
                  sequence  */ 
                             
                  { 
                                    
                  if(c!='\r')                         
                  /* Check for carriage
                  returns  */ 
                                   
                  { 
                                        
                  if(!wordstarted) 
                                         
                  { 
                                                
                  wordstarted=TRUE; 
                                         
                  } 
                                   
                  } 
                             } 
                            else 
                             { 
                                  
                  if(wordstarted)  wordcount++; 
                                  
                  wordstarted=FALSE; 
                                  
                  linecount++; 
                            }  
                      } 
                    } 
                   
	printf("\n\n"); 
	printf("Number of Words = %d",wordcount); 
                   
	printf("\n\n"); 
	printf("Number of Lines = %d",linecount); 
                   
                  CloseHandle(filehandle);       
                  /* Close the open file handle */ 
                   
                  printf("\n\n"); 
	return 0; 
                  } 
                  
                    
                      
                        | To
                          add the contents of two text files and store it in a
                          third text file. Use Windows API functions. Assume the
                          two files to be file1.txt and file2.txt stored in
                          C:\    Create a third file called
                          file3.txt and store the result in it. | 
                       
                     
                   
                  #include<windows.h> 
                  #include<stdio.h> 
                  #define MAX 4*1024    /*
                  Max size of Array = 4k Arrays give a very fast performance but
                  is inefficient */ 
                   
                  #define FILEPATH1 "d:\\file1.txt"       
                  /* File 1 to read from  */ 
                  #define FILEPATH2 "d:\\file2.txt"       
                  /* File 2  to read
                  from  */ 
                  #define FILEPATH3 "d:\\file3.txt"       
                  /* File 3  to write into
                  File 3 = File 1 + File 2  */ 
                   
                  int main() 
                  { 
                   
                  /* Define Arrays */ 
	char Arr1[MAX]=""; 
    char Arr2[MAX]=""; 
    char Arr3[MAX]=""; 
                   
                  /* Define File Handles */ 
	HANDLE filehandle1; 
	HANDLE filehandle2; 
    HANDLE filehandle3; 
                   
                  /* Define number of bytes
                  read/written from/to  files */ 
                   
    DWORD h1; 
    DWORD h2; 
    DWORD h3; 
                   
	int readerror; 
                  int writeerror; 
 filehandle1 = CreateFile( FILEPATH1,  
                                                        
                  GENERIC_READ | GENERIC_WRITE,  
                                                        
                  0, 
                                                        
                  NULL, 
                                                        
                  OPEN_EXISTING, 
                                                        
                  0, 
                                                        
                  NULL ); 
                   
 if(INVALID_HANDLE_VALUE==filehandle1) 
 { 
                     MessageBox(0,"File 1 returned Invalid Handle", "Error",0); 
                     printf("Error Number = %d",GetLastError()); 
                     return 0; 
 } 
                   
                   
                  readerror=ReadFile(filehandle1, Arr1,sizeof(Arr1),&h1,NULL );  
                   
                  if((readerror && h1)==0)  
 { 
	 MessageBox(0,"Error Reading File 1", "Read Error",0); 
	 printf("Error Number =%d",GetLastError()); 
	 CloseHandle(filehandle1); 
	 return 0; 
 } 
                   
 Arr1[h1]='\0'; 
                   
 filehandle2 = CreateFile( FILEPATH2, 
                                                        
                  GENERIC_READ | GENERIC_WRITE, 
                                                        
                  0, 
                                                        
                  NULL, 
                                                        
                  OPEN_EXISTING, 
                                                        
                  0, 
                                                        
                  NULL ); 
                   
 if(INVALID_HANDLE_VALUE==filehandle2) 
 { 
                     MessageBox(0,"File 2 returned Invalid Handle",
                  "Error",0); 
                     printf("Error Number = %d",GetLastError()); 
                     return 0; 
 } 
                   
                  readerror=ReadFile(filehandle2, Arr2,sizeof(Arr2),&h2,NULL );  
                   
                  if((readerror && h2)==0)  
 { 
	 MessageBox(0,"Error Reading File 2", "Read Error",0); 
	 printf("Error Number =%d",GetLastError()); 
	 CloseHandle(filehandle2); 
	 CloseHandle(filehandle1); 
	 return 0; 
 } 
                   
 Arr2[h2]='\0'; 
                   
                  /* 
                  Add contents of Array 1 and Array 2 to Array 3  */ 
 strcpy(Arr3,Arr1); 
 strcat(Arr3,Arr2); 
                   
 filehandle3= CreateFile( FILEPATH3,  
                                                        
                  GENERIC_READ | GENERIC_WRITE,  
                                                        
                  0, 
                                                        
                  NULL, 
                                                        
                  CREATE_ALWAYS,  
                                                        
                  0, 
                                                        
                  NULL ); 
                   
	if( INVALID_HANDLE_VALUE ==filehandle3)  
	{ 
                     MessageBox(NULL,"Invalid File Handle 3","Error",0); 
                     printf("Error Number =%d",GetLastError()); 
                     CloseHandle(filehandle2); 
                     CloseHandle(filehandle1); 
                     return 0; 
	} 
	else 
                   
	{  
                      writeerror=WriteFile(filehandle3,Arr3,strlen(Arr3),&h3,NULL);   
                      if(!writeerror) 
                      { 
                          MessageBox(NULL,"Cannot write to File","Write Error",0); 
                          printf("Error Number =%d",GetLastError()); 
                          CloseHandle(filehandle3); 
                          CloseHandle(filehandle2); 
                          CloseHandle(filehandle1); 
                          return 0; 
                     } 
	} 
                   
                  printf("Program successfully completed."); 
                  printf("\n\n"); 
                  return 0; 
                  }
                    
                    
                    
                   
                    
                      
                | 
                   Win32
                  Non Console based Applications   | 
                       
                     
                   
                  
                    
                      
                         
                         | 
                        
         ...............By
        Adarsh Hoizal  | 
                       
                      
                        
                          
                            
                              
                                | 
                                   To
                                  Create a simple window.  | 
                               
                             
                           
                          
                            
                              
                                #include<windows.h> 
                                   
                                  LRESULT CALLBACK
                                  myfunc(HWND,UINT,WPARAM,LPARAM);   
                                  /* This will handle the message calls */ 
                                   
                                  int WINAPI WinMain(HINSTANCE h, HINSTANCE
                                  p,LPSTR l, int n) 
                                  { 
                                     WNDCLASS w ;  /*
                                  Its a structure whose elements we need to fill
                                  in */ 
                                    
                                  HWND handle ; 
                                     MSG m ; 
                                   
                                     w.cbClsExtra =0 ; 
                                     w.cbWndExtra =0 ; 
                                     w.hbrBackground =(HBRUSH)
                                  GetStockObject(WHITE_BRUSH) ; 
                                     w.hCursor =0 ; 
                                     w.hIcon=0 ; 
                                     w.hInstance =h ; 
                                     w.lpfnWndProc =myfunc ; 
                                     w.lpszClassName ="MyClass"
                                  ; 
                                     w.lpszMenuName =NULL ; 
                                     w.style =0 ; 
                                   
                                  /*
                                  First we need to register the class with the
                                  Windows */ 
                                   
                                     if(!RegisterClass(&w)) return
                                  0 ;
                                  /*
                                  Now that the class has been registered we can
                                  create the window */ 
                                   
                                     handle=CreateWindow("MyClass",          
                                  /*
                                  Class Name */ 
                                                                         
                                  "Adarsh's Window",   /*
                                  Title  Text */ 
                                                                          
                                  WS_OVERLAPPEDWINDOW,   /*
                                  Style */ 
                                                                          
                                  50,                    
                                  /*
                                  X Co-ordinate */ 
                                                                          
                                  50,                    
                                  /*
                                  Y Co-ordinate */ 
                                                                          
                                  300,                  
                                  /*
                                  Width */ 
                                                                          
                                  300,                  
                                  /* Height */ 
                                                                          
                                  0,                      
                                  /*  Parent Window  */ 
                                                                          
                                  0,                      
                                  /*  Child Identifier  */ 
                                                                          
                                  h,                      
                                  /* 
                                  Application Instance  */ 
                                                                          
                                  0)
                                  ;                    
                                  /* 
                                  Window creation data 
                                  */ 
                                    
                                  if (!handle) return 0;   /*
                                  In case handle is not created terminate
                                  program */ 
                                   
                                     ShowWindow(handle,n); 
                                     UpdateWindow(handle); 
                                   /*
                                  Now the window is created and we just need to
                                  get messages from the windows operating system
                                  about various events encountered by it.
                                  Whatever message we feel is appropriate to
                                  take some action would be dealt with
                                  accordingly and remaining would be left to the
                                  operating system to take care of */ 
                                   
                                     while(GetMessage(&m,0,0,0)) 
                                     { 
                                       TranslateMessage(&m); 
                                       DispatchMessage(&m); 
                                     } 
                                   
                                     return m.wParam ; 
                                  } 
                                   
                                  LRESULT CALLBACK myfunc(HWND hw,UINT
                                  msg,WPARAM wP,LPARAM lP) 
                                  { 
                                     switch(msg) 
                                     { 
                                     
                                  /* Trap
                                  the event of closing the window. When found,
                                  terminate the program and return 0 to compiler
                                  indicating successful program execution */ 
                                   
                                       case WM_DESTROY:   
                                               
                                  PostQuitMessage(0); 
                                               
                                  break; 
                                   
                                      
                                  default:            
                                  /*
                                  In any other event let windows handle the
                                  message */ 
                                              
                                  return DefWindowProc(hw,msg,wP,lP); 
                                     } 
                                    return 0; 
                                  } 
                                    | 
                               
                             
                           
                          
                            
                              
                                | To
                                  make your own SCREEN SAVER with your own
                                  custom settings. Compile the below program to
                                  generate an icon. Select this icon from the
                                  Screen Saver dialog box. Please note that you
                                  also need to make/copy an icon which would be
                                  stored in the resource file. | 
                               
                             
                           
                          
                            
                              
                                #include<windows.h> 
                                  #include<scrnsave.h> 
                                   
                                   
                                  BOOL WINAPI
                                  ScreenSaverConfigureDialog(HWND,UINT,WPARAM,LPARAM);  
                                  /*
                                  These functions are not used   */ 
                                  BOOL WINAPI RegisterDialogClasses(HANDLE);   
                                  /* 
                                  but
                                  could be used
                                  to enhance screen saver features  */ 
                                   
                                  char
                                  str[80]="Welcome to Adarsh Hoizal's
                                  Homepage";      
                                  /*
                                  The text you want to display */ 
                                   
                                  int delay=200; 
                                   
                                  LRESULT CALLBACK ScreenSaverProc(HWND hwnd , UINT  message, WPARAM wparam ,LPARAM lparam) 
                                  { 
                                   	static HDC hdc; 
                                   	static unsigned int timer; 
                                   	static RECT scrdim; 
                                   	static SIZE size; 
                                   	static int X=0, Y=0; 
                                   	static HBRUSH hBlkBrush; 
                                   	static TEXTMETRIC tm; 
                                   
                                   	switch(message) 
                                   	{ 
                                      	case WM_CREATE : 
                                      		timer=SetTimer(hwnd,1,delay,NULL); 
                                      		hBlkBrush=(HBRUSH) GetStockObject(BLACK_BRUSH); 
                                      		break; 
                                   
                                      	case WM_ERASEBKGND: 
                                      		hdc=GetDC(hwnd); 
                                      		GetClientRect(hwnd,&scrdim); 
                                      		SelectObject(hdc,hBlkBrush); 
                                      		PatBlt(hdc,0,0,scrdim.right,scrdim.bottom,PATCOPY); 
                                      		GetTextMetrics(hdc,&tm); 
                                      		GetTextExtentPoint32(hdc,str,strlen(str),&size); 
                                      		ReleaseDC(hwnd,hdc); 
                                      		break; 
                                   
                                      	case WM_TIMER: 
                                      		hdc=GetDC(hwnd); 
                                      		SelectObject(hdc,hBlkBrush); 
                                      		PatBlt(hdc,X,Y,X+size.cx,Y+size.cy,PATCOPY); 
                                      		X+=10;Y+=10; 
                                        
                                      		if(X>scrdim.right) X=0; 
                                      		if(Y>scrdim.bottom)Y=0; 
                                      		SetBkColor(hdc,RGB(0,0,0)); 
                                      		SetTextColor(hdc,RGB(0,255,255)); 
                                      		TextOut(hdc,X,Y,str,strlen(str)); 
                                      		ReleaseDC(hwnd,hdc); 
                                      		break; 
                                   
                                      	case WM_DESTROY: 
                                      		KillTimer(hwnd,timer); 
                                      		break; 
                                   
                                      	default: 
                                      		return DefScreenSaverProc(hwnd,message,wparam,lparam); 
                                    	} 
	return 0; 
                                  } 
                                   
                                   
                                  BOOL WINAPI ScreenSaverConfigureDialog(HWND hdwnd,UINT message,WPARAM wparam,LPARAM lparam) 
	{ 
                                   		return 0; 
	} 
                                   
	BOOL WINAPI RegisterDialogClasses(HANDLE hInst) 
	{ 
                                   		return 1; 
	} 
                                  
                                  
                                    
                                      
                                        | 
                                           Write
                                          a program to create a Pop up timer.  | 
                                       
                                     
                                   
                                  #include <windows.h> 
                                  #include "resource.h"  /*
                                  See the Resource file below *?
                                   
                                   
                                   
                                  WNDCLASS w; 
                                  MSG m; 
                                  HWND hMainWnd; 
                                  HMENU hMenu, hPop; 
                                  int x,y; 
                                  POINT p; 
                                  int pos; 
                                  int toggle = 0; 
                                  int flag; 
                                   
                                  LRESULT CALLBACK TimerProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) 
                                  { 
                                    switch(LOWORD(wParam)) 
                                    { 
                                       case 1: 
                                            
                                  MessageBox(hWnd,"Timer 1","Alert",0); 
                                            
                                  break; 
                                   
                                      case 2: 
                                           
                                  MessageBox(hWnd,"Timer 2","Alert",0); 
                                           
                                  break; 
                                    } 
                                  return 0; 
                                  } 
                                   
                                  LRESULT CALLBACK wp(HWND hWnd,UINT msg, WPARAM wParam, LPARAM lParam) 
                                  { 
                                   switch(msg) 
                                    { 
                                      case WM_DESTROY: 
                                            
                                  PostQuitMessage(0); 
                                            
                                  break; 
                                   
                                      case WM_CREATE: 
                                            
                                  break; 
                                   
                                     case
                                  WM_RBUTTONDOWN: 
                                             
                                  x = LOWORD(lParam); 
                                             
                                  y = HIWORD(lParam); 
                                             
                                  p.x = x; 
                                             
                                  p.y = y; 
                                   
                                             
                                  ClientToScreen(hWnd,&p); 
                                             
                                  TrackPopupMenu(hPop,TPM_LEFTALIGN,p.x,p.y ,0,hWnd,0); 
                                             
                                  break; 
                                   
                                   
                                      case WM_INITMENUPOPUP: 
                                             
                                  hMenu = (HMENU) wParam; 
                                             
                                  pos = LOWORD(lParam); 
                                   
                                             
                                  toggle = (toggle == 0)?1:0; 
                                             
                                  flag = (toggle==1)?MF_GRAYED:MF_ENABLED; 
                                   
                                             
                                  EnableMenuItem(hMenu,IDM_F_OPEN,flag|MF_BYCOMMAND); 
                                             
                                  break; 
                                   
                                      case WM_COMMAND: 
                                             
                                  switch(LOWORD(wParam)) 
                                             
                                  { 
                                                
                                  case 1: 
                                                    
                                  MessageBox(hWnd,"Item 1","Alert",0); 
                                                    
                                  break; 
                                   
                                                
                                  case 2: 
                                                   
                                  MessageBox(hWnd,"Item 2","Alert",0); 
                                                   
                                  break; 
                                   
                                               
                                  case IDM_F_OPEN: 
                                                  
                                  MessageBox(hWnd,"File Opened","Alert",0); 
                                                  
                                  break; 
                                   
                                              
                                  case IDM_F_CLOSE: 
                                                  
                                  MessageBox(hWnd,"File Closed","Alert",0); 
                                                  
                                  break; 
                                   
                                              
                                  case IDM_F_EXIT: 
                                                  
                                  MessageBox(hWnd,"Bye","Alert",0); 
                                                  
                                  DestroyWindow(hWnd); 
                                                  
                                  break; 
                                            
                                  } 
                                           
                                  break; 
                                   
	default: 
		return DefWindowProc(hWnd,msg,wParam,lParam); 
	} 
                                   
	return 0l; 
                                  } 
                                   
                                  int WINAPI WinMain(HINSTANCE hInst,HINSTANCE
                                  hPrev, LPSTR lpszCommand, int nCmdShow) 
                                  { 
                                    w.cbClsExtra = 0; 
                                    w.cbWndExtra = 0; 
                                    w.hbrBackground = GetStockObject(WHITE_BRUSH); 
                                    w.hCursor = LoadCursor(0,IDC_ARROW); 
                                    w.hIcon = LoadIcon(0,IDI_APPLICATION); 
                                    w.hInstance = hInst; 
                                    w.lpfnWndProc = wp; 
                                    w.lpszClassName = "TimeTested"; 
                                    w.lpszMenuName = 0; 
                                    w.style = CS_HREDRAW| CS_VREDRAW; 
                                   
                                    if (!RegisterClass(&w)) return 0; 
                                   
                                    hMenu =
                                  LoadMenu(hInst,MAKEINTRESOURCE(IDM_MAIN));
                                  /*
                                  This will provide handle to the resource
                                  object */
                                   
                                   
                                    hPop  =  CreatePopupMenu(); 
                                   
                                    if (!hPop) return 0;
                                   
                                   
                                    AppendMenu(hPop,MF_STRING,1,"Item1"); 
                                    AppendMenu(hPop,MF_STRING,2,"Item2"); 
                                   
                                    hMainWnd = CreateWindow("TimeTested","Time",WS_OVERLAPPEDWINDOW,10,10,200,200,0,hMenu,hInst,0); 
                                   
                                    if(!hMainWnd) return 0; 
                                   
                                    ShowWindow(hMainWnd,nCmdShow); 
                                   
                                    while(GetMessage(&m,0,0,0)) 
                                    { 
                                       TranslateMessage(&m); 
                                       DispatchMessage(&m); 
                                    } 
                                   
                                   return m.wParam; 
                                  } 
                                   
                                  Resource.h
                                  contains the following  
                                   
                                  //{{NO_DEPENDENCIES}} 
                                  // Microsoft Developer Studio generated include file. 
                                  // Used by MainScript.rc 
                                   
                                  #define IDM_MAIN                        101 
                                  #define IDM_POPUP                       102 
                                  #define IDM_F_OPEN                      40001 
                                  #define IDM_F_CLOSE                     40002 
                                  #define IDM_F_EXIT                      40003 
                                  #define IDM_E_CUT                       40004 
                                  #define ID_CONTEXT                      40005 
                                  #define ID_ITEM2                        40006 
                                  #define ID_ITEM3                        40007 
                                   
                                  // Next default values for new objects 
                                    
                                  #ifdef APSTUDIO_INVOKED 
                                  #ifndef APSTUDIO_READONLY_SYMBOLS 
                                  #define _APS_NEXT_RESOURCE_VALUE        103 
                                  #define _APS_NEXT_COMMAND_VALUE         40008 
                                  #define _APS_NEXT_CONTROL_VALUE         1000 
                                  #define _APS_NEXT_SYMED_VALUE           101 
                                  #endif 
                                  #endif 
                                    | 
                               
                             
                           
                         | 
                       
                     
                   
                 |