Pages

Sunday, November 18, 2012

VC++ Ignore CTRL-C and Ctrl-Break Signal

More at MSDN-Example 
MSDN-Function
 
#include <windows.h> 
#include <stdio.h> 
 
BOOL CtrlHandler( DWORD fdwCtrlType ) 
{ 
  switch( fdwCtrlType ) 
  { 
    // Handle the CTRL-C signal. 
    case CTRL_C_EVENT: 
      printf( "Ctrl-C event\n\n" );
      Beep( 750, 300 ); 
      return( TRUE );
 
    // CTRL-CLOSE: confirm that the user wants to exit. 
    case CTRL_CLOSE_EVENT: 
      Beep( 600, 200 ); 
      printf( "Ctrl-Close event\n\n" );
      return( TRUE ); 
 
    // Pass other signals to the next handler. 
    case CTRL_BREAK_EVENT: 
      Beep( 900, 200 ); 
      printf( "Ctrl-Break event\n\n" );
      return TRUE; 
 
    case CTRL_LOGOFF_EVENT: 
      Beep( 1000, 200 ); 
      printf( "Ctrl-Logoff event\n\n" );
      return FALSE; 
 
    case CTRL_SHUTDOWN_EVENT: 
      Beep( 750, 500 ); 
      printf( "Ctrl-Shutdown event\n\n" );
      return FALSE; 
 
    default: 
      return FALSE; 
  } 
} 
 
int main( void ) 
{ 
  if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) ) 
  { 
    printf( "\nThe Control Handler is installed.\n" ); 
    printf( "\n -- Now try pressing Ctrl+C or Ctrl+Break, or" ); 
    printf( "\n    try logging off or closing the console...\n" ); 
    printf( "\n(...waiting in a loop for events...)\n\n" ); 
 
    while( 1 ){ } 
  } 
  else 
  {
    printf( "\nERROR: Could not set control handler"); 
    return 1;
  }
return 0;
}

VC++ Receive a stream of text from a system(Command) process

more at MSDN 
 
// crt_popen.c
/ This program uses _popen and _pclose to receive a 
// stream of text from a system process.
//

#include <stdio.h>
#include <stdlib.h>

int main( void )
{

   char   psBuffer[128];
   FILE   *pPipe;

        /* Run DIR so that it writes its output to a pipe. Open this
         * pipe with read text attribute so that we can read it 
         * like a text file. 
         */

   if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
      exit( 1 );

   /* Read pipe until end of file, or an error occurs. */

   while(fgets(psBuffer, 128, pPipe))
   {
      printf(psBuffer);
   }


   /* Close pipe and print return value of pPipe. */
   if (feof( pPipe))
   {
     printf( "\nProcess returned %d\n", _pclose( pPipe ) );
   }
   else
   {
     printf( "Error: Failed to read the pipe to the end.\n");
   }
}

VC++ Clearing the Screen

more at MSDN 
 
#include <windows.h>

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;

// Get the number of character cells in the current buffer. 

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer 
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write 
                                    coordScreen,     // Coordinates of first cell 
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer 
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute 
                                    coordScreen,      // Coordinates of first cell 
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}

int main( void )
{
    HANDLE hStdout;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(hStdout);
    
    return 0;
}

Saturday, November 17, 2012

VC++ Important pre-define pre-processor

_UNICODE
Is unicode set or not set

_DEBUG
Is configuration set to debug or release

Wednesday, September 12, 2012

C++ CODE Set File Attributes to Read or Write

Include following header files

#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <io.h>


//Code Start


#include "stdafx.h"

void printMyMsg();

int setRead(wchar_t *);
int setWrite(wchar_t *);

char * wcharToChar(wchar_t *);

int _tmain(int argc, _TCHAR* argv[])
{
    if(argc < 3)
    {
        printMyMsg();
        exit(2);
    }
    if(!_wcsicmp(argv[1],L"-r"))
    {
        setRead(argv[2]);
        exit(2);
    }
    else if(!_wcsicmp(argv[1],L"-w"))
    {
        setWrite(argv[2]);
        exit(2);
    }
    else
    {
        printMyMsg();
        exit(2);
    }

    return 0;
}

int setRead(wchar_t * fname)
{
    return(_chmod(wcharToChar(fname),_S_IREAD));
}
int setWrite(wchar_t * fname)
{
    return(_chmod(wcharToChar(fname),_S_IWRITE));
}
void printMyMsg()
{
    printf("\nUsage: SetFileAttribute -R|-W <filename>");
    printf("\n");
    system("pause");
}
char * wcharToChar(wchar_t * inStr)
{
    size_t len = wcslen(inStr);
    char * tchar = (char *)malloc(len+1);
    wcstombs_s(NULL,tchar,len+1,inStr,len+1);
    return(tchar);
}

C++ Convert Between Various String Types

online tool to convert from one datatype to another

http://www.convertdatatypes.com/Language-CPlusPlus.html

Friday, June 15, 2012

MSDN usefull Links

C++ UNREFERENCED_PARAMETER

Let's start with UNREFERENCED_PARAMEER. This macro is defined in winnt.h, like so:

#define UNREFERENCED_PARAMETER(P) (P)
 

In other words, UNREFERENCED_PARAMETER expands to the parameter or expression passed.Its purpose is to avoid compiler warnings about unreferenced parameters.Many programmers, including yours truly,
like to compile with the highest warning level,Level 4 (/W4).Level 4 warnings fall into the category of "things that can be safely ignored." Little infelicities that won't break your code, though they might make you look bad.For example, you might have some line of code in your program like this

int x=1;

but you never use x. Perhaps this line is left over from a time when you did use x,but then you removed the code and forgot to remove the variable.Warning Level 4 can find these minor mishaps.So why not let the compiler help you achieve the highest level of professionalism possible? Compiling with Level 4 is a way to show pride in your work. Level 4 is de rigueur if you're writing a library for public consumption. You don't want to force your developers to use a lower level to compile their code cleanly.

MSDN Link

Wednesday, May 30, 2012

C++ String Comparison


Case sensitive

int strcmp
    (const char *string1,const char *string2 );

int wcscmp
    (const wchar_t *string1,const wchar_t *string2 );

int _mbscmp
    (const unsigned char *string1,const unsigned char *string2 );

Case insensitive

int _stricmp
    (const char *string1,const char *string2);

int _wcsicmp
    (const wchar_t *string1,const wchar_t *string2);

int _mbsicmp
    (const unsigned char *string1,const unsigned char *string2);

Tuesday, May 29, 2012

C++ targetver.h

What's the difference between WINVER, _WIN32_WINNT, _WIN32_WINDOWS, and _WIN32_IE?

#define WINVER         0x0400
#define _WIN32_WINNT   0x0400
#define _WIN32_WINDOWS 0x0400
#define _WIN32_IE      0x0400

Let's take them in order.

The WINVER symbol is the earliest one. That's the symbol that 16-bit Windows used to control the versioning of its header files, and its use carried forward into the 32-bit header files, presumably from the people who did the initial conversion of the header files to 32-bit and who grew up with the WINVER symbol. This symbol is still used a lot in the header files that can trace their origins to 16-bit Windows, such as winuser.h, wingdi.h, and mmsystem.h.

The _WIN32_WINNT symbol came next. I'm not sure where it came from, but from its name it probably was invented by the Windows NT team in order to allow them to block off sections of the header file that are available only in the Windows NT implementation of Win32. Don't forget that in the early days, there was also Win32s, a subset of Win32 that could run on 16-bit Windows 3.1. The single WINVER symbol wasn't enough to specify exactly what you wanted to be compatible with. For example, a function available only in Windows NT 3.1 would be guarded with #if _WIN32_WINNT >= 0x030A so that programs that wanted to run on Win32s could set _WIN32_WINNT to zero and keep that function off-limits.

Similarly, both Windows 95 and Windows NT 4 identified themselves as Windows major version 4, so the WINVER symbol was insufficient to distinguish them. Functions that existed in Windows NT 4 but not in Window 95 were therefore guarded with _WIN32_WINNT.

On the other hand, there were also functions that were first introduced in Windows 95 and did not exist in the original version of Windows NT 4. The _WIN32_WINDOWS symbol let you specify that you wanted access to stuff that was new for Windows 95 and which would also be ported to Windows NT 4 and future versions of Windows NT. 
 


Continue Reading... 
 



Minimum system required                 Value for NTDDI_VERSION
Windows 7                               NTDDI_WIN7     (0x06010000)
Windows Server 2008                     NTDDI_WS08     (0x06000100)
Windows Vista (SP1)                     NTDDI_VISTASP1 (0x06000100)
Windows Vista                           NTDDI_VISTA    (0x06000000)
Windows Server 2003 (SP2)               NTDDI_WS03SP2  (0x05020200)
Windows Server 2003 (SP1)               NTDDI_WS03SP1  (0x05020100)
Windows Server 2003                     NTDDI_WS03     (0x05020000)
Windows XP (SP3)                        NTDDI_WINXPSP3 (0x05010300)
Windows XP (SP2)                        NTDDI_WINXPSP2 (0x05010200)
Windows XP (SP1)                        NTDDI_WINXPSP1 (0x05010100)
Windows XP                              NTDDI_WINXP    (0x05010000)
Windows 2000(SP4)                       NTDDI_WIN2KSP4 (0x05000400)
Windows 2000(SP3)                       NTDDI_WIN2KSP3 (0x05000300)
Windows 2000(SP2)                       NTDDI_WIN2KSP2 (0x05000200)
Windows 2000(SP1)                       NTDDI_WIN2KSP1 (0x05000100)
Windows 2000                            NTDDI_WIN2K    (0x05000000)



Minimum system required    value for _WIN32_WINNT and WINVER
Windows 7                  _WIN32_WINNT_WIN7  (0x0601)
Windows Server 2008        _WIN32_WINNT_WS08  (0x0600)
Windows Vista              _WIN32_WINNT_VISTA (0x0600)

Windows Server 2003 (SP1), _WIN32_WINNT_WS03  (0x0502)
Windows XP with SP2

Windows Server 2003,       _WIN32_WINNT_WINXP (0x0501)
Windows XP

Windows 2000               _WIN32_WINNT_WIN2K (0x0500)


Minimum version required     value of _WIN32_IE
Internet Explorer 8.0        _WIN32_IE_IE80    (0x0800)
Internet Explorer 7.0        _WIN32_IE_IE70    (0x0700)
Internet Explorer 6.0 SP2    _WIN32_IE_IE60SP2 (0x0603)
Internet Explorer 6.0 SP1    _WIN32_IE_IE60SP1 (0x0601)
Internet Explorer 6.0        _WIN32_IE_IE60    (0x0600)
Internet Explorer 5.5        _WIN32_IE_IE55    (0x0550)
Internet Explorer 5.01       _WIN32_IE_IE501   (0x0501)
Internet Explorer 5.0,       _WIN32_IE_IE50    (0x0500)
5.0a,
5.0b   

SQL case sensitive

ALTER TABLE mytable
ALTER COLUMN mycolumn VARCHAR(10)
COLLATE SQL_Latin1_General_CP1_CS_AS

This will change the column to be case sensitive.
Any selects or joins on this column will become case sensitive

OR

To perform case sensitive comparison for single query add "COLLATE Latin1_General_CS_AS" in where condation
SELECT *  FROM Users
where Uname COLLATE Latin1_General_CS_AS = 'Name'

Friday, May 25, 2012

C++ File _stat() _stat64i32()

MSDN

#include <sys/types.h> followed by
#include <sys/stat.h>

#define _stat64i32() _stat()

is used to get file information. 
If the file size is larger than 2 GB, the 32-bit status functions return successfully (0) but report an incorrect file size. If you know that the file size might be larger than 2 GB, use one of the 64-bit status functions, such as _stat64 Or following method.

use
FILE * pFile;
fseek (pFile , 0 , SEEK_END);            Move to End
fpos_t pos;
fgetpos(pFile,&pos);                     Get Position

fpos_t is __int64 so 8 byte.